1 下载需要的jar包

Ftp服务器实现文件的上传和下载,主要依赖jar包为:

2 搭建ftp服务器

参考Windows 上搭建Apache FtpServer,搭建ftp服务器

3 主要代码

在eclipse中实现ftp的上传和下载功能还是很简单的,在编码过程中遇到的一个bug就是对于ftp中中文文件的下载不是乱码,就是下载后文件的大小是0KB。后来发现问题在于eclipse的编码,更改为“utf-8”,在上传和下载的时候,设置ftp服务端目录的名字,编码为iso-8859-1格式。

package T0728;

import java.io.FileInputStream;
import java.io.FileOutputStream; import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply; class FtpUtil {
private FTPClient ftpClient;
private String serverIp;
private int port;
private String userName;
private String passWord;
public FtpUtil(String serverIp, int port, String userName, String passWord) {
super();
this.serverIp = serverIp;
this.port = port;
this.userName = userName;
this.passWord = passWord;
} /**
* 连接ftp服务器
* @return
*/
public boolean open(){
if(ftpClient != null && ftpClient.isConnected())
return true;
//连接服务器
try{
ftpClient = new FTPClient();
ftpClient.connect(serverIp, port); //连接服务器
ftpClient.login(userName, passWord); //登录 ftpClient.setBufferSize(1024);
//设置文件类型,二进制
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); int reply = ftpClient.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)){ //判断ftp服务器是否连通
this.closeFtp();
System.out.println("FtpServer连接失败!");
return false; }
return true;
}catch(Exception e){
this.closeFtp();
e.printStackTrace();
return false;
}
} /**
* 关闭ftp服务器,主要是对disconnect函数的调用
*/
public void closeFtp() {
try {
if (ftpClient != null && ftpClient.isConnected())
ftpClient.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Close Server Success :"+this.serverIp+";port:"+this.port);
} /**
* 从ftp服务器下载文件
* @param ftpDirectoryAndFileName 包含ftp部分的文件路径和名字,这里是从ftp设置的根目录开始
* @param localDirectoryAndFieName 本文的文件路径和文件名字,相当于是绝对路径
* @return
*/
public boolean donwLoad(String ftpDirectoryAndFileName,String localDirectoryAndFieName){
if(!ftpClient.isConnected()){
return false;
}
FileOutputStream fos =null;
try {
fos = new FileOutputStream(localDirectoryAndFieName);
//下面的函数实现文件的下载功能,参数的设置解决了ftp服务中的中文问题。这里要记得更改eclipse的编码格式为utf-8
ftpClient.retrieveFile(new String(ftpDirectoryAndFileName.getBytes(), "iso-8859-1"), fos);
fos.close();
return true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return false;
}finally{ this.closeFtp();
} } /**
*从本地上传文件到ftp服务器
* @param ftpDirectoryAndFileName
* @param localDirectoryAndFieName
* @return
*/
public boolean upLoading(String ftpDirectoryAndFileName,String localDirectoryAndFieName){
if(!ftpClient.isConnected()){
return false;
} FileInputStream fis = null; try {
fis = new FileInputStream(localDirectoryAndFieName);
//和文件的下载基本一致,但是要注意流的写法
ftpClient.storeFile(new String(ftpDirectoryAndFileName.getBytes(), "iso-8859-1"), fis);
fis.close();
return true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return false;
}finally{
this.closeFtp();
}
} }

测试代码

package T0728;

public class FtpMain {

    public static void main(String[] args) {
// TODO Auto-generated method stub FtpUtil ftpUtil = new FtpUtil("168.33.51.174", 2121, "admin", "123456");
if(ftpUtil.open()){
//ftpUtil.donwLoad("/中.txt", "E:/ftp2/中文.txt");
ftpUtil.upLoading("/hh/2.mp3", "E:/ftp2/1.mp3");
}
} }

最新文章

  1. CentOS7.2 创建本地YUM源和局域网YUM源
  2. Nginx + PHP 缓存详解
  3. Vivado学习笔记_002
  4. 在动态引用DLL-A中,当参数是个实体,而实体的属性在另一个DLL-B中。。我们需要得到A这个实体并将其赋值,并将赋值的实体传人DLL-A的方法中。
  5. eclipse sysout快捷输入启用
  6. [Windows Azure] 使用 Windows Azure 快速搭建 Redis 服务器
  7. [PCB设计] 3、用CAM350修改GERBER文件(删除某些部分)
  8. Webpack 2 视频教程 018 - 使用可视化图表进行统计分析打包过程
  9. opencv学习之路(40)、人脸识别算法——EigenFace、FisherFace、LBPH
  10. javascript--返回顶部效果
  11. C#绘图:带背景,拖鼠标画矩形和直线
  12. eclipse中设置python的版本
  13. boke练习: springboot整合springSecurity出现的问题,传递csrf
  14. ActiveMQ使用例子
  15. linux下Mysql多实例实现
  16. IIS URL Rewrite – Installation and Use
  17. iOS:删除、插入、移动单元格
  18. 解决:springmvc maven 项目搭建完后没有src目录,而且maven导入很慢
  19. MS SQL语句优化
  20. echarts使用记录(三):x/y轴数据和刻度显示及坐标中网格显示、格式化x/y轴数据

热门文章

  1. Mac OS 的命令行 总结
  2. 《Python编程从入门到实践》_第六章_字典
  3. 12.ThreadPoolExecutor线程池原理及其execute方法
  4. SQL Server 实现Split函数
  5. 原生javascript实现网页显示日期时钟效果
  6. thinkphp 单字母函数
  7. ecshop屏蔽wap功能
  8. nodejs+websocket制作聊天室视频教程
  9. Python爬虫从入门到放弃(十)之 关于深度优先和广度优先
  10. Selenium chrome配置不加载图片Python版