import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;
import sun.net.TelnetInputStream;
import sun.net.ftp.FtpClient; public class Ftp {
FtpClient ftpClient = null;
private static Log logger; private static String UserName = null; private static String Password = null; private static String Ip = null; private static Integer Port = null; private static Properties Property = null; private FTPClient FtpClientTEMP = null; private static SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm"); public static final int BINARY_FILE_TYPE = FTP.BINARY_FILE_TYPE; public static final int ASCII_FILE_TYPE = FTP.ASCII_FILE_TYPE;
public static int i=1;
public static Boolean connect_flag = false; public boolean loadFile(Ftp ftp , String[] remoteFileName, String[] localFileName,InputStream is) {
boolean flag = true;
// InputStream is = null;
FileOutputStream os = null;
long result = 0;
try {
FtpClient client = connectServerClient(ftp,is);
for(int i = 0; i < remoteFileName.length; i++){
os = new FileOutputStream(localFileName[i]);
is = client.get(remoteFileName[i]);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
result = result + c;
}
}
} catch (Exception e) {
e.printStackTrace();
logger.debug("本地文件下载失败!", e);
} finally {
try {
is.close();
os.close();
closeConnect(ftp);
} catch (IOException e) {
e.printStackTrace();
}
}
return flag;
} public boolean uploadFile(Ftp ftp,InputStream is,File[] docNew, String[] newFileName) {
boolean flag = true;
try {
connect_flag = connectServer(ftp,is);
       //设置的三步骤
ftp.getFtpClientTEMP().setFileType(BINARY_FILE_TYPE);
ftp.getFtpClientTEMP().enterLocalPassiveMode();
ftp.getFtpClientTEMP().setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
InputStream input = null;
for(int i = 0; i < docNew.length; i++){
File file = docNew[i];
File newFile = new File(newFileName[i]);
String dir = newFile.getParentFile().getPath();
if (!ftp.getFtpClientTEMP().changeWorkingDirectory(dir)) {// 如果不能进入dir下,说明此目录不存在! if (!makeDirectory(ftp,is,newFile.getParentFile().getPath())) { System.out.println("创建文件目录【"+dir+"】失败!");
}
}
changeWorkingDirectory(ftp,"/");// 回到FTP根目录
input = new FileInputStream(file); if (input == null) {
System.out.println("本地文件不存在");
logger.debug("本地文件不存在,请重新选择!"); }
if (newFileName[i].trim().equals("")) { newFileName[i] = file.getName(); }
flag = ftp.getFtpClientTEMP().storeFile(newFileName[i], input);
if (flag) {
System.out.println("upload File succeed"); } else {
System.out.println("upload File false"); }
}
input.close();
} catch (IOException e) {
e.printStackTrace();
logger.debug("本地文件上传失败!", e);
// TODO: handle exception
} catch (Exception e) {
e.printStackTrace();
// logger.debug("本地文件上传失败!", e);
// TODO: handle exception
}finally{
closeConnect(ftp); } return flag;
} public void closeConnect(Ftp ftp) {
FtpClientTEMP = ftp.getFtpClientTEMP();
ftpClient = ftp.getFtpClient();
try {
if(FtpClientTEMP != null){
FtpClientTEMP.logout();
FtpClientTEMP.disconnect();
FtpClientTEMP = null;
} if(ftpClient != null){
ftpClient.closeServer();
ftpClient = null;
}
} catch (Exception e) {
e.printStackTrace();
}
} public void setFileType(int fileType) {
try {
// connectServer();
FtpClientTEMP.setFileType(fileType);
} catch (Exception e) {
e.printStackTrace();
}
} protected FTPClient getFtpClient(Ftp ftp,InputStream is) {
connectServer(ftp,is);
return FtpClientTEMP;
} private static void setArg(InputStream is) {
Property = new Properties();
BufferedInputStream inBuff = null;
try {
// File file = new File(configFile); inBuff = new BufferedInputStream(is); Property.load(inBuff); UserName = Property.getProperty("username");
Password = Property.getProperty("password");
Ip = Property.getProperty("ip");
Port = Integer.parseInt(Property.getProperty("port"));
} catch (FileNotFoundException e1) {
System.out.println("FTP配置文件不存在!");
e1.printStackTrace();
} catch (IOException e) {
System.out.println("FTP配置文件无法读取!");
e.printStackTrace();
} } public boolean connectServer(Ftp ftp,InputStream is) {
FtpClientTEMP = ftp.getFtpClientTEMP();
boolean flag = true;
if (FtpClientTEMP == null) {
int reply;
try {
setArg(is);
FtpClientTEMP = new FTPClient();
FtpClientTEMP.setControlEncoding("GBK");
FtpClientTEMP.setDefaultPort(Port);
FtpClientTEMP.configure(getFtpConfig());
FtpClientTEMP.connect(Ip);
FtpClientTEMP.login(UserName, Password);
FtpClientTEMP.setDefaultPort(Port);
//System.out.print(FtpClient.getReplyString());
reply = FtpClientTEMP.getReplyCode();
FtpClientTEMP.setDataTimeout(120000); if (!FTPReply.isPositiveCompletion(reply)) {
FtpClientTEMP.disconnect();
System.err.println("FTP server refused connection.");
// logger.debug("FTP 服务拒绝连接!");
flag = false;
}
// System.out.println(i);
// i ; } catch (SocketException e) {
flag = false;
e.printStackTrace();
System.err.println("登录ftp服务器【" +Ip+ "】失败,连接超时!");
// logger.debug("登录ftp服务器【" Ip "】失败");
} catch (IOException e) {
flag = false; e.printStackTrace();
System.err.println("登录ftp服务器【"+ Ip +"】失败,FTP服务器无法打开!");
// logger.debug("登录ftp服务器【" Ip "】失败");
} }
return flag;
} public static void changeWorkingDirectory(Ftp ftp , String directory) {
try {
ftp.getFtpClientTEMP().changeWorkingDirectory(directory);
} catch (IOException ioe) {
ioe.printStackTrace();
}
} public void renameFile(Ftp ftp,InputStream is,String oldFileName, String newFileName) {
try {
connectServer(ftp,is);
FtpClientTEMP.rename(oldFileName, newFileName);
} catch (IOException ioe) {
ioe.printStackTrace();
}
} private static FTPClientConfig getFtpConfig() {
FTPClientConfig ftpConfig = new FTPClientConfig(
FTPClientConfig.SYST_UNIX);
ftpConfig.setServerLanguageCode(FTP.DEFAULT_CONTROL_ENCODING);
return ftpConfig;
} private static String iso8859togbk(Object obj) {
try {
if (obj == null)
return "";
else
return new String(obj.toString().getBytes("iso-8859-1"), "GBK");
} catch (Exception e) {
return "";
}
} public boolean makeDirectory(Ftp ftp,InputStream is,String dir) throws IOException {
boolean flag = true;
FtpClientTEMP = ftp.getFtpClientTEMP();
if(FtpClientTEMP == null){
connectServer(ftp,is);
}
flag = FtpClientTEMP.makeDirectory(dir);
if (!flag) {
System.out.println("make Directory " +dir+ " false");
}
return flag;
} public static Log getLogger() {
return logger;
} public static void setLogger(Log logger) {
Ftp.logger = logger;
} public FtpClient connectServerClient(Ftp ftp,InputStream is){
ftpClient = new FtpClient();
setArg(is);
try {
if(Port != -1){
ftpClient.openServer(Ip,Port);
}else{
ftpClient.openServer(Ip);
}
ftpClient.login(UserName, Password);
ftpClient.binary();// 用2进制上传、下载
System.out.println("已登录到\"" + ftpClient.pwd() + "\"目录");
return ftpClient;
}catch (IOException e){
e.printStackTrace();
return null;
}
} public FTPClient getFtpClientTEMP() {
return FtpClientTEMP;
}
public void setFtpClientTEMP(FTPClient ftpClientTEMP) {
FtpClientTEMP = ftpClientTEMP;
} public void setFtpClient(FtpClient ftpClient) {
this.ftpClient = ftpClient;
} public FtpClient getFtpClient() {
return ftpClient;
} }

  

FTPUtil 多文件上传参考代码的更多相关文章

  1. WebLogic 任意文件上传远程代码执行_CVE-2018-2894漏洞复现

    WebLogic 任意文件上传远程代码执行_CVE-2018-2894漏洞复现 一.漏洞描述 Weblogic管理端未授权的两个页面存在任意上传getshell漏洞,可直接获取权限.Oracle 7月 ...

  2. Rxjava+Retrofit2+Okhttp3多文件上传(服务器端代码+客户端代码)

    所有代码亲测可用,如有问题,欢迎指正. 首先在ApiService接口文件中新建文件上传接口 public interface ApiService { static final String BAS ...

  3. js 实现 input type="file" 文件上传示例代码

    在开发中,文件上传必不可少但是它长得又丑.浏览的字样不能换,一般会让其隐藏点其他的标签(图片等)来时实现选择文件上传功能 在开发中,文件上传必不可少,<input type="file ...

  4. WebLogic 任意文件上传 远程代码执行漏洞 (CVE-2018-2894)------->>>任意文件上传检测POC

    前言: Oracle官方发布了7月份的关键补丁更新CPU(Critical Patch Update),其中针对可造成远程代码执行的高危漏洞 CVE-2018-2894 进行修复: http://ww ...

  5. FTP服务器文件上传的代码实现

    方式一: @Test public void testFtpClient() throws Exception { // 1.创建一个FtpClient对象 FTPClient ftpClient = ...

  6. 100)PHP,文件上传总代码整理

    首先是我的目录关系: 然后我的html表单代码: <html> <head> <title>Form</title> <meta http-equ ...

  7. php文件上传参考配置与大文件上传

      PHP用超级全局变量数组$_FILES来记录文件上传相关信息的,在php文件上传之前,可通过调节php.ini中相关配置指令,来控制上传相关细节. 1.file_uploads=on/off   ...

  8. JSCH实现文件上传的代码实例

    package com.vcredit.ddcash.monitor.sendmail; import java.io.File;import java.io.FileInputStream;impo ...

  9. php课程---文件操作及文件上传的代码总结

    php里面文件包含目录和文件两种 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...

随机推荐

  1. 【bzoj1030】: [JSOI2007]文本生成器 字符串-AC自动机-DP

    [bzoj1030]: [JSOI2007]文本生成器 首先把匹配任意一个的个数的问题转化为总个数-没有一个匹配的个数 先构造AC自动机,然后枚举每一位的字母以及在自动机上的位置 f[i][j]为第i ...

  2. 配置IIS使用Python 与常见问题解决

    打开IIS管理器 选择功能视图,然后选择ISAPI和CGI限制 打开后,在右侧操作,点击添加,会出现下图所示 按图中提示填写相应部分,在选择路径时,默认可能是dll文件,改成全部文件即可,然后再选择p ...

  3. Hyper-V 配置虚拟机内网及外网环境

    一.为Hyper-V虚拟机准备内外网环境 1.内网环境——虚拟机与虚拟机及主机可以互通

  4. City Game UVALive - 3029(悬线法求最大子矩阵)

    题意:多组数据(国外题好像都这样),每次n*m矩形,F表示空地,R表示障碍 求最大子矩阵(悬线法模板) 把每个格子向上延伸的空格看做一条悬线 以le[i][j],re[i][j],up[i][j]分别 ...

  5. throw new Error('Cyclic dependency' + nodeRep)

    近日重装node_modules 依赖之后,项目启动报错 throw new Error('Cyclic dependency' + nodeRep) 查找资料后得知 产生这个 bug 的原因是循环引 ...

  6. Python数组(一)

    一.索引 数组中的索引(下标)是从0开始递增的,你可以像下面这样使用编号来访问各个元素: test=['java','C#','C++','html','Spring'] print(test[0]) ...

  7. POJ1051 P,MTHBGWB

    题目来源:http://poj.org/problem?id=1051 题目大意: Morse密码里每个字母用长度不定的点和线来表示,一条信息中字母的编码之间用空隙隔开.下表为Morse密码的编码表: ...

  8. python 分页 封装

    分页 封装 我是在项目根目录创建个分页文件 分页代码: class Pagination(object): def __init__(self, data_num, current_page, url ...

  9. storm local logback

    <configuration> <property name="pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} ...

  10. delete ELK index

    Go to tab “Dev Tools”4. On the left console type:GET _cat/indices?v&s=store.size:descand execute ...