2012-06-12 127 views
3

我正嘗試通過FTP下載文件,但retrieveFileStream返回null,我已經檢查getReplyString並返回200,這是我的代碼:的InputStream返回null

File file = new File(this.config.getDestDirectory() + File.separator + fileName); 
String t = this.ftp.getReplyString(); 
InputStream in = this.ftp.retrieveFileStream(fileName); 
FileOutputStream fos = new FileOutputStream(file); 
StreamUtils.copy(in, fos); 
in.close(); 
fos.close(); 

什麼建議嗎?

非常感謝。

+3

您的InputStream查看'fileName',而不是'file'。這是打算? –

+1

什麼是「ftp」,它來自哪裏? –

+0

感謝您的回答,是的,是根據API的意圖,retrieveFileStream的參數是遠程文件的名稱,在我的代碼文件中是out目錄:FileOutputStream fos = new FileOutputStream(file); – Jjreina

回答

-1
+0

downvoting,因爲它是一個鏈接唯一的答案,現在鏈接已經死了。 X( – d3m0li5h3r

+0

)正如我從源中發現的,「如果在連接建立和初始化過程中的任何時候報告了FTP協議錯誤,都會返回null」,請參閱org.apache.commons.net.ftp.FTPClient#_openDataConnection_(字符串,字符串) – iozee

0

嘗試這種方式。其從我的代碼庫的例子,我已經使用了開源的Apache公地網FTP-2.0。

public void goforIt() 
{ 
    FTPClient con = null; 

    try 
    { 
     con = new FTPClient(); 
     con.connect("10.0.0.1");  // Dummy address 

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

      OutputStream out = new FileOutputStream(new File(data)); 
      boolean result = con.retrieveFile("prerakm4a.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

AFAIK。您可以通過調用completePendingCommand()來確認文件傳輸,並驗證傳輸是否確實成功。即您需要添加調用fos.clos()下面的函數。

fos.close(); client.completePendingCommand()

應該這樣做! 你也可以考慮這個,根據API爲FTPClient.retrieveFileStream(),當它無法打開數據連接時,該方法返回null,在這種情況下,你應該檢查答覆代碼(例如getReplyCode(),getReplyString(),getReplyStrings ())看看它爲什麼失敗。

0

enter image description here

如果您已經驗證了上述所有的答案,仍然有問題,只檢查上述下源設置。如果你有排除:**那麼沒有任何文件可用,如果你只是從編輯選項中刪除**,你的代碼應該工作。我有