本文转载自别人。
最近研究ftp的上传下载。网上一阵搜索,找到以下代码。
主要使用apache中的net包来实现
 
 
  1. public enum UploadStatus {  
  2.     Create_Directory_Fail,      //远程服务器相应目录创建失败          
  3.     Create_Directory_Success,   //远程服务器闯将目录成功          
  4.     Upload_New_File_Success,    //上传新文件成功          
  5.     Upload_New_File_Failed,     //上传新文件失败          
  6.     File_Exits,                 //文件已经存在          
  7.     Remote_Bigger_Local,        //远程文件大于本地文件          
  8.     Upload_From_Break_Success,  //断点续传成功          
  9.     Upload_From_Break_Failed,   //断点续传失败          
  10.     Delete_Remote_Faild;        //删除远程文件失败          
  11. }  
  1. public enum DownloadStatus {  
  2.     Remote_File_Noexist, //远程文件不存在          
  3.     Local_Bigger_Remote, //本地文件大于远程文件          
  4.     Download_From_Break_Success, //断点下载文件成功          
  5.     Download_From_Break_Failed, //断点下载文件失败          
  6.     Download_New_Success, //全新下载文件成功          
  7.     Download_New_Failed; //全新下载文件失败     
  8. }  

 

核心代码:

  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.OutputStream;  
  7. import java.io.PrintWriter;  
  8. import org.apache.commons.net.PrintCommandListener;  
  9. import org.apache.commons.net.ftp.FTP;  
  10. import org.apache.commons.net.ftp.FTPClient;  
  11. import org.apache.commons.net.ftp.FTPFile;  
  12. import org.apache.commons.net.ftp.FTPReply;  
  13.  
  14. public class ContinueFTP {  
  15.     private FTPClient ftpClient = new FTPClient();  
  16.  
  17.     public ContinueFTP() {  
  18.         // 设置将过程中使用到的命令输出到控制台  
  19.         this.ftpClient.addProtocolCommandListener(new PrintCommandListener(  
  20.                 new PrintWriter(System.out)));  
  21.     }  
  22.  
  23.     /**  
  24.      * 连接到FTP服务器  
  25.      *   
  26.      * @param hostname  
  27.      *            主机名  
  28.      * @param port  
  29.      *            端口  
  30.      * @param username  
  31.      *            用户名  
  32.      * @param password  
  33.      *            密码  
  34.      * @return 是否连接成功  
  35.      * @throws IOException  
  36.      */ 
  37.     public boolean connect(String hostname, int port, String username,  
  38.             String password) throws IOException {  
  39.         ftpClient.connect(hostname, port);  
  40.         if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {  
  41.             if (ftpClient.login(username, password)) {  
  42.                 return true;  
  43.             }  
  44.         }  
  45.         disconnect();  
  46.         return false;  
  47.     }  
  48.  
  49.     /**  
  50.      * 从FTP服务器下载文件,支持断点续传  
  51.      *   
  52.      * @param remote  
  53.      *            远程文件路径。例如:ftp/301/App_1323/1040/eclipse-SDK-3.7-win32.zip  
  54.      *            第一个不用带“/”。这是跟上传的区别  
  55.      * @param local  
  56.      *            本地文件路径  
  57.      * @return 是否成功  
  58.      * @throws IOException  
  59.      */ 
  60.     public boolean download(String remote, String local) throws IOException {  
  61.         ftpClient.enterLocalPassiveMode();  
  62.         ftpClient.setFileType(FTP.BINARY_FILE_TYPE);  
  63.         boolean result;  
  64.         File f = new File(local);  
  65.         FTPFile[] files = ftpClient.listFiles(remote);  
  66.         if (files.length != 1) {  
  67.             System.out.println("远程文件不唯一");  
  68.             return false;  
  69.         }  
  70.         long lRemoteSize = files[0].getSize();  
  71.         if (f.exists()) {  
  72.             OutputStream out = new FileOutputStream(f, true);  
  73.             System.out.println("本地文件大小为:" + f.length());  
  74.             if (f.length() >= lRemoteSize) {  
  75.                 System.out.println("本地文件大小大于远程文件大小,下载中止");  
  76.                 return false;  
  77.             }  
  78.             ftpClient.setRestartOffset(f.length());  
  79.             result = ftpClient.retrieveFile(remote, out);  
  80.             out.close();  
  81.         } else {  
  82.             OutputStream out = new FileOutputStream(f);  
  83.             result = ftpClient.retrieveFile(remote, out);  
  84.             out.close();  
  85.         }  
  86.         return result;  
  87.     }  
  88.  
  89.     /**  
  90.      * 上传文件到FTP服务器,支持断点续传  
  91.      *   
  92.      * @param local  
  93.      *            本地文件名称,绝对路径  
  94.      * @param remote  
  95.      *            远程文件路径,使用/home/directory1/subdirectory/file.ext  
  96.      *            按照Linux上的路径指定方式,支持多级目录嵌套,支持递归创建不存在的目录结构  
  97.      * @return 上传结果  
  98.      * @throws IOException  
  99.      */ 
  100.     public UploadStatus upload(String local, String remote) throws IOException {  
  101.         // 设置PassiveMode传输  
  102.         ftpClient.enterLocalPassiveMode();  
  103.         // 设置以二进制流的方式传输  
  104.         ftpClient.setFileType(FTP.BINARY_FILE_TYPE);  
  105.         UploadStatus result;  
  106.         // 对远程目录的处理  
  107.         String remoteFileName = remote;  
  108.         if (remote.contains("/")) {  
  109.             remoteFileName = remote.substring(remote.lastIndexOf("/") + 1);  
  110.             String directory = remote.substring(0, remote.lastIndexOf("/") + 1);  
  111.             if (!directory.equalsIgnoreCase("/")  
  112.                     && !ftpClient.changeWorkingDirectory(directory)) {  
  113.                 // 如果远程目录不存在,则递归创建远程服务器目录  
  114.                 int start = 0;  
  115.                 int end = 0;  
  116.                 if (directory.startsWith("/")) {  
  117.                     start = 1;  
  118.                 } else {  
  119.                     start = 0;  
  120.                 }  
  121.                 end = directory.indexOf("/", start);  
  122.                 while (true) {  
  123.                     String subDirectory = remote.substring(start, end);  
  124.                     if (!ftpClient.changeWorkingDirectory(subDirectory)) {  
  125.                         if (ftpClient.makeDirectory(subDirectory)) {  
  126.                             ftpClient.changeWorkingDirectory(subDirectory);  
  127.                         } else {  
  128.                             System.out.println("创建目录失败");  
  129.                             return UploadStatus.Create_Directory_Fail;  
  130.                         }  
  131.                     }  
  132.  
  133.                     start = end + 1;  
  134.                     end = directory.indexOf("/", start);  
  135.  
  136.                     // 检查所有目录是否创建完毕  
  137.                     if (end <= start) {  
  138.                         break;  
  139.                     }  
  140.                 }  
  141.             }  
  142.         }  
  143.  
  144.         // 检查远程是否存在文件  
  145.         FTPFile[] files = ftpClient.listFiles(remoteFileName);  
  146.         if (files.length == 1) {  
  147.             long remoteSize = files[0].getSize();  
  148.             File f = new File(local);  
  149.             long localSize = f.length();  
  150.             if (remoteSize == localSize) {  
  151.                 return UploadStatus.File_Exits;  
  152.             } else if (remoteSize > localSize) {  
  153.                 return UploadStatus.Remote_Bigger_Local;  
  154.             }  
  155.  
  156.             // 尝试移动文件内读取指针,实现断点续传  
  157.             InputStream is = new FileInputStream(f);  
  158.             if (is.skip(remoteSize) == remoteSize) {  
  159.                 ftpClient.setRestartOffset(remoteSize);  
  160.                 if (ftpClient.storeFile(remote, is)) {  
  161.                     return UploadStatus.Upload_From_Break_Success;  
  162.                 }  
  163.             }  
  164.  
  165.             // 如果断点续传没有成功,则删除服务器上文件,重新上传  
  166.             if (!ftpClient.deleteFile(remoteFileName)) {  
  167.                 return UploadStatus.Delete_Remote_Faild;  
  168.             }  
  169.             is = new FileInputStream(f);  
  170.             if (ftpClient.storeFile(remote, is)) {  
  171.                 result = UploadStatus.Upload_New_File_Success;  
  172.             } else {  
  173.                 result = UploadStatus.Upload_New_File_Failed;  
  174.             }  
  175.             is.close();  
  176.         } else {  
  177.             InputStream is = new FileInputStream(local);  
  178.             if (ftpClient.storeFile(remoteFileName, is)) {  
  179.                 result = UploadStatus.Upload_New_File_Success;  
  180.             } else {  
  181.                 result = UploadStatus.Upload_New_File_Failed;  
  182.             }  
  183.             is.close();  
  184.         }  
  185.         return result;  
  186.     }  
  187.  
  188.     /**  
  189.      * 断开与远程服务器的连接  
  190.      *   
  191.      * @throws IOException  
  192.      */ 
  193.     public void disconnect() throws IOException {  
  194.         if (ftpClient.isConnected()) {  
  195.             ftpClient.disconnect();  
  196.         }  
  197.     }  
  198.  
  199.     public static void main(String[] args) {  
  200.         ContinueFTP myFtp = new ContinueFTP();  
  201.         try {  
  202.             myFtp.connect("10.10.92.235"21"userName""passWord");  
  203.             System.out.println(myFtp.download(  
  204.                     "ftp/301/App_1323/1040/eclipse-SDK-3.7-win32.zip",  
  205.                     "E:\\11.zip"));  
  206.             // System.out.println(myFtp  
  207.             // .upload("E:\\11.zip",  
  208.             // "/ftp/301/App_1323/1040/app/cd38bbd9779b415a8d9982e93f4fbff5.zip"));  
  209.             myFtp.disconnect();  
  210.         } catch (IOException e) {  
  211.             System.out.println("连接FTP出错:" + e.getMessage());  
  212.         }  
  213.     }  
  214. }