输入流返回 null

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

有什么建议吗?

非常感谢你。


3
你的 InputStream 看的是 fileName,而不是 file。这是有意为之吗? - S.L. Barth
1
"ftp"是什么,它的起源是什么? - Neil Coffey
感谢您的回答,是的,这是有意的。根据API,retrieveFileStream的参数是远程文件的名称,在我的代码中,file是输出目录:FileOutputStream fos = new FileOutputStream(file); - Jjreina
ftp是FTPClient,我还检查了ftp.isconected并返回true。 - Jjreina
1
我猜想你正在使用Apache Commons FTPClient - S.L. Barth
谢谢一切,问题已解决,retrieveFileStream需要完整路径:ftp网址+文件名。 - Jjreina
4个回答

0

enter image description here

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


0
据我所知,您应该通过调用completePendingCommand()来完成文件传输,并验证传输是否成功。也就是说,您需要添加以下函数调用fos.clos()。

fos.close(); client.completePendingCommand()

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


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

-1

因为这是一个仅包含链接的回答,而且链接已经失效了,所以被踩了。X( - d3m0li5h3r
根据我的了解,如果在建立和初始化连接的任何阶段报告FTP协议错误,则返回null。请参见org.apache.commons.net.ftp.FTPClient#openDataConnection(String, String)。 - iozee

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接