gpt4 book ai didi

java - 如何正确关闭FTPClient FileStream

转载 作者:行者123 更新时间:2023-12-01 20:08:30 25 4
gpt4 key购买 nike

我正在使用 Apache Commons Net 的 FTPClient 从位于服务器上的文件中读取内容。仅读取一次时效果很好。但是当我尝试读取第二个文件时,FTPClient 的 InputStream 返回 null。这是我的代码:

            FTPClient ftpClient = new FTPClient();
ftpClient.connect("myhostname");
ftpClient.login("myusername", "mypassword");

// read InputStream from file
InputStream inputStream = ftpClient.retrieveFileStream("/my/firstfile.txt");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

// read every line...

// close everything
inputStream.close();
bufferedReader.close();


// second try
inputStream = ftpClient.retrieveFileStream("/my/secondfile.txt");
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

// ...
inputStream.close();
bufferedReader.close();

我做错了什么?

最佳答案

关闭InputStream后,执行以下操作:

ftpClient.completePendingCommand();

您可以在 javadoc of FTPClient#retrieveFileStream 中找到更多信息:

To finalize the file transfer you must call completePendingCommand and check its return value to verify success. If this is not done, subsequent commands may behave unexpectedly.

关于java - 如何正确关闭FTPClient FileStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47040594/

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