apache 的 commons-net-3.3.jar 的 ftp java代码上传下载文件

http://joudi.blog.51cto.com/4686277/1546891

http://www.mamicode.com/info-detail-1241612.html

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.genomics.ib.item.control;  
  2.   
  3. import java.io.File;      
  4. import java.io.FileInputStream;      
  5. import java.io.FileOutputStream;  
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;  
  8.   
  9. import org.apache.commons.net.ftp.FTPClient;      
  10. import org.apache.commons.net.ftp.FTPFile;  
  11. import org.apache.commons.net.ftp.FTPReply;      
  12.       
  13. /**  
  14.  * @author   
  15.  * package com.genomics.ib.item.control  
  16.  * @title ItemFtp  
  17.  * @Description :  FTP 上传下载工具类  
  18.  * @time 2013-11-27   
  19.  */  
  20. public class ItemFtp {        
  21.          
  22.     private  FTPClient ftp;        
  23.     /**    
  24.      *     
  25.      * @param path 上传到ftp服务器哪个路径下       
  26.      * @param addr 地址    
  27.      * @param port 端口号    
  28.      * @param username 用户名    
  29.      * @param password 密码    
  30.      * @return    
  31.      * @throws Exception    
  32.      */      
  33.     private  boolean connect(String path,String addr,int port,String username,String password) throws Exception {        
  34.         boolean result = false;        
  35.         ftp = new FTPClient();        
  36.         int reply;        
  37.         ftp.connect(addr,port);        
  38.         ftp.login(username,password);        
  39.         ftp.setFileType(FTPClient.BINARY_FILE_TYPE);        
  40.         reply = ftp.getReplyCode();        
  41.         if (!FTPReply.isPositiveCompletion(reply)) {        
  42.             ftp.disconnect();        
  43.             return result;        
  44.         }        
  45.         ftp.changeWorkingDirectory(path);        
  46.         result = true;        
  47.         return result;        
  48.     }        
  49.       
  50.       
  51.       
  52.     /**  
  53.      * @author   
  54.      * @class  ItemFtp  
  55.      * @title  upload  
  56.      * @Description :   
  57.      * @time 2013 2013-11-27  
  58.      * @return void  
  59.      * @exception :(Error note)  
  60.      * @param file 上传的文件或文件夹  
  61.      * @param path 上传的文件的路径   
  62.      * @throws Exception  
  63.      */  
  64.     private void upload(File file , String path) throws Exception{     
  65.           
  66.         System.out.println( " file.isDirectory() : " + file.isDirectory()  );  
  67.           
  68.         if(file.isDirectory()){             
  69.             ftp.makeDirectory(file.getName());                  
  70.             ftp.changeWorkingDirectory(file.getName());        
  71.             String[] files = file.list();               
  72.             for (int i = 0; i < files.length; i++) {        
  73.                 File file1 = new File(file.getPath()+"\\"+files[i] );        
  74.                 if(file1.isDirectory()){        
  75.                     upload(file1 , path );        
  76.                     ftp.changeToParentDirectory();        
  77.                 }else{                      
  78.                     File file2 = new File(file.getPath()+"\\"+files[i]);        
  79.                     FileInputStream input = new FileInputStream(file2);        
  80.                     ftp.storeFile(file2.getName(), input);        
  81.                     input.close();                              
  82.                 }                   
  83.             }        
  84.         }else{        
  85.             File file2 = new File(file.getPath());      
  86.               
  87.             System.out.println( " file.getPath() : " + file.getPath()  + " | file2.getName() : " + file2.getName() );  
  88.               
  89.             InputStream input = new FileInputStream(file2);     
  90.                
  91.             ftp.changeWorkingDirectory(path);     
  92.             ftp.storeFile(file2.getName(), input);        
  93.               
  94.             input.close();  //关闭输入流  
  95.             ftp.logout();  //退出连接  
  96.         }        
  97.     }      
  98.       
  99.       
  100.          
  101.     /**  
  102.      * @author   
  103.      * @class  ItemFtp  
  104.      * @title  download  
  105.      * @Description : FPT 下载文件方法  
  106.      * @time 2013 2013-11-27  
  107.      * @return void  
  108.      * @exception :(Error note)  
  109.      * @param reomvepath 下载的文件的路径   
  110.      * @param fileName  下载的文件名   
  111.      * @param localPath 下载的文件本地路径  
  112.      * @throws Exception  
  113.      */  
  114.     @SuppressWarnings("unused")  
  115.     private void download(String reomvepath , String fileName , String localPath  ) throws Exception{     
  116.                
  117.             ftp.changeWorkingDirectory(reomvepath);    
  118.               
  119.          // 列出该目录下所有文件  
  120.             FTPFile[] fs = ftp.listFiles();  
  121.             // 遍历所有文件,找到指定的文件  
  122.             for (FTPFile ff : fs) {  
  123.                  if (ff.getName().equals(fileName)) {  
  124.                   // 根据绝对路径初始化文件  
  125.                   File localFile = new File(localPath + "/" + ff.getName());  
  126.                   // 输出流  
  127.                   OutputStream is = new FileOutputStream(localFile);  
  128.                   // 下载文件  
  129.                   ftp.retrieveFile(ff.getName(), is);  
  130.                   System.out.println("下载成功!");  
  131.                   is.close();    
  132.                  }  
  133.             }  
  134.               
  135.             ftp.logout();  //退出连接  
  136.               
  137.     }   
  138.       
  139.    public static void main(String[] args) throws Exception{      
  140.            
  141.        ItemFtp t = new ItemFtp();      
  142.         
  143.        boolean lianjie = t.connect("D:\\", "127.0.0.1", 21, "jiang", "jiang");      
  144.       System.out.println( "连接 :" + lianjie  );  
  145.         
  146.       //上传  
  147. //      File file = new File("d:\\test.txt");    
  148. //      t.upload(file , "E:\\ftptest\\mulu");    
  149.         
  150.       //下载  
  151.       t.download("E:\\ftptest\\mulu", "test.txt", "D:\\db");  
  152.         
  153.         
  154.    }      
  155. }   
借助jar包 apache 的 commons-net-3.3.jar
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值