简体   繁体   English

使用 Apache Commons Net FTPClient 从 FTP 服务器循环读取多个文件

[英]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
    }
}
  1. I didn't see the output stream is not properly closed.我没有看到 output stream 没有正确关闭。
  2. finalBytes is o bytes? finalBytes 是 o 字节?
  3. where you defined the buffer variable?你在哪里定义了缓冲区变量?

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.

相关问题 使用org.apache.commons.net.ftp.FTPClient从AsyncTask类登录FTP时出错 - Error when logging into FTP from AsyncTask class using org.apache.commons.net.ftp.FTPClient 无法使用 Apache Commons FTPClient 访问 FTP 服务器上的子文件夹 - Can't access subfolders on FTP server using Apache Commons FTPClient org.apache.commons.net.ftp.FTPClient listFiles 总是从根目录返回文件,而与参数中给出的路径名无关 - org.apache.commons.net.ftp.FTPClient listFiles always returns files from root directory irrespective of the pathname given in argument 使用org.apache.commons.net.ftp.FTPClient保护FTP - Secure FTP with org.apache.commons.net.ftp.FTPClient apache.commons.net.ftp.FTPClient未将文件上传到所需的文件夹 - apache.commons.net.ftp.FTPClient not uploading the file to the required folder 如何使用“ org.apache.commons.net.ftp.FTPClient”解决异常 - How to solve exception with “org.apache.commons.net.ftp.FTPClient” org.apache.commons.net.ftp.FTPClient listFiles()的问题 - Issue with org.apache.commons.net.ftp.FTPClient listFiles() 如何导入 org.apache.commons.net.ftp.FTPClient - How to import org.apache.commons.net.ftp.FTPClient 在Java上下载FTP文件夹(使用FTPClient Apache Commons Net 3.6 API) - Download FTP folder on Java (Using FTPClient Apache Commons Net 3.6 API) 来自 org.apache.commons.net.ftp 的 FTPClient 类中 enterLocal...() 和 enterRemote...() 方法之间的区别 - Difference between enterLocal...() and enterRemote...() methods in FTPClient class from org.apache.commons.net.ftp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM