FTPUtil 多文件上传参考代码
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 多文件上传参考代码的更多相关文章
- WebLogic 任意文件上传远程代码执行_CVE-2018-2894漏洞复现
WebLogic 任意文件上传远程代码执行_CVE-2018-2894漏洞复现 一.漏洞描述 Weblogic管理端未授权的两个页面存在任意上传getshell漏洞,可直接获取权限.Oracle 7月 ...
- Rxjava+Retrofit2+Okhttp3多文件上传(服务器端代码+客户端代码)
所有代码亲测可用,如有问题,欢迎指正. 首先在ApiService接口文件中新建文件上传接口 public interface ApiService { static final String BAS ...
- js 实现 input type="file" 文件上传示例代码
在开发中,文件上传必不可少但是它长得又丑.浏览的字样不能换,一般会让其隐藏点其他的标签(图片等)来时实现选择文件上传功能 在开发中,文件上传必不可少,<input type="file ...
- WebLogic 任意文件上传 远程代码执行漏洞 (CVE-2018-2894)------->>>任意文件上传检测POC
前言: Oracle官方发布了7月份的关键补丁更新CPU(Critical Patch Update),其中针对可造成远程代码执行的高危漏洞 CVE-2018-2894 进行修复: http://ww ...
- FTP服务器文件上传的代码实现
方式一: @Test public void testFtpClient() throws Exception { // 1.创建一个FtpClient对象 FTPClient ftpClient = ...
- 100)PHP,文件上传总代码整理
首先是我的目录关系: 然后我的html表单代码: <html> <head> <title>Form</title> <meta http-equ ...
- php文件上传参考配置与大文件上传
PHP用超级全局变量数组$_FILES来记录文件上传相关信息的,在php文件上传之前,可通过调节php.ini中相关配置指令,来控制上传相关细节. 1.file_uploads=on/off ...
- JSCH实现文件上传的代码实例
package com.vcredit.ddcash.monitor.sendmail; import java.io.File;import java.io.FileInputStream;impo ...
- php课程---文件操作及文件上传的代码总结
php里面文件包含目录和文件两种 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...
随机推荐
- completer自动完成
由于项目需要,在输入框中要做一些输入限制的同时,更加要求用户体验,提供一些自动完成设置.所以有需求,总会有解决方式,下面说一下自动完成插件的原理: html的body部分: <span styl ...
- C#中工厂模式的作用
1.比如,主要用于对扩展性有要求的功能. 以简单工厂为例: 接口Fun有三个实现 class FunA FunB FunC工厂 class Fac { public static Fun getF ...
- 「十二省联考 2019」异或粽子——tire树+堆
题目 [题目描述] 小粽是一个喜欢吃粽子的好孩子.今天她在家里自己做起了粽子. 小粽面前有 $n$ 种互不相同的粽子馅儿,小粽将它们摆放为了一排,并从左至右编号为 $1$ 到 $n$.第 $i$ 种馅 ...
- oracle修改连接数
使用 sqlplus登陆 sqlplus system 然后切换到sysdba模式 conn ?/ as sysdba 查询当前的processes sessions的大小 show ...
- Jmeter-无法启动,'findstr'不是内部或外部命令,也不是可运行的程序
今天有一个同事的jmeter无法安装,于是帮他看了看,报以下错误: JAVA的环境变量没有配置好,于是重新配置了下环境变量后,再启动,发现还是不好,于是网上查了下, 发现要在电脑的环境变量中增加 pa ...
- 添加fping监控
第一步:安装fping服务 yum -y install fping 第二步:在zabbix-server服务端上启用fping服务 重启zabbix-server 第三步:在主机上添加fping监控 ...
- 使用esp32-Arduino+PubSubClient+mqtt 上传数据到中移动OneNet
使用esp32-doit-dev-v1开发板,测试mqtt协议, 发布(publish)到onenet 平台.注意:1.使用的mqtt arduino 客户端是 pubsubclient 库.其默认是 ...
- Install AntContrib
link: http://www.neiland.net/blog/article/how-to-install-ant-contrib/ Step 1: Get ANT-Contrib And In ...
- poj3274 找平衡数列(哈希加一点数学思维)
题目传送门 题目大意:有n只牛,每只牛有k个属性,接下来n个数字,每个数字的二进制位上的1和0分别表示某种属性的有或者无,然后一个特殊数列就是,一个区间内所有牛的各种属性的总和相等(有e种1属性 e ...
- Jenkins自动化CI CD流水线之1--介绍与安装
第1章 大纲 CI/CD, DevOps介绍 Git安装与使用 Jenkins安装与使用 权限管理 参数化构建 Master-Slave 流水线(Pipeline) 邮件通知 应用案例 自动发布PHP ...