java开发FTP客户端列出指定目录下面所有文件

               

http://zhouzaibao.iteye.com/blog/362866


  1. import java.io.IOException;  
  2. import java.io.PrintWriter;  
  3. import java.util.ArrayList;  
  4. import java.util.Iterator;  
  5. import org.apache.commons.net.PrintCommandListener;  
  6. import org.apache.commons.net.ftp.FTPClient;  
  7. import org.apache.commons.net.ftp.FTPFile;  
  8. import org.apache.commons.net.ftp.FTPReply;  
  9. import org.apache.log4j.Logger;  
  10. /** 
  11.  * 列出FTP服务器上指定目录下面的所有文件 
  12.  * @author BenZhou mailto:[email protected] 
  13.  * 原文地址:http://zhouzaibao.iteye.com/blog/362866  
  14.  */  
  15. public class FTPListAllFiles {  
  16.     private static Logger logger = Logger.getLogger(FTPListAllFiles.class);  
  17.     public FTPClient ftp;  
  18.     public ArrayList<String> arFiles;  
  19.       
  20.     /** 
  21.      * 重载构造函数 
  22.      * @param isPrintCommmand 是否打印与FTPServer的交互命令 
  23.      */  
  24.     public FTPListAllFiles(boolean isPrintCommmand){  
  25.         ftp = new FTPClient();  
  26.         arFiles = new ArrayList<String>();  
  27.         if(isPrintCommmand){  
  28.             ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));  
  29.         }  
  30.     }  
  31.       
  32.     /** 
  33.      * 登陆FTP服务器 
  34.      * @param host FTPServer IP地址 
  35.      * @param port FTPServer 端口 
  36.      * @param username FTPServer 登陆用户名 
  37.      * @param password FTPServer 登陆密码 
  38.      * @return 是否登录成功 
  39.      * @throws IOException 
  40.      */  
  41.     public boolean login(String host,int port,String username,String password) throws IOException{  
  42.         this.ftp.connect(host,port);  
  43.         if(FTPReply.isPositiveCompletion(this.ftp.getReplyCode())){  
  44.             if(this.ftp.login(username, password)){  
  45.                 this.ftp.setControlEncoding("GBK");  
  46.                 return true;  
  47.             }  
  48.         }  
  49.         if(this.ftp.isConnected()){  
  50.             this.ftp.disconnect();  
  51.         }  
  52.         return false;  
  53.     }  
  54.       
  55.     /** 
  56.      * 关闭数据链接 
  57.      * @throws IOException 
  58.      */  
  59.     public void disConnection() throws IOException{  
  60.         if(this.ftp.isConnected()){  
  61.             this.ftp.disconnect();  
  62.         }  
  63.     }  
  64.       
  65.     /** 
  66.      * 递归遍历出目录下面所有文件 
  67.      * @param pathName 需要遍历的目录,必须以"/"开始和结束 
  68.      * @throws IOException 
  69.      */  
  70.     public void List(String pathName) throws IOException{  
  71.         if(pathName.startsWith("/")&&pathName.endsWith("/")){  
  72.             String directory = pathName;  
  73.             //更换目录到当前目录  
  74.             this.ftp.changeWorkingDirectory(directory);  
  75.             FTPFile[] files = this.ftp.listFiles();  
  76.             for(FTPFile file:files){  
  77.                 if(file.isFile()){  
  78.                     arFiles.add(directory+file.getName());  
  79.                 }else if(file.isDirectory()){  
  80.                     List(directory+file.getName()+"/");  
  81.                 }  
  82.             }  
  83.         }  
  84.     }  
  85.       
  86.     /** 
  87.      * 递归遍历目录下面指定的文件名 
  88.      * @param pathName 需要遍历的目录,必须以"/"开始和结束 
  89.      * @param ext 文件的扩展名 
  90.      * @throws IOException  
  91.      */  
  92.     public void List(String pathName,String ext) throws IOException{  
  93.         if(pathName.startsWith("/")&&pathName.endsWith("/")){  
  94.             String directory = pathName;  
  95.             //更换目录到当前目录  
  96.             this.ftp.changeWorkingDirectory(directory);  
  97.             FTPFile[] files = this.ftp.listFiles();  
  98.             for(FTPFile file:files){  
  99.                 if(file.isFile()){  
  100.                     if(file.getName().endsWith(ext)){  
  101.                         arFiles.add(directory+file.getName());  
  102.                     }  
  103.                 }else if(file.isDirectory()){  
  104.                     List(directory+file.getName()+"/",ext);  
  105.                 }  
  106.             }  
  107.         }  
  108.     }  
  109.     public static void main(String[] args) throws IOException {  
  110.         FTPListAllFiles f = new FTPListAllFiles(true);  
  111.         if(f.login("192.168.0.3"21"test""test")){  
  112.             f.List("/","mp4");  
  113.         }  
  114.         f.disConnection();  
  115.         Iterator<String> it = f.arFiles.iterator();  
  116.         while(it.hasNext()){  
  117.             logger.info(it.next());  
  118.         }  
  119.           
  120.     }  
  121. }  

           

猜你喜欢

转载自blog.csdn.net/qq_44910516/article/details/89278586