Commons net实现 FTP上传下载

最近项目中需要到Ftp文件上传,选择了Commons net。Commons net包中的ftp工具类能够帮助我们轻松实现Ftp方式的文件上传/下载。其中最重要的一个类就是FTPClient类,这个提供了许多FTP操作相关的

方法,比如链接,登录,上传,下载,和注销。

FTP 操作的过程一般为连接服务器,登录,进行文件上传/下载,文件(目录)的添加删除修改等操作。平常用的比较多的是文件的上传和下载。

下面是一些基本的上传操作(将Commons net的jar包引入即可使用):

[java]  view plain  copy
  1. public class FtpUtil {  
  2.   
  3.     public static void main(String[] args) {  
  4.   
  5.         FTPClient ftpClient = new FTPClient();  
  6.   
  7.         try {  
  8.             //连接指定服务器,默认端口为21  
  9.             ftpClient.connect("127.0.0.1");  
  10.   
  11.             System.out.println("connect to server");  
  12.             //获取响应字符串(FTP服务器上可设置)  
  13.             String replyString = ftpClient.getReplyString();  
  14.             System.out.println("replyString: " + replyString);  
  15.             //获取响应码用于验证是否连接成功  
  16.             int reply = ftpClient.getReplyCode();  
  17.             if (!FTPReply.isPositiveCompletion(reply)) {  
  18.                 System.out.println("");  
  19.                 System.exit(1);  
  20.             }  
  21.             //设置链接编码,windows主机UTF-8会乱码,需要使用GBK或gb2312编码  
  22.             ftpClient.setControlEncoding("GBK");  
  23.               
  24.             //登录服务器  
  25.             boolean login = ftpClient.login("luojing""luojing");  
  26.             if (login) {  
  27.                 System.out.println("登录成功!");  
  28.             } else {  
  29.                 System.out.println("登录失败!");  
  30.             }  
  31.               
  32.             //获取所有文件和文件夹的名字  
  33.             FTPFile[] files = ftpClient.listFiles();  
  34.               
  35.             for(FTPFile file : files){  
  36.                 if(file.isDirectory()){  
  37.                     System.out.println(file.getName() + " 是文件夹");  
  38.                 }  
  39.                 if(file.isFile()){  
  40.                     System.out.println(file.getName() + " 是文件");  
  41.                 }  
  42.             }  
  43.               
  44.             //生成InputStream用于上传本地文件  
  45.             InputStream in = new FileInputStream("e:\\1.txt");  
  46.               
  47.             //上传文件  
  48.             ftpClient.storeFile("dest.txt",in);  
  49.             in.close();  
  50.               
  51.             //注销登录  
  52.             boolean logout = ftpClient.logout();  
  53.             if (logout) {  
  54.                 System.out.println("注销成功!");  
  55.             } else {  
  56.                 System.out.println("注销失败!");  
  57.             }  
  58.   
  59.         } catch (Exception e) {  
  60.             e.printStackTrace();  
  61.   
  62.         } finally {  
  63.             //关闭链接需要放在finally语句块中  
  64.             if (ftpClient.isConnected()) {  
  65.                 try {  
  66.                     ftpClient.disconnect();  
  67.                 } catch (IOException e) {  
  68.                     e.printStackTrace();  
  69.                 }  
  70.             }  
  71.   
  72.         }  
  73.     }  
  74.   
  75. }  

此外,FTPClient类中也提供了一些文件/文件夹操作的方法。通过commos net提供的方法,可以方便的实现断点传输等功能。我还可以同个retrieveFileStream方法来获取远程服务器中指定文件的一个输入流来供我们手动的进行读操作,也可以使用appendFileStream方法来获取要上传到远程服务器中文件对应的输出流对象,然后我们就可以手动的从本地文件中读取数据然后写入到远程服务中,比如我们想知道上传的进度。总的来说,Commons net提供的方法还是非常好使,非常方便的。一些其他的功能就需要在使用的时候去看API手册了。

猜你喜欢

转载自blog.csdn.net/M_Jack/article/details/80337907