天天看點

java中ftp斷點上傳功能的實作

主要使用apache中的net包來實作。網址http://commons.apache.org/net/。具體包的下載下傳和API文檔請看官網。

    斷點上傳就是在上傳的過程中設定傳輸的起始位置。并設定二進制傳輸。

Java代碼

  1. package apache.net.test;   
  2. import java.io.File;   
  3. import java.io.FileInputStream;   
  4. import java.io.FileOutputStream;   
  5. import java.io.IOException;   
  6. import java.io.InputStream;   
  7. import java.io.OutputStream;   
  8. import java.io.PrintWriter;   
  9. import org.apache.commons.net.PrintCommandListener;   
  10. import org.apache.commons.net.ftp.FTP;   
  11. import org.apache.commons.net.ftp.FTPClient;   
  12. import org.apache.commons.net.ftp.FTPFile;   
  13. import org.apache.commons.net.ftp.FTPReply;   
  14. public class ContinueFTP {   
  15.     private FTPClient ftpClient = new FTPClient();   
  16.     public ContinueFTP(){   
  17.         //設定将過程中使用到的指令輸出到控制台   
  18.         this.ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));   
  19.     }   
  20.     public boolean connect(String hostname,int port,String username,String password) throws IOException{   
  21.         ftpClient.connect(hostname, port);   
  22.         if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){   
  23.             if(ftpClient.login(username, password)){   
  24.                 return true;   
  25.             }   
  26.         }   
  27.         disconnect();   
  28.         return false;   
  29.     }   
  30.     public boolean download(String remote,String local) throws IOException{   
  31.         ftpClient.enterLocalPassiveMode();   
  32.         ftpClient.setFileType(FTP.BINARY_FILE_TYPE);   
  33.         boolean result;   
  34.         File f = new File(local);   
  35.         FTPFile[] files = ftpClient.listFiles(remote);   
  36.         if(files.length != 1){   
  37.             System.out.println("遠端檔案不唯一");   
  38.             return false;   
  39.         }   
  40.         long lRemoteSize = files[0].getSize();   
  41.         if(f.exists()){   
  42.             OutputStream out = new FileOutputStream(f,true);   
  43.             System.out.println("本地檔案大小為:"+f.length());   
  44.             if(f.length() >= lRemoteSize){   
  45.                 System.out.println("本地檔案大小大于遠端檔案大小,下載下傳中止");   
  46.                 return false;   
  47.             }   
  48.             ftpClient.setRestartOffset(f.length());   
  49.             result = ftpClient.retrieveFile(remote, out);   
  50.             out.close();   
  51.         }else {   
  52.             OutputStream out = new FileOutputStream(f);   
  53.             result = ftpClient.retrieveFile(remote, out);   
  54.             out.close();   
  55.         }   
  56.         return result;   
  57.     }   
  58.     public UploadStatus upload(String local,String remote) throws IOException{   
  59.         //設定PassiveMode傳輸   
  60.         ftpClient.enterLocalPassiveMode();   
  61.         //設定以二進制流的方式傳輸   
  62.         ftpClient.setFileType(FTP.BINARY_FILE_TYPE);   
  63.         UploadStatus result;   
  64.         //對遠端目錄的處理   
  65.         String remoteFileName = remote;   
  66.         if(remote.contains("/")){   
  67.             remoteFileName = remote.substring(remote.lastIndexOf("/")+1);   
  68.             String directory = remote.substring(0,remote.lastIndexOf("/")+1);   
  69.             if(!directory.equalsIgnoreCase("/")&&!ftpClient.changeWorkingDirectory(directory)){   
  70.                 //如果遠端目錄不存在,則遞歸建立遠端伺服器目錄   
  71.                 int start=0;   
  72.                 int end = 0;   
  73.                 if(directory.startsWith("/")){   
  74.                     start = 1;   
  75.                 }else{   
  76.                     start = 0;   
  77.                 }   
  78.                 end = directory.indexOf("/",start);   
  79.                 while(true){   
  80.                     String subDirectory = remote.substring(start,end);   
  81.                     if(!ftpClient.changeWorkingDirectory(subDirectory)){   
  82.                         if(ftpClient.makeDirectory(subDirectory)){   
  83.                             ftpClient.changeWorkingDirectory(subDirectory);   
  84.                         }else {   
  85.                             System.out.println("建立目錄失敗");   
  86.                             return UploadStatus.Create_Directory_Fail;   
  87.                         }   
  88.                     }   
  89.                     start = end + 1;   
  90.                     end = directory.indexOf("/",start);   
  91.                     //檢查所有目錄是否建立完畢   
  92.                     if(end <= start){   
  93.                         break;   
  94.                     }   
  95.                 }   
  96.             }   
  97.         }   
  98.         //檢查遠端是否存在檔案   
  99.         FTPFile[] files = ftpClient.listFiles(remoteFileName);   
  100.         if(files.length == 1){   
  101.             long remoteSize = files[0].getSize();   
  102.             File f = new File(local);   
  103.             long localSize = f.length();   
  104.             if(remoteSize==localSize){   
  105.                 return UploadStatus.File_Exits;   
  106.             }else if(remoteSize > localSize){   
  107.                 return UploadStatus.Remote_Bigger_Local;   
  108.             }   
  109.             //嘗試移動檔案内讀取指針,實作斷點續傳   
  110.             InputStream is = new FileInputStream(f);   
  111.             if(is.skip(remoteSize)==remoteSize){   
  112.                 ftpClient.setRestartOffset(remoteSize);   
  113.                 if(ftpClient.storeFile(remote, is)){   
  114.                     return UploadStatus.Upload_From_Break_Success;   
  115.                 }   
  116.             }   
  117.             //如果斷點續傳沒有成功,則删除伺服器上檔案,重新上傳   
  118.             if(!ftpClient.deleteFile(remoteFileName)){   
  119.                 return UploadStatus.Delete_Remote_Faild;   
  120.             }   
  121.             is = new FileInputStream(f);   
  122.             if(ftpClient.storeFile(remote, is)){       
  123.                 result = UploadStatus.Upload_New_File_Success;   
  124.             }else{   
  125.                 result = UploadStatus.Upload_New_File_Failed;   
  126.             }   
  127.             is.close();   
  128.         }else {   
  129.             InputStream is = new FileInputStream(local);   
  130.             if(ftpClient.storeFile(remoteFileName, is)){   
  131.                 result = UploadStatus.Upload_New_File_Success;   
  132.             }else{   
  133.                 result = UploadStatus.Upload_New_File_Failed;   
  134.             }   
  135.             is.close();   
  136.         }   
  137.         return result;   
  138.     }   
  139.     public void disconnect() throws IOException{   
  140.         if(ftpClient.isConnected()){   
  141.             ftpClient.disconnect();   
  142.         }   
  143.     }   
  144.     public static void main(String[] args) {   
  145.         ContinueFTP myFtp = new ContinueFTP();   
  146.         try {   
  147.             myFtp.connect("192.168.21.171", 21, "test", "test");   
  148.             System.out.println(myFtp.upload("E:\\VP6.flv", "/MIS/video/VP6.flv"));   
  149.             myFtp.disconnect();   
  150.         } catch (IOException e) {   
  151.             System.out.println("連接配接FTP出錯:"+e.getMessage());   
  152.         }   
  153.     }   
  154. }