2012-07-23 48 views
3

給定一個URL,我想將該URL中的文件轉移到特定的ftp。 我有兩個選擇: 1)使用外部庫(如apache common-io)我可以從url中獲取文件,然後將其上傳到ftp。 2)打開兩個套接字(到url到ftp),並簡單地將文件從url傳輸到ftp。如何使用java將文件從url傳輸到ftp?

我是對的嗎? 這樣做的最佳方式是什麼?

我正在使用彈簧。

這裏有一些鏈接,我發現它:

How to download and save a file from Internet using Java?

Here is the apache common-io library

編輯

我處理100-200M的文件。

更新: 我選擇先下載文件然後上傳。 我選擇這樣做,因爲我不希望下載文件時出現很多問題,但我期望將文件上載到FTP服務器時出現問題。所以,我認爲錯誤處理會更好。 缺少這種解決方案的原因是我將臨時文件存儲在磁盤中。 我會稍後更新,如果我遇到一些問題。

回答

2
  1. 要求你讀取URL引用的整個文件,然後才能將它發送到ftp服務器。如果文件非常大,則可能需要在從URL連接讀取時將其寫入臨時文件,然後在發送到FTP服務器時從臨時文件讀取。使用這種方法,將URL的讀取和向FTP服務器的寫入保持分開,您可以單獨處理錯誤。例如,如果URL連接引發異常,則可以相應地處理它,並可能重試請求。在嘗試與FTP服務器進行任何通信之前,您可以確保擁有完整的文件。

  2. 如果您建立了輸入和輸出流並準備好讀取和寫入數據流,則可以執行此操作。這意味着你已經完成了所有的協議,並且在FTP服務器上,這意味着告訴它你想上傳一個文件,你想要上傳它,以及文件名是什麼。然後,您可以簡單地從URL輸入流中讀取並將該塊寫入FTP的輸出流。因爲這是流式傳輸的,所以如果您與Web服務器之間的通信,或者您與FTP服務器之間的通信中斷。您可能必須從頭開始重新啓動所有內容。

無論哪種方式都是可以接受的。

+0

謝謝!你的詳細答案真的幫了我很大的忙,我想我會用第一種方式去處理100-200M文件,並且失去連接可能會非常昂貴......因爲即使我需要重新出演,問題出在FTP服務器上。 – Noam 2012-07-23 19:30:17

+0

當您使用2個打開的流時,只能對小數據量(buf,buffer)進行重試。下載連接將長時間打開(您將等待上傳),因此您的連接可能會被拒絕。如果您無法從偏移量繼續下載,則必須重新開始整個過程​​。 – alexsmail 2012-07-24 08:22:37

4

1.坦率地說,如果您使用的是開源的,那麼請利用外部庫。

2.不要重新發明輪子,你有信任庫在您的處置,所以用它。

我還附上我的計劃,我曾經上傳和下載的歌曲ftp server使用apache's common lib

上傳:

public void goforIt(){ 


     FTPClient con = null; 

     try 
     { 
      con = new FTPClient(); 
      con.connect("192.168.2.57"); 

      if (con.login("Administrator", "KUjWbk")) 
      { 
       con.enterLocalPassiveMode(); // important! 
       con.setFileType(FTP.BINARY_FILE_TYPE); 
       String data = "/sdcard/vivekm4a.m4a"; 

       FileInputStream in = new FileInputStream(new File(data)); 
       boolean result = con.storeFile("/vivekm4a.m4a", in); 
       in.close(); 
       if (result) Log.v("upload result", "succeeded"); 
       con.logout(); 
       con.disconnect(); 
      } 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 






    } 

你下載過:

public void goforIt(){ 
    FTPClient con = null; 

    try 
    { 
     con = new FTPClient(); 
     con.connect("192.168.2.57"); 

     if (con.login("Administrator", "KUjWbk")) 
     { 
      con.enterLocalPassiveMode(); // important! 
      con.setFileType(FTP.BINARY_FILE_TYPE); 
      String data = "/sdcard/vivekm4a.m4a"; 

      OutputStream out = new FileOutputStream(new File(data)); 
      boolean result = con.retrieveFile("vivekm4a.m4a", out); 
      out.close(); 
      if (result) Log.v("download result", "succeeded"); 
      con.logout(); 
      con.disconnect(); 
     } 
    } 
    catch (Exception e) 
    { 
     Log.v("download result","failed"); 
     e.printStackTrace(); 
    } 



} 
0
IMPORTS : 
import org.apache.commons.net.ftp.FTP; 
import org.apache.commons.net.ftp.FTPClient; 

CODE : 
public static void main(String args[]) 
try { 
    //Create a FTP Client 
    FTPClient ftp = new FTPClient(); 

    //Set Host Information 
    ftp.connect(HOST_NAME); 
    ftp.login(USER_NAME, PASSW)RD); 

    //Set File Type "Specially for PDF's" 
    ftp.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE); 
    ftp.setFileTransferMode(FTP.BINARY_FILE_TYPE); 

    //Change the current Directory to a desired one 
    ftp.changeWorkingDirectory(SOME_DIRECTORY); 

    //get File from the FTP location 
    String fileNames[] = ftp.listNames(); 
    List<String> fTPFileNameList = new ArrayList<String>(); 

    //Iterate to get File Names 
    if (fileNames != null && fileNames.length > 0) { 
    for (String fName : fileNames) { 
    fTPFileNameList.add((new File(fName)).getName()); 
    } 
    } 

    // Check the file is present on FTP 
    if(fileNames != null && fileNames.length > 0 && fTPFileNameList.contains(FILE_NAME)){ 
    //File is present on FTP 
    } else { 
    //File is not present on FTP 
    String httpURL = "YourDesiredURL"; 
    InputStream fis; 
    try{ 
    fis = new URL(httpURL).openStream(); 
    } catch (FileNotFoundException e) { 
    //File is not present on the URL 
    } 
    ftp.storeFile(FILE_NAME, fis); 
    fis.close(); 
    } 
    ftp.logout(); 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
    } 

MAVEN DEPENDENCY : 

< dependency> 
     < groupId>commons-net< /groupId> 
     < artifactId>commons-net< /artifactId> 
     < version>1.3.0</ version> 
< /dependency> 


JAR LOCATION : 
http://www.jarfinder.com/index.php/java/info/org.apache.commons.net.ftp.FTPClient