问题描述
我正在使用 org.apache.commons.net.ftp.FTPClient 并且看到的行为非常......令人困惑.
I'm using org.apache.commons.net.ftp.FTPClient and seeing behavior that is, well... perplexing.
下面的方法打算通过一个 FTPFile 列表,读入它们,然后对内容做一些事情.这一切都在起作用.不(真正)工作的是 FTPClient 对象执行以下操作...
The method beneath intends to go through an FTPFile list, read them in and then do something with the contents. That's all working. What is not (really) working is that the FTPClient object does the following...
1) Properly retrieves and stores the FIRST file in the list
2) List item evaluates to NULL for x number of successive iterations of the loop (x varies on successive attempts
3) manages to retrieve exactly 1 more file in the list
4) reports that it is null for exactly 1 more file in the list
5) hangs indefinitely, reporting no further activity.
public static String mergeXMLFiles(List<FTPFile> files, String rootElementNodeName, FTPClient ftp){
String ret = null;
String fileAsString = null;
//InputStream inStream;
int c;
if(files == null || rootElementNodeName == null)
return null;
try {
System.out.println("GETTING " + files.size() + " files");
for (FTPFile file : files) {
fileAsString = "";
InputStream inStream = ftp.retrieveFileStream(file.getName());
if(inStream == null){
System.out.println("FtpUtil.mergeXMLFiles() couldn't initialize inStream for file:" + file.getName());
continue;//THIS IS THE PART THAT I SEE FOR files [1 - arbitrary number (usually around 20)] and then 1 more time for [x + 2] after [x + 1] passes successfully.
}
while((c = inStream.read()) != -1){
fileAsString += Character.valueOf((char)c);
}
inStream.close();
System.out.println("FILE:" + file.getName() + "\n" + fileAsString);
}
} catch (Exception e) {
System.out.println("FtpUtil.mergeXMLFiles() failed:" + e);
}
return ret;
}
有人见过这样的吗?我是 FTPClient 的新手,我做错了什么吗?
has anyone seen anything like this? I'm new to FTPClient, am I doing something wrong with it?
推荐答案
根据 API for FTPClient.retrieveFileStream()
,该方法返回null
无法打开数据连接,在这种情况下,您应该检查回复代码(例如 getReplyCode()
, getReplyString()
, getReplyStrings()
) 以查看失败的原因.此外,您应该通过调用 completePendingCommand()
并验证传输确实成功.
According to the API for FTPClient.retrieveFileStream()
, the method returns null
when it cannot open the data connection, in which case you should check the reply code (e.g. getReplyCode()
, getReplyString()
, getReplyStrings()
) to see why it failed. Also, you are suppose to finalize file transfers by calling completePendingCommand()
and verifying that the transfer was indeed successful.
这篇关于java中FTPClient类的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!