[英]Reading multiple files in loop from FTP server using Apache Commons Net FTPClient
I have list of files that needs to be read from FTP server.我有需要从 FTP 服务器读取的文件列表。 I have a method readFile(String path, FTPClient client)
which reads and prints the file.我有一个读取和打印文件的方法readFile(String path, FTPClient client)
。
public byte[] readFile(String path,FTPClient client){
InputStream inStream = null;
ByteArrayOutputStream os = null;
byte[] finalBytes = new byte[0];
int reply;
int len;
byte[] buffer = new byte[1024];
try{
os = new ByteArrayOutputStream();
inStream = client.retrieveFileStream(path);
reply = client.getReplyCode();
log.warn("In getFTPfilebytes() :: Reply code -"+reply);
while ((len = inStream.read(buffer)) != -1) {
// write bytes from the buffer into output stream
os.write(buffer, 0, len);
}
finalBytes = os.toByteArray();
if(inStream == null){
throw new Exception("File not found");
}
inStream.close();
}catch(Exception e){
}finally{
try{ inStream.close();} catch(Exception e){}
}
return finalBytes;
}
I am calling above method in loop of list which contains strings of file path.我在包含文件路径字符串的列表循环中调用上述方法。
Issue - In loop only first file is getting read properly.问题 - 在循环中,只有第一个文件被正确读取。 Afterwards, it does not read file and throws an exception.之后,它不读取文件并引发异常。 inStream
gives NULL for second iteration/second file. inStream
为第二次迭代/第二个文件提供 NULL。 Also while iterating first file reply code after retrieveFileStream
is "125(Data connection already open; transfer starting.) "在retrieveFileStream
之后迭代第一个文件回复代码时也是“125(数据连接已经打开;传输开始。) ”
In second iteration it gives "200 (The requested action has been successfully completed.)"在第二次迭代中,它给出“200(请求的操作已成功完成。)”
I am not able to understand what is wrong here.我无法理解这里出了什么问题。 Have not closing inputstream
connection properly?没有正确关闭inputstream
连接?
According to the documentation of the FTPClient.retrieveFileStream()
method ,根据FTPClient.retrieveFileStream()
方法的文档,
You must close the InputStream when you finish reading from it.完成读取后,您必须关闭 InputStream。 The InputStream itself will take care of closing the parent data connection socket upon being closed. InputStream 本身将负责在关闭时关闭父数据连接套接字。
When you close the stream, your client connection will be closed too.当您关闭 stream 时,您的客户端连接也将关闭。 So instead of using the same client over and over, you need to create a new client connection for each file.因此,您需要为每个文件创建一个新的客户端连接,而不是一遍又一遍地使用同一个客户端。
You have to call FTPClient.completePendingCommand
and close the input stream, as the documentation for FTPClient.retrieveFileStream
says:您必须调用FTPClient.completePendingCommand
并关闭输入 stream,因为FTPClient.retrieveFileStream
的文档说:
Returns an InputStream from which a named file from the server can be read.返回一个 InputStream,可以从中读取来自服务器的命名文件。 If the current file type is ASCII, the returned InputStream will convert line separators in the file to the local representation.如果当前文件类型是 ASCII,则返回的 InputStream 会将文件中的行分隔符转换为本地表示。 You must close the InputStream when you finish reading from it.完成读取后,您必须关闭 InputStream。 The InputStream itself will take care of closing the parent data connection socket upon being closed. InputStream 本身将负责在关闭时关闭父数据连接套接字。
To finalize the file transfer you must call
completePendingCommand
and check its return value to verify success.要完成文件传输,您必须调用completePendingCommand
并检查其返回值以验证成功。 If this is not done, subsequent commands may behave unexpectedly.如果不这样做,后续命令可能会出现意外行为。
inStream = client.retrieveFileStream(path);
try {
while ((len = inStream.read(buffer)) != -1) {
// write bytes from the buffer into output stream
os.write(buffer, 0, len);
}
finalBytes = os.toByteArray();
} finally {
inStream.close()
if (!client.completePendingCommand()) {
// error
}
}
please log the path so that we can see the path is correct or not.请记录路径,以便我们可以看到路径是否正确。 I guess the stream which is not properly closed makes the issue我猜 stream 没有正确关闭会导致问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.