分享

基于Apache FTP点断续传的文件上传和下载

 心不留意外尘 2016-07-17

http://blog.csdn.net/boonya/article/details/24029917

2014

参考地址:http://www./program/630532.html

基于Apache FTP实现文件上传下载工具 ,上传文件时需要考虑以下问题(实例是续传功能):

(1)、 FTP服务器是否存在改目录,如果不存在目录则需要创建目录。

(2)、判断上传文件是否已经存在,如果存在是需要删除后再上传还是续传。

1、上传或下载状态的枚举类:

  1. package com.scengine.wtms.utils.ftp;  
  2.   
  3. public enum UploadStatus  
  4. {  
  5.     File_Exits(0), Create_Directory_Success(1), Create_Directory_Fail(2), Upload_From_Break_Success(3), Upload_From_Break_Faild(4), Download_From_Break_Success(5), Download_From_Break_Faild(6), Upload_New_File_Success(7), Upload_New_File_Failed(8), Delete_Remote_Success(9), Delete_Remote_Faild(10),Remote_Bigger_Local(11),Remote_smaller_locall(12);  
  6.   
  7.     private int status;  
  8.   
  9.     public int getStatus()  
  10.     {  
  11.         return status;  
  12.     }  
  13.   
  14.     public void setStatus(int status)  
  15.     {  
  16.         this.status = status;  
  17.     }  
  18.   
  19.     UploadStatus(int status)  
  20.     {  
  21.         this.status = status;  
  22.     }  
  23. }  

2、工具类代码:

  1. package com.scengine.wtms.utils.ftp;  
  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.io.PrintWriter;  
  10. import org.apache.commons.net.PrintCommandListener;  
  11. import org.apache.commons.net.ftp.FTP;  
  12. import org.apache.commons.net.ftp.FTPClient;  
  13. import org.apache.commons.net.ftp.FTPFile;  
  14. import org.apache.commons.net.ftp.FTPReply;  
  15.   
  16. public class ContinueFTP  
  17. {  
  18.   
  19.     private FTPClient ftpClient = new FTPClient();  
  20.   
  21.     /** 
  22.      * 对象构造 设置将过程中使用到的命令输出到控制台 
  23.      */  
  24.     public ContinueFTP()  
  25.     {  
  26.         this.ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));  
  27.     }  
  28.   
  29.     /** 
  30.      *  
  31.      * java编程中用于连接到FTP服务器 
  32.      *  
  33.      * @param hostname 
  34.      *            主机名 
  35.      *  
  36.      * @param port 
  37.      *            端口 
  38.      *  
  39.      * @param username 
  40.      *            用户名 
  41.      *  
  42.      * @param password 
  43.      *            密码 
  44.      *  
  45.      * @return 是否连接成功 
  46.      *  
  47.      * @throws IOException 
  48.      */  
  49.   
  50.     public boolean connect(String hostname, int port, String username, String password) throws IOException  
  51.     {  
  52.   
  53.         ftpClient.connect(hostname, port);  
  54.   
  55.         if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode()))  
  56.         {  
  57.   
  58.             if (ftpClient.login(username, password))  
  59.             {  
  60.                 return true;  
  61.             }  
  62.         }  
  63.         disconnect();  
  64.         return false;  
  65.   
  66.     }  
  67.   
  68.     /** 
  69.      *  
  70.      * 从FTP服务器上下载文件 
  71.      *  
  72.      * @param remote 
  73.      *            远程文件路径 
  74.      *  
  75.      * @param local 
  76.      *            本地文件路径 
  77.      *  
  78.      * @return 是否成功 
  79.      *  
  80.      * @throws IOException 
  81.      */  
  82.   
  83.     @SuppressWarnings("resource")  
  84.     public boolean download(String remote, String local) throws IOException  
  85.     {  
  86.   
  87.         ftpClient.enterLocalPassiveMode();  
  88.   
  89.         ftpClient.setFileType(FTP.BINARY_FILE_TYPE);  
  90.   
  91.         boolean result;  
  92.   
  93.         File f = new File(local);  
  94.   
  95.         FTPFile[] files = ftpClient.listFiles(remote);  
  96.   
  97.         if (files.length != 1)  
  98.         {  
  99.             System.out.println("远程文件不唯一");  
  100.             return false;  
  101.         }  
  102.   
  103.         long lRemoteSize = files[0].getSize();  
  104.   
  105.         if (f.exists())  
  106.         {  
  107.             OutputStream out = new FileOutputStream(f, true);  
  108.             System.out.println("本地文件大小为:" + f.length());  
  109.   
  110.             if (f.length() >= lRemoteSize)  
  111.             {  
  112.   
  113.                 System.out.println("本地文件大小大于远程文件大小,下载中止");  
  114.   
  115.                 return false;  
  116.   
  117.             }  
  118.   
  119.             ftpClient.setRestartOffset(f.length());  
  120.   
  121.             result = ftpClient.retrieveFile(remote, out);  
  122.   
  123.             out.close();  
  124.   
  125.         } else  
  126.         {  
  127.   
  128.             OutputStream out = new FileOutputStream(f);  
  129.             result = ftpClient.retrieveFile(remote, out);  
  130.             out.close();  
  131.         }  
  132.   
  133.         return result;  
  134.   
  135.     }  
  136.   
  137.     /** 
  138.      *  
  139.      * 上传文件到FTP服务器,支持断点续传 
  140.      *  
  141.      * @param local 
  142.      *            本地文件名称,绝对路径 
  143.      *  
  144.      * @param remote 
  145.      *            远程文件路径,使用/home/directory1/subdirectory/file.ext 
  146.      *            按照Linux上的路径指定方式,支持多级目录嵌套,支持递归创建不存在的目录结构 
  147.      *  
  148.      * @return 上传结果 
  149.      *  
  150.      * @throws IOException 
  151.      */  
  152.   
  153.     @SuppressWarnings("resource")  
  154.     public UploadStatus upload(String local, String remote) throws IOException  
  155.     {  
  156.   
  157.         // 设置PassiveMode传输  
  158.   
  159.         ftpClient.enterLocalPassiveMode();  
  160.   
  161.         // 设置以二进制流的方式传输  
  162.   
  163.         ftpClient.setFileType(FTP.BINARY_FILE_TYPE);  
  164.   
  165.         UploadStatus result;  
  166.   
  167.         // 对远程目录的处理  
  168.   
  169.         String remoteFileName = remote;  
  170.   
  171.         if (remote.contains("/"))  
  172.         {  
  173.   
  174.             remoteFileName = remote.substring(remote.lastIndexOf("/") + 1);  
  175.   
  176.             String directory = remote.substring(0, remote.lastIndexOf("/") + 1);  
  177.   
  178.             if (!directory.equalsIgnoreCase("/") && !ftpClient.changeWorkingDirectory(directory))  
  179.             {  
  180.   
  181.                 // 如果远程目录不存在,则递归创建远程服务器目录  
  182.   
  183.                 int start = 0;  
  184.   
  185.                 int end = 0;  
  186.   
  187.                 if (directory.startsWith("/"))  
  188.                 {  
  189.   
  190.                     start = 1;  
  191.   
  192.                 } else  
  193.                 {  
  194.   
  195.                     start = 0;  
  196.   
  197.                 }  
  198.   
  199.                 end = directory.indexOf("/", start);  
  200.   
  201.                 while (true)  
  202.                 {  
  203.   
  204.                     String subDirectory = remote.substring(start, end);  
  205.   
  206.                     if (!ftpClient.changeWorkingDirectory(subDirectory))  
  207.                     {  
  208.   
  209.                         if (ftpClient.makeDirectory(subDirectory))  
  210.                         {  
  211.   
  212.                             ftpClient.changeWorkingDirectory(subDirectory);  
  213.   
  214.                         } else  
  215.                         {  
  216.   
  217.                             System.out.println("创建目录失败");  
  218.   
  219.                             return UploadStatus.Create_Directory_Fail;  
  220.   
  221.                         }  
  222.   
  223.                     }  
  224.   
  225.                     start = end + 1;  
  226.   
  227.                     end = directory.indexOf("/", start);  
  228.   
  229.                     // 检查所有目录是否创建完毕  
  230.   
  231.                     if (end <= start)  
  232.                     {  
  233.   
  234.                         break;  
  235.   
  236.                     }  
  237.   
  238.                 }  
  239.   
  240.             }  
  241.   
  242.         }  
  243.   
  244.         // 检查远程是否存在文件  
  245.   
  246.         FTPFile[] files = ftpClient.listFiles(remoteFileName);  
  247.   
  248.         if (files.length == 1)  
  249.         {  
  250.   
  251.             long remoteSize = files[0].getSize();  
  252.   
  253.             File f = new File(local);  
  254.   
  255.             long localSize = f.length();  
  256.   
  257.             if (remoteSize == localSize)  
  258.             {  
  259.   
  260.                 return UploadStatus.File_Exits;  
  261.   
  262.             } else if (remoteSize > localSize)  
  263.             {  
  264.   
  265.                 return UploadStatus.Remote_Bigger_Local;  
  266.   
  267.             }  
  268.   
  269.             // 尝试移动文件内读取指针,实现断点续传  
  270.   
  271.             InputStream is = new FileInputStream(f);  
  272.   
  273.             if (is.skip(remoteSize) == remoteSize)  
  274.             {  
  275.   
  276.                 ftpClient.setRestartOffset(remoteSize);  
  277.   
  278.                 if (ftpClient.storeFile(remote, is))  
  279.                 {  
  280.   
  281.                     return UploadStatus.Upload_From_Break_Success;  
  282.   
  283.                 }  
  284.   
  285.             }  
  286.   
  287.             // 如果断点续传没有成功,则删除服务器上文件,重新上传  
  288.   
  289.             if (!ftpClient.deleteFile(remoteFileName))  
  290.             {  
  291.   
  292.                 return UploadStatus.Delete_Remote_Faild;  
  293.   
  294.             }  
  295.   
  296.             is = new FileInputStream(f);  
  297.   
  298.             if (ftpClient.storeFile(remote, is))  
  299.             {  
  300.   
  301.                 result = UploadStatus.Upload_New_File_Success;  
  302.   
  303.             } else  
  304.             {  
  305.   
  306.                 result = UploadStatus.Upload_New_File_Failed;  
  307.   
  308.             }  
  309.   
  310.             is.close();  
  311.   
  312.         } else  
  313.         {  
  314.   
  315.             InputStream is = new FileInputStream(local);  
  316.   
  317.             if (ftpClient.storeFile(remoteFileName, is))  
  318.             {  
  319.   
  320.                 result = UploadStatus.Upload_New_File_Success;  
  321.   
  322.             } else  
  323.             {  
  324.   
  325.                 result = UploadStatus.Upload_New_File_Failed;  
  326.   
  327.             }  
  328.   
  329.             is.close();  
  330.         }  
  331.   
  332.         return result;  
  333.   
  334.     }  
  335.   
  336.     /** 
  337.      *  
  338.      * 断开与远程服务器的连接 
  339.      *  
  340.      * @throws IOException 
  341.      */  
  342.   
  343.     public void disconnect() throws IOException  
  344.     {  
  345.   
  346.         if (ftpClient.isConnected())  
  347.         {  
  348.             ftpClient.disconnect();  
  349.         }  
  350.   
  351.     }  
  352.   
  353.     public static void main(String[] args)  
  354.     {  
  355.         ContinueFTP myFtp = new ContinueFTP();  
  356.         try  
  357.         {  
  358.   
  359.             myFtp.connect("192.168.1.200", 21, "duser", "HTPDuserXP32");  
  360.   
  361.             System.out.println(myFtp.upload("C:\\Users\\Administrator\\Desktop\\swing.drawer.jar", "/jars/swing.drawer.jar"));  
  362.   
  363.             myFtp.disconnect();  
  364.   
  365.         } catch (IOException e)  
  366.         {  
  367.   
  368.             System.out.println("连接FTP出错:" + e.getMessage());  
  369.   
  370.         }  
  371.   
  372.     }  
  373.   
  374. }  



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多