我正在尝试使用 apache-commons net FTP lib 从 FTP 服务器获取数据。如果目录中只有 1 个文件,该代码可以正常工作,但在我第二次调用retrieveFileStream() 时始终返回 null。有什么想法吗?我编写了以下示例代码来演示我的问题。
public static void main(String[] args) throws Exception
{
String strLine;
FTPClient client = null;
try{
client = new FTPClient();
client.connect("localhost", 21);
client.enterLocalPassiveMode();
client.login("ftptester", "letmein");
client.changeWorkingDirectory("remote");
FTPFile[] ftpFiles = client.listFiles();
if (ftpFiles != null && ftpFiles.length > 0) {
for (FTPFile file : ftpFiles) {
if (!file.isFile()) {
continue;
}
InputStream fin = client.retrieveFileStream(filepath);
if (fin == null) {
System.out.println("could not retrieve file: " + filepath);
continue;
}
byte[] data = readBytes(fin); // helper method not shown, just processes the input stream
fin.close();
fin = null;
System.out.println("data: " + new String(data));
}
}
}
finally {
... // cleanup code
}
}
哎哟!缺少的魔法是:
completePendingCommand()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)