一个java FTP的客户端

使用的了apache的commons-net实现批量下载,上传,删除等功能:
java 代码
 
  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.net.SocketException;  
  7. import java.util.List;  
  8. import java.util.Properties;  
  9.   
  10. import org.apache.commons.net.ftp.FTPClient;  
  11. import org.apache.commons.net.ftp.FTPFile;  
  12. import org.apache.commons.net.ftp.FTPListParseEngine;  
  13.   
  14. public class FtpClientHandler {  
  15.   
  16.     private FTPClient f              = new FTPClient();  
  17.   
  18.     private String    host           = "10.168.211.37";  
  19.   
  20.     private String    username       = "test";  
  21.   
  22.     private String    password       = "test";  
  23.   
  24.     private String    r_workDirector = "./";  
  25.   
  26.     private String    l_workDirector = "../";  
  27.   
  28.     private String    config_file    = "../config/boss-config.properties";  
  29.   
  30.     private String    remote_files   = "*";  
  31.   
  32.     private String    local_files    = "*";  
  33.   
  34.     public FtpClientHandler() throws IOException {  
  35.         super();  
  36.         init();  
  37.     }  
  38.   
  39.     public void init() throws IOException {  
  40.         FileInputStream fis = new FileInputStream(config_file);  
  41.         Properties config = new Properties();  
  42.         config.load(fis);  
  43.         host = config.getProperty("Boss_FTP_Url");  
  44.         username = config.getProperty("Username");  
  45.         password = config.getProperty("Password");  
  46.         r_workDirector = config.getProperty("Remote_workdirector");  
  47.         l_workDirector = config.getProperty("Local_workdirector");  
  48.   
  49.         remote_files = config.getProperty("Remote_download_files");  
  50.         local_files = config.getProperty("Local_upload_files");  
  51.   
  52.     }  
  53.   
  54.     private void connection() throws SocketException, IOException {  
  55.         f.connect(host);  
  56.         f.login(username,  
  57.                 password);  
  58.         f.changeWorkingDirectory(r_workDirector);  
  59.     }  
  60.   
  61.     private void close() throws IOException {  
  62.         if (f != null)  
  63.             f.disconnect();  
  64.     }  
  65.   
  66.     public void DeleteFiles() throws IOException {  
  67.         connection();  
  68.         FTPListParseEngine engine = f.initiateListParsing(remote_files);  
  69.         FTPFile[] files = engine.getFiles();  
  70.         for (int i = 0; i < files.length; i++) {  
  71.             System.out.print(i + ">>");  
  72.             System.out.println(files[i].getRawListing());  
  73.             f.dele(files[i].getName());  
  74.         }  
  75.         close();  
  76.     }  
  77.   
  78.     public void DownloadFiles() throws FileNotFoundException, IOException {  
  79.         connection();  
  80.         FTPListParseEngine engine = f.initiateListParsing(remote_files);  
  81.         FTPFile[] files = engine.getFiles();  
  82.         for (int i = 0; i < files.length; i++) {  
  83.             System.out.print(i + ">>");  
  84.             System.out.println(files[i].getRawListing());  
  85.             f.retrieveFile(files[i].getName(),  
  86.                            new FileOutputStream(new File(l_workDirector  
  87.                                    + files[i].getName())));  
  88.         }  
  89.         close();  
  90.     }  
  91.   
  92.     public void StorFiles() throws FileNotFoundException, IOException {  
  93.         connection();  
  94.         File file = new File(l_workDirector);// 在此目录中找文件  
  95.   
  96.         BFSFileSearch search = new BFSFileSearch();  
  97.         List fileList = search.scanFiles(file,  
  98.                                          local_files);  
  99.         for (int i = 0; i < fileList.size(); i++) {  
  100.             String fileName = (String) fileList.get(i);  
  101.             f.storeFile(fileName,  
  102.                         new FileInputStream(new File(l_workDirector + fileName)));  
  103.         }  
  104.         close();  
  105.     }  
  106. }  
其中利用深度搜索机制来对*,?等通配符的支持:
java 代码
 
  1. import java.io.File;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4. import java.util.Vector;  
  5.   
  6. public class BFSFileSearch {  
  7.     /** 
  8.      * @param fileName 
  9.      *            String 需要查找的文件,可含通配符如*.java 
  10.      * @param base 
  11.      *            File String 欲查找的文件夹 
  12.      * @param ArrayList 
  13.      *            fileList 结果集 
  14.      * @param count 
  15.      *            int 控制结果数量,为0,表示返回所有的匹配的文件 
  16.      */  
  17.     public void scanFiles(File base, String fileName, List fileList, int count) {  
  18.         Queue queue = new Queue();// 实例化队列  
  19.         queue.put(base);// 入队  
  20.         while (!queue.isEmpty()) {  
  21.             File f = (File) queue.get();// 出队列  
  22.             if (f.exists() && f.isDirectory()) {  
  23.                 String[] files = f.list();  
  24.                 for (int i = 0; i < files.length; i++) {  
  25.                     File f2 = new File(f,  
  26.                                        files[i]);  
  27.                     if (f2.isDirectory()) {// 文件夹则进队列  
  28.                         queue.put(f2);  
  29.                     } else {// 文件则进行匹配  
  30.                         String filePath = f2.getAbsolutePath();  
  31.                         filePath = filePath.substring(filePath.lastIndexOf("/") + 1);// 提取文件名  
  32.                         if (wildcardMatch(fileName,  
  33.                                           filePath)) {// 匹配成功  
  34.                             if (count != 0 && fileList.size() >= count) {  
  35.                                 return;  
  36.                             }  
  37.                             fileList.add(filePath);// 添加到结果集  
  38.                         }  
  39.                     }  
  40.                 }  
  41.             }  
  42.         }  
  43.     }  
  44.   
  45.     /** 
  46.      * overwrite method scanFiles 
  47.      * @param base 
  48.      * @param fileName 
  49.      * @return 
  50.      */  
  51.     public List scanFiles(File base, String fileName) {  
  52.         Queue queue = new Queue();// 实例化队列  
  53.         queue.put(base);// 入队  
  54.         List fileList = new ArrayList();  
  55.         while (!queue.isEmpty()) {  
  56.             File f = (File) queue.get();// 出队列  
  57.             if (f.exists() && f.isDirectory()) {  
  58.                 String[] files = f.list();  
  59.                 for (int i = 0; i < files.length; i++) {  
  60.                     File f2 = new File(f,  
  61.                                        files[i]);  
  62.                     if (f2.isDirectory()) {// 文件夹则进队列  
  63.                         queue.put(f2);  
  64.                     } else {// 文件则进行匹配  
  65.                         String filePath = f2.getAbsolutePath();  
  66.                         filePath = filePath.substring(filePath.lastIndexOf("/") + 1);// 提取文件名  
  67.                         if (wildcardMatch(fileName,  
  68.                                           filePath)) {// 匹配成功  
  69.                             fileList.add(filePath);// 添加到结果集  
  70.                         }  
  71.                     }  
  72.                 }  
  73.             }  
  74.         }  
  75.         return fileList;  
  76.     }  
  77.   
  78.     public boolean wildcardMatch(String pattern, String string) {  
  79.         int stringLength = string.length();  
  80.         int stringIndex = 0;  
  81.         for (int patternIndex = 0; patternIndex < pattern.length(); ++patternIndex) {  
  82.             char c = pattern.charAt(patternIndex);  
  83.   
  84.             if (c == '*') {  
  85.                 while (stringIndex < stringLength) {  
  86.                     if (wildcardMatch(pattern.substring(patternIndex + 1),  
  87.                                       string.substring(stringIndex))) {  
  88.                         return true;  
  89.                     }  
  90.                     ++stringIndex;  
  91.                 }// end of while  
  92.   
  93.             } else if (c == '?') {  
  94.                 ++stringIndex;  
  95.                 if (stringIndex > stringLength) {  
  96.                     return false;  
  97.                 }  
  98.   
  99.             } else {  
  100.                 if (stringIndex >= stringLength  
  101.                         || c != string.charAt(stringIndex)) {  
  102.                     return false;  
  103.                 }  
  104.                 ++stringIndex;  
  105.             }  
  106.         }  
  107.         return stringIndex == stringLength;  
  108.     }  
  109.   
  110.     class Queue {// 先进先出的队列  
  111.         private Vector vector = new Vector();  
  112.   
  113.         public void put(Object object) {// 入队  
  114.             vector.addElement(object);  
  115.         }  
  116.   
  117.         public Object get() {// 出队  
  118.             Object object = peek();  
  119.             if (object != null) {  
  120.                 vector.removeElementAt(0);  
  121.             }  
  122.             return object;  
  123.         }  
  124.   
  125.         public Object peek() {// 取队列首元素  
  126.             if (isEmpty()) {  
  127.                 return null;  
  128.             }  
  129.             return vector.elementAt(0);  
  130.         }  
  131.   
  132.         public boolean isEmpty() {// 队列是否为空  
  133.             return vector.isEmpty();  
  134.         }  
  135.   
  136.         public int size() {// 队列的大小  
  137.             return vector.size();  
  138.         }  
  139.   
  140.     }  
  141. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值