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 ...
随机推荐
- 洛谷P3724 [AH2017/HNOI2017]大佬(决策单调性)
传送门 这个思路很妙诶->这里 以下为了方便,我把自信说成血量好了 虽然表面上看起来每一天有很多种选择,然而我们首先要保证的是不死,然后考虑不死的情况下最多能拿出多少天来进行其他操作.不死可以d ...
- P4094 [HEOI2016/TJOI2016]字符串 后缀数组+主席树+二分答案
$ \color{#0066ff}{ 题目描述 }$ 佳媛姐姐过生日的时候,她的小伙伴从某东上买了一个生日礼物.生日礼物放在一个神奇的箱子中.箱子外边写了一个长为n的字符串s,和m个问题.佳媛姐姐必须 ...
- STL用法
map.find(key) 获取map容器中指定键值x的元素,如果找到,返回此元素的迭代器,否则返回map::end()的迭代器(即查找到容器的末尾都没有找到此元素).
- flask 坑
no python application found, check your startup logs for errors 日志里面报类似于“Mon Mar 23 10:26:49 2015 – ...
- linux中tomcat内存溢出
刚开始测试服务器与线上后台都不能上传10分钟以上的视频,后来只要是视频就不能上传,进入服务器查日志得到如下错误: Caused by: java.lang.OutOfMemoryError: Java ...
- Ruby truthy and falsey
在Ruby里只有false 和nil表示falsey link: https://gist.github.com/jfarmer/2647362
- Codeforces Round #482 (Div. 2) B、Treasure Hunt(模拟+贪心)979B
题目 大致题意 n表示要进行n次操作,接着给出三个字符串,表示三个人初始拥有的串.每次操作要替换字符串中的字母,询问最后在游戏中曾出现过的相同的子串谁最多. 思路 (1) 讨论最多的子串,肯定是全部 ...
- TP框架中D方法和M方法
D()和M()方法的区别: D和M的区别主要在于 M方法不需要创建模型类文件,M方法不会读取模型类,所以默认情况下自动验证是无效的,但是可以通过动态赋值的方式实现 而D方法必须有创建模型类. 我们可以 ...
- 查看当前linux有多少http连接数
已采纳 1.查看apache当前并发访问数: #对比httpd.conf中MaxClients的数字差距多少.netstat -an | grep ESTABLISHED | wc -l 2.查看ht ...
- 用sphinx-doc优雅的写文档
Sphinx 是一个工具,它使得创建一个智能而美丽的文档变得简单.作者Georg Brandl,基于BSD许可证. 起初为写 Python 文档而诞生的 Sphinx,支持为各种语言生成软件开发文档. ...