3

我正在尝试通过 ftp 下载文件,但 retrieveFileStream 返回 null,我检查了 getReplyString 并返回 200,这是我的代码:

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();

有什么建议吗?

非常感谢你。

4

4 回答 4

0

试试这种方式。它是我代码库中的一个示例,我使用了开源 Apache commons-net-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();
    }
}
于 2012-06-14T10:09:06.900 回答
0

AFAIK。您假设通过调用 completePendingCommand() 并验证传输确实成功来完成文件传输。即你需要在fos.clos()下面添加调用函数。

fos.close(); client.completePendingCommand()

应该这样做!你也可以考虑一下,根据 FTPClient.retrieveFileStream() 的 API,该方法在无法打开数据连接时返回 null,这种情况下你应该检查回复代码(例如 getReplyCode()、getReplyString()、getReplyStrings( )) 看看为什么它失败了。

于 2013-06-05T13:02:17.893 回答
0

在此处输入图像描述

如果您已经验证了上述所有答案但仍有问题,只需检查源下的上述设置。如果您有 Excluded:** 那么所有文件都将不可用,如果您只是从 Edit 选项中删除该 ** 您的代码应该可以工作。我有

于 2013-09-17T21:36:30.060 回答
-1

在这里您可以看到此方法何时返回 null: http ://commons.apache.org/net/api-3.1/src-html/org/apache/commons/net/ftp/FTPClient.html#line.1742

于 2012-06-14T09:58:36.717 回答