gpt4 book ai didi

java - 使用 Apache Common Net FTPClient 下载列出的文件时,下载的文件为空或返回的 InputStream 为 null

转载 作者:行者123 更新时间:2023-11-30 05:29:30 25 4
gpt4 key购买 nike

我正在为我的 FTP 客户端项目使用 Apache Commons Net FTP 库。我成功登录,但当我尝试从 FTP 服务器下载文件时,得到空文件或 null InputStream

我尝试设置文件传输模式或文件类型,但都不起作用。

这是我启动客户端的代码示例:

FTPClient client = new FTPClient();
client.connect(host, port);
client.login(user, password);

这是检索文件的代码:

// I do some filtering here, only download file containing certain prefix/suffix/text
FTPFile[] file = client.listFiles(fullDirectory, (file) -> file != null && file.isFile() && file.getName().contains(fileName));
// get the real file
// this will produce file with empty content
client.retrieveFile(file.getName(), new FileOutputStream(new File(file.getName)));

// get InputStream
// this will produce null InputStream
InputStream is = client.retrieveFileStream(file.getName());

我还尝试添加一些附加属性,例如:

FTPClient client = new FTPClient();
client.connect(host, port);
client.setFileType(FTP.ASCII_FILE_TYPE);
client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
client.login(user, password);

但是它们都不起作用。

这可能与服务器配置或其他什么有关吗?实际上我没有 FTP 服务器的配置访问权限。提供商仅授予我对其的读取权限。

最佳答案

您正在列出一个特定目录 (fullDirectory)。但是,您会发现,您正在从当前工作目录下载该文件(未指定完整路径)。所以可能找不到。

尝试将目录路径与文件名组合起来:

fullDirectory + "/" + file.getName()
<小时/>

您可能正在寻找这个:
Download entire FTP directory in Java (Apache Net Commons)

关于java - 使用 Apache Common Net FTPClient 下载列出的文件时,下载的文件为空或返回的 InputStream 为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57864770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com