FTPClient中使用completePendingCommand方法注意事项

方法介绍:

    * There are a few FTPClient methods that do not complete the
    * entire sequence of FTP commands to complete a transaction.  These
    * commands require some action by the programmer after the reception
    * of a positive intermediate command.  After the programmer's code
    * completes its actions, it must call this method to receive
    * the completion reply from the server and verify the success of the
    * entire transaction.
   public boolean completePendingCommad() throws IOException;
   {
        return FTPReply.isPositiveCompletion(getReply());
   }

方法介绍中未说明,在何种情况下应该使用该方法。但是跟踪代码可以发现
这是一个同步阻塞方法,如果调用错误,会导致程序卡住假死在这里。

卡住代码
 String line = _controlInput_.readLine();

何时调用?

其实ftp功能,总结来说,只有上传和下载。只有在获取返回流时,才需要调用completePendingCommad方法,因为返回流不是立刻处理的。所以需用手动调用结束方法。

public boolean storeFile(String remote, InputStream local)
public OutputStream storeFileStream(String remote)

public boolean retrieveFile(String remote, OutputStream local)
public InputStream retrieveFileStream(String remote)

我们看到上面4个人方法,其中两个有流返回,另外两个无返回。当调用有返回流方法时,需要手动调用completePendingCommad方法,即第二个和第四个是需要调用completePendingCommad方法,其他两个方法如果调用了,则会产生卡死超时现象。

注意事项

不可多加或者漏加,否则会导致程序卡死

参考链接

commons-net FTPClient API存取设计

Commons-net FTPClient completePendingCommand()经常使程序死掉的原因分析以及解决方式

【特此声明:本文原创,禁止转载!觉得有用打赏一个吧】

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容