2012-06-12 130 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

如果您已经验证了上述所有的答案,仍然有问题,只检查上述下源设置。如果你有排除:**那么没有任何文件可用,如果你只是从编辑选项中删除**,你的代码应该工作。我有