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

[经验分享] 使用Apache commons-net.jar开发FTP工具类

[复制链接]

尚未签到

发表于 2016-6-9 13:31:24 | 显示全部楼层 |阅读模式
最开始使用jdk自带的rt.jar开发FTP工具类,后来发现中文乱码,本机windows是GBK,工程utf-8的,大概看了下jdk的FtpClient的基类NetworkClient默认输出的编码是ISO8859_1。后来决定使用apache的commons-net.jar来开发。下面是具体代码

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.SocketException;
import java.util.TimeZone;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;

public class FtpTest {
private FTPClient ftpClient;
/**
* 链接ftp
* @param ip 地址
* @param port 端口号
* @param user 用户名
* @param password 密码
* @param path 路径
*/
public void connectServer(String ip,int port,String user,String password,String path) {
try {
this.ftpClient = new FTPClient();
FTPClientConfig config = new FTPClientConfig(FTPClientConfig.SYST_NT);//适用的系统
config.setServerTimeZoneId(TimeZone.getDefault().getID());//获取主机默认时区
this.ftpClient.setControlEncoding("GBK");
this.ftpClient.configure(config);
this.ftpClient.connect(ip, port);
int reply = this.ftpClient.getReplyCode();
//ftp服务器链接响应,已2开头通过
if(!FTPReply.isPositiveCompletion(reply)) {
//响应异常断开连接
System.out.println("链接失败!");
this.ftpClient.disconnect();
}
this.ftpClient.login(user, password);
this.ftpClient.changeWorkingDirectory(path);
this.ftpClient.setFileType(FTPClient.FILE_STRUCTURE);//设置文件传输类型
System.out.println("登陆成功,当前所在目录:"+this.ftpClient.printWorkingDirectory());
} catch (SocketException e) {
System.out.println("链接FTP服务器失败!");
e.printStackTrace();
} catch (IOException e) {
System.out.println("登陆FTP服务器失败!");
e.printStackTrace();
}
}
/**
* 关闭FTP服务器连接
*@data 2014-6-10 下午1:51:40
*/
public void closeFtp() {
if(null != this.ftpClient && this.ftpClient.isConnected()) {
try {
boolean rs = this.ftpClient.logout();
if(rs) {
System.out.println("退出并关闭FTP服务器连接!");
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("退出FTP服务器发生异常!");
} finally {
try {
this.ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
System.out.println("强制关闭异常的FTP服务器链接!");
}
}
}
}
/**
* 下载文件,测试通过2GB大小文件,具体看网络环境。
* @param localFilePath 本地路径及文件名
* @param remoteFileName ftp文件目录及文件名,如/test.doc.目录前必须有/否则会找不到文件
* @return
*/
public boolean downloadFile(String localFilePath, String remoteFileName) {
BufferedOutputStream outStream = null;
boolean rs = false;
try {
outStream = new BufferedOutputStream(new FileOutputStream(localFilePath));
rs = this.ftpClient.retrieveFile(remoteFileName, outStream);
System.out.println("文件下载成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("本地文件未找到!");
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件下载发生异常!");
} finally {
if(outStream != null) {
try {
outStream.flush();
outStream.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件传输关闭异常!");
}
}
}
return rs;
}
/**
* 下载文件,测试通过2GB大小文件,具体看网络环境。
* @param localFile 本地文件
* @param remoteFileName FTP服务器文件目录及文件名
* @return
*/
public boolean downloadFile(File localFile, String remoteFileName) {
BufferedOutputStream outStream = null;  
FileOutputStream outStr = null;
boolean rs = false;
try {
outStr = new FileOutputStream(localFile);
outStream = new BufferedOutputStream(outStr);
rs = this.ftpClient.retrieveFile(remoteFileName, outStream);
System.out.println("文件下载成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("本地文件未找到!");
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件下载发生异常!");
} finally {
try {
if(null != outStream) {
outStream.flush();
outStream.close();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("文件传输关闭异常!");
} finally {
if (null != outStr) {  
try {  
outStr.flush();  
outStr.close();  
} catch (IOException e) {  
e.printStackTrace();  
System.out.println("文件输出关闭异常!");
}  
}
}
}
return rs;
}
/**
* 文件上传
* @param localFilePath 文件本地路径
* @param remoteFileName 上传到FTP服务器路径级文件名
* @return
*/
public boolean uploadFile(String localFilePath, String remoteFileName) {
BufferedInputStream inStream = null;
boolean rs = false;
try {
inStream = new BufferedInputStream(new FileInputStream(localFilePath));
rs = this.ftpClient.storeFile(remoteFileName, inStream);
System.out.println("文件上传成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("本地文件未找到!");
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件上传发生异常!");
} finally {  
if (inStream != null) {  
try {  
inStream.close();  
} catch (IOException e) {  
e.printStackTrace();  
System.out.println("文件传输关闭异常!");
}  
}  
}
return rs;
}
/**
* 文件上传
* @param localFile 本地文件
* @param remoteFileName 上传到FTP服务器路径级文件名
* @return
*/
public boolean uploadFile(File localFile, String remoteFileName) {
BufferedInputStream inStream = null;
boolean rs = false;
try {
inStream = new BufferedInputStream(new FileInputStream(localFile));
rs = this.ftpClient.storeFile(remoteFileName, inStream);
System.out.println("文件上传成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("本地文件未找到!");
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件上传发生异常!");
} finally {  
if (inStream != null) {  
try {  
inStream.close();  
} catch (IOException e) {  
e.printStackTrace();  
}  
}  
}
return rs;
}
/**
* 变更工作目录
* @param dir 需变更到的工作目录
*/
public void changeDir(String dir) {
try {
this.ftpClient.changeWorkingDirectory(dir);
System.out.println("当前工作目录为:" + dir);
} catch (IOException e) {
e.printStackTrace();
System.out.println("变更工作目录:" + dir + "时出错!");
}
}
/**
* 返回上级目录
*/
public void toParentDir() {  
try {  
this.ftpClient.changeToParentDirectory();  
System.out.println("成功返回上级目录!");
System.out.println("当前工作目录为:" + this.ftpClient.printWorkingDirectory());
} catch (IOException e) {  
e.printStackTrace();  
System.out.println("返回上级目录时出错!");  
}  
}
/**
* 查询某目录下所有下文件目录
* @return
*/
public String[] getListFiles(String path){
String files[] = null;  
try {  
if (null == path || "".equals(path)) {
path = "/";
}
files = this.ftpClient.listNames(path);  
} catch (IOException e) {  
e.printStackTrace();  
}  
return files;
}
public static void main(String[] args) {
try {
FtpTest ftp = new FtpTest();
ftp.connectServer("192.168.1.133", 21, "ftpAdmin", "admin", "/");
//ftp.downloadFile("d://LOL_V3.1.2.0-V3.1.2.1_PATCH.exe", "/LOL_V3.1.2.0-V3.1.2.1_PATCH.exe");
//ftp.downloadFile(new File("d://test.zip"), "/test.zip");
//ftp.uploadFile("d://test.zip", "/ym/test.zip");
//ftp.uploadFile(new File("d://test.zip"), "/test.zip");
ftp.changeDir("/fasdfasym");
//ftp.toParentDir();
//String[] files = ftp.getListFiles("null");
String[] files = ftp.getListFiles("/ym");
for (String s : files) {
System.out.println(s);
}
ftp.closeFtp();
} catch (Exception e) {
e.printStackTrace();
}
}
}

运维网声明 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-228307-1-1.html 上篇帖子: FTP服务器上传目录修改-vsftps 下篇帖子: 用ant压缩文件并备份到ftp的代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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