设为首页 收藏本站
查看: 1146|回复: 0

[经验分享] FTP的上传下载工具类

[复制链接]

尚未签到

发表于 2016-6-8 11:21:55 | 显示全部楼层 |阅读模式
  下载commons-net的最新包,并引入至项目中。
  以下为相关例子代码:
  

package com.ffcs.icity.common.util;
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.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPHTTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* 提供FTP的文件上传以及下载功能
* @author alan
* @createtime 2013-6-24     
*
*/
public class FtpUtil {
private final static Logger log = LoggerFactory.getLogger(FtpUtil.class);
/**
* Description: 向FTP服务器上传文件
*
* @param url
*            FTP服务器hostname
* @param port
*            FTP服务器端口
* @param username
*            FTP登录账号
* @param password
*            FTP登录密码
* @param path
*            FTP服务器保存目录
* @param filename
*            上传到FTP服务器上的文件名
* @param input
*            输入流
* @return 成功返回true,否则返回false
*/
public static boolean uploadFile(String url, int port, String username,
String password, String path, String filename, InputStream input) {
// 初始表示上传失败
boolean success = false;
FTPClient ftp = null;
String proxySet = System.getProperty("proxySet");
//true表示有设置代理
// 创建FTPClient对象
if("true".equals(proxySet)) {
ftp = new FTPHTTPClient(System.getProperty("http.proxyHost"), Integer.parseInt(System.getProperty("http.proxyPort")));
} else {
ftp = new FTPClient();
}
try {
int reply;
// 连接FTP服务器
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.connect(url, port);
// 登录ftp
ftp.login(username, password);
// 看返回的值是不是230,如果是,表示登陆成功
reply = ftp.getReplyCode();
// 以2开头的返回值就会为真
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
// 转到指定上传目录
ftp.changeWorkingDirectory(path);
//如果有配置代理参数时,需要设置如下模式
if("true".equals(proxySet)) {
ftp.enterLocalPassiveMode();
}
// 将上传文件存储到指定目录
ftp.storeFile(filename, input);
// 关闭输入流
input.close();
// 退出ftp
ftp.logout();
// 表示上传成功
success = true;
} catch (IOException e) {
//e.printStackTrace();
log.error("in FtpUtil.uploadFile has an error,proxySet is {},and e is {}",new Object[]{proxySet,e});
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
/**
* Description: 从FTP服务器下载文件
*
* @param url
*            FTP服务器hostname
* @param port
*            FTP服务器端口
* @param username
*            FTP登录账号
* @param password
*            FTP登录密码
* @param remotePath
*            FTP服务器上的相对路径
* @param fileName
*            要下载的文件名
* @param localPath
*            下载后保存到本地的路径
* @return
*/
public static boolean downFile(String url, int port, String username,
String password, String remotePath, String fileName,
String localPath) {
// 初始表示下载失败
boolean success = false;
FTPClient ftp = null;
String proxySet = System.getProperty("proxySet");
//true表示有设置代理
// 创建FTPClient对象
if("true".equals(proxySet)) {
ftp = new FTPHTTPClient(System.getProperty("http.proxyHost"), Integer.parseInt(System.getProperty("http.proxyPort")));
} else {
ftp = new FTPClient();
}
try {
int reply;
// 连接FTP服务器
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.connect(url, port);
// 登录ftp
ftp.login(username, password);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
} // 转到指定下载目录
ftp.changeWorkingDirectory(remotePath);
//如果有配置代理参数时,需要设置如下模式
if("true".equals(proxySet)) {
ftp.enterLocalPassiveMode();
}
// 列出该目录下所有文件
FTPFile[] fs = ftp.listFiles();
// 遍历所有文件,找到指定的文件
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
File dir = new File(localPath);
if (!dir.exists()) {
if (!dir.mkdirs()) {
throw new IOException("创建保存目录失败");
}
}
// 根据绝对路径初始化文件
File localFile = new File(localPath + "/" + ff.getName());
// 输出流
OutputStream is = new FileOutputStream(localFile);
// 下载文件
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
// 退出ftp
ftp.logout();
// 下载成功
success = true;
} catch (IOException e) {
//e.printStackTrace();
log.error("in FtpUtil.downFile has an error,proxySet is {},and e is {}",new Object[]{proxySet,e});
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
public static void main(String[] args) throws FileNotFoundException {
//String url = "42.120.41.94";
//int port = 21;
//String username = "ftp88888888";
//String password = "ftp@88888888$%^";
//String path = "/";
//String filename = "test.txt";
//InputStream in = new FileInputStream(new File("d://test.txt"));
//long a = System.currentTimeMillis();
//uploadFile(url,port,username,password,path,filename,in);
//long b = System.currentTimeMillis();
//System.out.println("result is OK. time is " + (b - a));
String url = "42.121.5.31";
int port = 21;
String username = "ftp88888888";
String password = "ftp@88888888$%^";
String path = "/m00201123";
String filename = "test12.txt";
InputStream in = new FileInputStream(new File("d://test.txt"));
long a = System.currentTimeMillis();
boolean result = uploadFile(url,port,username,password,path,filename,in);
long b = System.currentTimeMillis();
System.out.println("result is " + result + ". time is " + (b - a));
//String url = "42.121.5.31";
//int port = 21;
//String username = "ftp88888888";
//String password = "ftp@88888888$%^";
//String remotePath = "/home/dmpush_disk/uploads/p88888888/reports";
//String filename = "20130204_4.3-1";
//String localPath = "d://Temp//push//";
//long a = System.currentTimeMillis();
//downFile(url,port,username,password,remotePath,filename,localPath);
//long b = System.currentTimeMillis();
//System.out.println("result is OK. time is " + (b - a));
}

}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-227806-1-1.html 上篇帖子: python 封装的一个FTP功能 下篇帖子: FTP服务器的文件管理
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表