Commons-net FTPClient completePendingCommand()经常使程序死掉的原因分析以及解决方式
2008-11-28 18:53
435 查看
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方法
相关文章推荐
- Commons-net FTPClient completePendingCommand()经常使程序死掉的原因分析以及解决方式
- FTPClient 处理多个文件时注意添加completePendingCommand
- org.apache.commons.net.ftp.FTPClient上传文件大小改变的解决方法
- linux 上使用commons-net.jar中 FTPClient类的listFiles、retrieveFile等方法停止不执行,出现假死状态解决办法
- OpenSSL.Net 在生产环境中无法正常加载的原因分析与解决方式。
- 安装SQL Server 2012过程中出现“启用windows功能NetFx3时出错”(错误原因、详细分析及解决方法)以及在Windows Server2012上安装.NET Framework 3
- linux 上使用commons-net.jar中 FTPClient类的listFiles、retrieveFile等方法停止不执行,出现假死状态解决办法
- 安装SQL Server 2012过程中出现“启用windows功能NetFx3时出错”(错误原因、详细分析及解决方法)以及在Windows Server2012上安装.NET Framework 3
- apache FTPClient replyCode 421,FTPConnectionClose 问题原因以及解决
- 多线程-线程安全问题的产生原因分析以及同步代码块的方式解决线程安全问题
- 安装SQL Server 2012过程中出现“启用windows功能NetFx3时出错”(错误原因、详细分析及解决方法)以及在Windows Server2012上安装.NET Framework 3.5的详细分析及安装过程
- apache.commons.net.ftp.FTPClient上传FTP
- web安全扫描问题(常见的)分析以及解决方式
- 【FTP】org.apache.commons.net.ftp.FTPClient实现复杂的上传下载,操作目录,处理编码
- android程序报错“error launching activity com.android.ddmlib.shellcommandunresponsiveexception”的解决方式
- 让Apache的commons-net-3.3中的FTPClient支持中文
- mini2440开发板运行Qt程序出现Segmentation fault的另一种奇葩原因:Ubuntu下使用FileZilla通过FTP方式传输程序
- 【实例说明FTP无法列表的原因及解决方式】
- sun.net.ftp.FtpClient 中文目录乱码问题解决
- 使用sun.net.ftp.FtpClient进行上传功能开发,在jdk1.7上不适用问题的解决