程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> JAVA完成FTP斷點上傳的辦法

JAVA完成FTP斷點上傳的辦法

編輯:關於JAVA

JAVA完成FTP斷點上傳的辦法。本站提示廣大學習愛好者:(JAVA完成FTP斷點上傳的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是JAVA完成FTP斷點上傳的辦法正文


本文實例講述了JAVA完成FTP斷點上傳的辦法。分享給年夜家供年夜家參考。詳細剖析以下:

這裡重要應用apache中的net包來完成。網址http://commons.apache.org/net/。詳細包的下載和API文檔請看官網。 斷點上傳就是在上傳的進程中設置傳輸的肇端地位。並設置二進制傳輸。

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.PrintWriter; 
import org.apache.commons.net.PrintCommandListener; 
import org.apache.commons.net.ftp.FTP; 
import org.apache.commons.net.ftp.FTPClient; 
import org.apache.commons.net.ftp.FTPFile; 
import org.apache.commons.net.ftp.FTPReply; 
public class ContinueFTP { 
   private FTPClient ftpClient = new FTPClient(); 
   public ContinueFTP(){ 
     //設置將進程中應用到的敕令輸入到掌握台 
     this.ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); 
   } 
   /** 
   * java編程頂用於銜接到FTP辦事器 
   * @param hostname 主機名 
   * @param port 端口 
   * @param username 用戶名 
   * @param password 暗碼 
   * @return 能否銜接勝利 
   * @throws IOException 
   */ 
   public boolean connect(String hostname,int port,String username,String password) throws IOException{ 
     ftpClient.connect(hostname, port); 
     if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){ 
       if(ftpClient.login(username, password)){ 
         return true; 
       } 
     } 
     disconnect(); 
     return false; 
   } 
   /** 
   * 從FTP辦事器高低載文件 
   * @param remote 長途文件途徑 
   * @param local 當地文件途徑 
   * @return 能否勝利 
   * @throws IOException 
   */ 
   public boolean download(String remote,String local) throws IOException{ 
     ftpClient.enterLocalPassiveMode(); 
     ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
     boolean result; 
     File f = new File(local); 
     FTPFile[] files = ftpClient.listFiles(remote); 
     if(files.length != 1){ 
       System.out.println("長途文件不惟一"); 
       return false; 
     } 
     long lRemoteSize = files[0].getSize(); 
     if(f.exists()){ 
       OutputStream out = new FileOutputStream(f,true); 
       System.out.println("當地文件年夜小為:"+f.length()); 
       if(f.length() >= lRemoteSize){ 
         System.out.println("當地文件年夜小年夜於長途文件年夜小,下載中斷"); 
         return false; 
       } 
       ftpClient.setRestartOffset(f.length()); 
       result = ftpClient.retrieveFile(remote, out); 
       out.close(); 
     }else { 
       OutputStream out = new FileOutputStream(f); 
       result = ftpClient.retrieveFile(remote, out); 
       out.close(); 
     } 
     return result; 
   } 
   /** 
   * 上傳文件到FTP辦事器,支撐斷點續傳 
   * @param local 當地文件稱號,相對途徑 
   * @param remote 長途文件途徑,應用/home/directory1/subdirectory/file.ext 依照Linux上的途徑指定方法,支撐多級目次嵌套,支撐遞歸創立不存在的目次構造 
   * @return 上傳成果 
   * @throws IOException 
   */ 
   public UploadStatus upload(String local,String remote) throws IOException{ 
     //設置PassiveMode傳輸 
     ftpClient.enterLocalPassiveMode(); 
     //設置以二進制流的方法傳輸 
     ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
     UploadStatus result; 
     //對長途目次的處置 
     String remoteFileName = remote; 
     if(remote.contains("/")){ 
       remoteFileName = remote.substring(remote.lastIndexOf("/")+1); 
       String directory = remote.substring(0,remote.lastIndexOf("/")+1); 
       if(!directory.equalsIgnoreCase("/")&&!ftpClient.changeWorkingDirectory(directory)){ 
         //假如長途目次不存在,則遞歸創立長途辦事器目次 
         int start=0; 
         int end = 0; 
         if(directory.startsWith("/")){ 
           start = 1; 
         }else{ 
           start = 0; 
         } 
         end = directory.indexOf("/",start); 
         while(true){ 
           String subDirectory = remote.substring(start,end);
           if(!ftpClient.changeWorkingDirectory(subDirectory)){ 
             if(ftpClient.makeDirectory(subDirectory)){ 
               ftpClient.changeWorkingDirectory(subDirectory); 
             }else { 
               System.out.println("創立目次掉敗"); 
               return UploadStatus.Create_Directory_Fail;
             } 
           } 
           start = end + 1; 
           end = directory.indexOf("/",start); 
           //檢討一切目次能否創立終了 
           if(end <= start){ 
             break; 
           } 
         } 
       } 
     } 
     //檢討長途能否存在文件 
     FTPFile[] files = ftpClient.listFiles(remoteFileName); 
     if(files.length == 1){ 
       long remoteSize = files[0].getSize(); 
       File f = new File(local); 
       long localSize = f.length(); 
       if(remoteSize==localSize){ 
         return UploadStatus.File_Exits; 
       }else if(remoteSize > localSize){ 
         return UploadStatus.Remote_Bigger_Local; 
       } 
       //測驗考試挪動文件內讀取指針,完成斷點續傳 
       InputStream is = new FileInputStream(f); 
       if(is.skip(remoteSize)==remoteSize){ 
         ftpClient.setRestartOffset(remoteSize); 
         if(ftpClient.storeFile(remote, is)){ 
           return UploadStatus.Upload_From_Break_Success; 
         } 
       } 
       //假如斷點續傳沒有勝利,則刪除辦事器上文件,從新上傳 
       if(!ftpClient.deleteFile(remoteFileName)){ 
         return UploadStatus.Delete_Remote_Faild; 
       } 
       is = new FileInputStream(f); 
       if(ftpClient.storeFile(remote, is)){   
         result = UploadStatus.Upload_New_File_Success; 
       }else{ 
         result = UploadStatus.Upload_New_File_Failed; 
       } 
       is.close(); 
     }else { 
       InputStream is = new FileInputStream(local); 
       if(ftpClient.storeFile(remoteFileName, is)){ 
         result = UploadStatus.Upload_New_File_Success; 
       }else{ 
         result = UploadStatus.Upload_New_File_Failed; 
       } 
       is.close(); 
     } 
     return result; 
   } 
   /** 
   * 斷開與長途辦事器的銜接 
   * @throws IOException 
   */ 
   public void disconnect() throws IOException{ 
     if(ftpClient.isConnected()){ 
       ftpClient.disconnect(); 
     } 
   } 
   public static void main(String[] args) { 
     ContinueFTP myFtp = new ContinueFTP(); 
     try { 
       myFtp.connect("192.168.21.171", 21, "test", "test"); 
       System.out.println(myFtp.upload("E:\\VP6.flv", "/MIS/video/VP6.flv")); 
       myFtp.disconnect(); 
     } catch (IOException e) { 
       System.out.println("銜接FTP失足:"+e.getMessage()); 
     } 
   } 
}

願望本文所述對年夜家的java法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved