FTP 工具类. 可以完成对目录创建的创建、修改、删除,对文件的上传下载等操作

  1. package rewin.util;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8. import java.io.OutputStream;  
  9. import java.net.SocketException;  
  10. import java.util.ArrayList;  
  11. import java.util.List;  
  12. import org.apache.commons.net.ftp.FTP;  
  13. import org.apache.commons.net.ftp.FTPClient;  
  14. import org.apache.commons.net.ftp.FTPFile;  
  15. import org.apache.commons.net.ftp.FTPReply;  
  16.   
  17. /** 
  18.  * FTP 工具类. 可以完成对目录创建的创建、修改、删除,对文件的上传下载等操作. 
  19.  * @author yuyoufa 
  20.  * @since 1.4 
  21.  */  
  22. public class FtpUtil {  
  23.       
  24.     /** 
  25.      * FTP客户端 
  26.      */  
  27.     private FTPClient ftp;  
  28.     public static final int BINARY_FILE_TYPE = FTP.BINARY_FILE_TYPE;  
  29.     public static final int ASCII_FILE_TYPE = FTP.ASCII_FILE_TYPE;  
  30.       
  31.     /** 
  32.      * 初始化客户端并完成对服务端的连接 
  33.      * @param ftpConfig ftp配置类 
  34.      * @throws SocketException 
  35.      * @throws IOException 
  36.      */  
  37.     public void connectServer(FtpConfig ftpConfig) throws SocketException,  
  38.             IOException {  
  39.         String server = ftpConfig.getServer();  
  40.         int port = ftpConfig.getPort();  
  41.         String user = ftpConfig.getUsername();  
  42.         String password = ftpConfig.getPassword();  
  43.         String location = ftpConfig.getLocation();  
  44.         connectServer(server, port, user, password, location);  
  45.     }  
  46.       
  47.     /** 
  48.      * 初始化客户端并完成对服务端的连接 
  49.      * @param server 服务端地址 
  50.      * @param port 端口号 
  51.      * @param username 用户名 
  52.      * @param password 密码 
  53.      * @param path 远程路径 值可以为空 
  54.      * @throws SocketException 
  55.      * @throws IOException 
  56.      */  
  57.     public void connectServer(String server, int port, String username,  
  58.             String password, String path) throws SocketException, IOException {  
  59.         ftp = new FTPClient();  
  60.         ftp.connect(server, port);  
  61.         ftp.setDataTimeout(120000);  
  62.         if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {     
  63.             ftp.disconnect();     
  64.             System.out.println(server + " 拒绝连接");     
  65.         }  
  66.         ftp.login(username, password);  
  67.         if (path.length() != 0) {  
  68.             ftp.changeWorkingDirectory(path);  
  69.         }  
  70.     }  
  71.       
  72.     /** 
  73.      * 设置ftp的文件传输类型 
  74.      * @param fileType 如:FTP.BINARY_FILE_TYPE 
  75.      * @throws IOException 
  76.      */  
  77.     public void setFileType(int fileType) throws IOException {  
  78.         ftp.setFileType(fileType);  
  79.     }  
  80.       
  81.     /** 
  82.      * 关闭ftp连接 
  83.      * @throws IOException 
  84.      */  
  85.     public void closeServer() throws IOException {  
  86.         if (ftp != null && ftp.isConnected()) {  
  87.             ftp.logout();  
  88.             ftp.disconnect();  
  89.         }  
  90.     }  
  91.       
  92.     /** 
  93.      * 改变ftp的工作目录 
  94.      * @param path 
  95.      * @return 
  96.      * @throws IOException 
  97.      */  
  98.     public boolean changeDirectory(String path) throws IOException {  
  99.         return ftp.changeWorkingDirectory(path);  
  100.     }  
  101.       
  102.     /** 
  103.      * 在服务端创建目录 
  104.      * @param pathName 可以是相对目录或绝对目录 
  105.      * @return 
  106.      * @throws IOException 
  107.      */  
  108.     public boolean createDirectory(String pathName) throws IOException {  
  109.         return ftp.makeDirectory(pathName);  
  110.     }  
  111.       
  112.     /** 
  113.      * 删除一个FTP服务器上的目录(如果为空) 
  114.      * @param path 目录路径 
  115.      * @return 
  116.      * @throws IOException 
  117.      */  
  118.     public boolean removeDirectory(String path) throws IOException {  
  119.         return ftp.removeDirectory(path);  
  120.     }  
  121.   
  122.     /** 
  123.      * 删除一个FTP服务器上的目录 
  124.      * @param path 目录路径 
  125.      * @param isAll 是否删除所有子目录和文件,如果有 
  126.      * @return 
  127.      * @throws IOException 
  128.      */  
  129.     public boolean removeDirectory(String path, boolean isAll)  
  130.             throws IOException {  
  131.   
  132.         if (!isAll) {  
  133.             return removeDirectory(path);  
  134.         }  
  135.         //遍历子目录和文件  
  136.         FTPFile[] ftpFileArr = ftp.listFiles(path);  
  137.         if (ftpFileArr == null || ftpFileArr.length == 0) {  
  138.             return removeDirectory(path);  
  139.         }  
  140.   
  141.         for (int i = 0; i < ftpFileArr.length; i++) {  
  142.             FTPFile ftpFile = ftpFileArr[i];  
  143.             String name = ftpFile.getName();  
  144.             if (ftpFile.isDirectory()) {  
  145.                 removeDirectory(path + "/" + name, true);  
  146.             } else if (ftpFile.isFile()) {  
  147.                 deleteFile(path + "/" + name);  
  148.             } else if (ftpFile.isSymbolicLink()) {  
  149.                   
  150.             } else if (ftpFile.isUnknown()) {  
  151.                   
  152.             }  
  153.         }  
  154.         return removeDirectory(path);  
  155.     }  
  156.       
  157.     /** 
  158.      * 返回给定目录下的文件 
  159.      * @param path 
  160.      * @return FTPFile组成的集合 
  161.      * @throws IOException 
  162.      */  
  163.     public List getFileList(String path) throws IOException {  
  164.   
  165.         FTPFile[] ftpFiles = ftp.listFiles(path);  
  166.   
  167.         List retList = new ArrayList();  
  168.         if (ftpFiles == null || ftpFiles.length == 0) {  
  169.             return retList;  
  170.         }  
  171.         for (int i = 0; i < ftpFiles.length; i++) {  
  172.             FTPFile ftpFile = ftpFiles[i];  
  173.             if (ftpFile.isFile()) {  
  174.                 retList.add(ftpFile.getName());  
  175.             }  
  176.         }  
  177.         return retList;  
  178.     }  
  179.       
  180.     /** 
  181.      * 删除文件 
  182.      * @param pathName 文件名 
  183.      * @return 删除结果,是否成功. 
  184.      * @throws IOException 
  185.      */  
  186.     public boolean deleteFile(String pathName) throws IOException {  
  187.         return ftp.deleteFile(pathName);  
  188.     }  
  189.       
  190.     /** 
  191.      * 上传文件,并重命名. 
  192.      * @param fileName 上传的文件,包含目录的文件名 
  193.      * @param newName 新的文件名 
  194.      * @return 上传结果,是否成功. 
  195.      * @throws IOException 
  196.      */  
  197.     public boolean uploadFile(String fileName, String newName)  
  198.             throws IOException {  
  199.         boolean flag = false;  
  200.         InputStream iStream = null;  
  201.         try {  
  202.             iStream = new FileInputStream(fileName);  
  203.             flag = ftp.storeFile(newName, iStream);  
  204.         } catch (IOException e) {  
  205.             flag = false;  
  206.             return flag;  
  207.         } finally {  
  208.             if (iStream != null) {  
  209.                 iStream.close();  
  210.             }  
  211.         }  
  212.         return flag;  
  213.     }  
  214.       
  215.     /** 
  216.      * 上传文件 
  217.      * @param fileName 上传的文件,包含目录的文件名 
  218.      * @return 上传结果,是否成功. 
  219.      * @throws IOException 
  220.      */  
  221.     public boolean uploadFile(String fileName) throws IOException {  
  222.         return uploadFile(fileName, fileName);  
  223.     }  
  224.       
  225.     /** 
  226.      * 上传文件,从InputStream 
  227.      * @param iStream 文件流 
  228.      * @param newName 新的文件名 
  229.      * @return 上传结果,是否成功. 
  230.      * @throws IOException 
  231.      */  
  232.     public boolean uploadFile(InputStream iStream, String newName)  
  233.             throws IOException {  
  234.         boolean flag = false;  
  235.         try {  
  236.             flag = ftp.storeFile(newName, iStream);  
  237.         } catch (IOException e) {  
  238.             flag = false;  
  239.             return flag;  
  240.         } finally {  
  241.             if (iStream != null) {  
  242.                 iStream.close();  
  243.             }  
  244.         }  
  245.         return flag;  
  246.     }  
  247.       
  248.     /** 
  249.      * 下载文件 
  250.      * @param remoteFileName 远程文件名 
  251.      * @param localFileName 本地文件 
  252.      * @return 返回操作结果 
  253.      * @throws IOException 
  254.      */  
  255.     public boolean download(String remoteFileName, String localFileName)  
  256.             throws IOException {  
  257.         boolean flag = false;  
  258.         File outfile = new File(localFileName);  
  259.         OutputStream oStream = null;  
  260.         try {  
  261.             oStream = new FileOutputStream(outfile);  
  262.             flag = ftp.retrieveFile(remoteFileName, oStream);  
  263.         } catch (IOException e) {  
  264.             flag = false;  
  265.             return flag;  
  266.         } finally {  
  267.             oStream.close();  
  268.         }  
  269.         return flag;  
  270.     }  
  271.       
  272.     /** 
  273.      * 下载文件,返回InputStream 
  274.      * @param sourceFileName 远程文件 
  275.      * @return InputStream 
  276.      * @throws IOException 
  277.      */  
  278.     public InputStream downFile(String sourceFileName) throws IOException {  
  279.         return ftp.retrieveFileStream(sourceFileName);  
  280.     }  
  281.   
  282.     public FTPClient getFtp() {  
  283.         return ftp;  
  284.     }  
  285.   
  286.     public void setFtp(FTPClient ftp) {  
  287.         this.ftp = ftp;  
  288.     }  
  289.   
  290. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值