Commons-net FTPClient completePendingCommand()经常使程序死掉的原因分析以及解决方式
2015-08-05 14:39
645 查看
commons-net的FTPClient,在使用public InputStream retrieveFileStream(String remote)
方法时需要特别注意,在调用这个接口后,一定要手动close掉返回的InputStream,然后再调用completePendingCommand方法,若不是按照这个顺序,则不对,伪代码:
InputStream is = ftpClient.retrieveFileStream(remote);
is.close();
ftpClient.completePendingCommand();
retrieveFileStream的API文档说的有点罗嗦,还可以使用下列方法来替换上述使用方式
使用一个中间文件来做一个转接,这种方式比上述方法的好处就是自己容易控制,不容易出问题。伪代码如下:
File localFile = new File(localPath, localFileName);
OutputStream output = new FileOutputStream(localFile);
ftpClient.retrieveFile(remoteFileName, output);
output.close();
InputStream input = new FileInputStream(localFile);
关于原因这里有比较具体的分析:http://marc.info/?l=jakarta-commons-user&m=110443645016720&w=2
简单来说:completePendingCommand()会一直在等FTP Server返回226 Transfer complete,但是FTP Server只有在接受到InputStream执行close方法时,才会返回。所以先要执行close方法
方法时需要特别注意,在调用这个接口后,一定要手动close掉返回的InputStream,然后再调用completePendingCommand方法,若不是按照这个顺序,则不对,伪代码:
InputStream is = ftpClient.retrieveFileStream(remote);
is.close();
ftpClient.completePendingCommand();
retrieveFileStream的API文档说的有点罗嗦,还可以使用下列方法来替换上述使用方式
使用一个中间文件来做一个转接,这种方式比上述方法的好处就是自己容易控制,不容易出问题。伪代码如下:
File localFile = new File(localPath, localFileName);
OutputStream output = new FileOutputStream(localFile);
ftpClient.retrieveFile(remoteFileName, output);
output.close();
InputStream input = new FileInputStream(localFile);
关于原因这里有比较具体的分析:http://marc.info/?l=jakarta-commons-user&m=110443645016720&w=2
简单来说:completePendingCommand()会一直在等FTP Server返回226 Transfer complete,但是FTP Server只有在接受到InputStream执行close方法时,才会返回。所以先要执行close方法
相关文章推荐
- PHP中$_SERVER的详细参数与说明
- 最新抓取淘宝价格数据,图片标题的就更好抓了整合就好
- ACM1089:A+B for Input-Output Practice (I)
- php spl之迭代器iterator
- 简单分页原理 php
- php-fpm参数优化
- Yii 读写分离 分表分库
- php学习笔记_函数类型
- PHP格式化导出EXCEL 【数值型字符串显示问题】
- php标准库spl之链表,堆栈,队列
- PHP实现合并discuz用户
- php xml操作
- php中防盗链使用.htaccess
- php 字符串截取函数
- FTP 上传 下载
- PHP iconv 解决utf-8和gb2312编码转换问题
- PHP 函数
- laravel 安装及入门
- 举例详解PHP脚本的测试方法
- 提高FTP传输速度小技巧