[JAVA]Apache FTPClient操作“卡死”问题的分析和解决
最近在和一个第三方的合作中不得已需要使用FTP文件接口。由于FTP Server由对方提供,而且双方背后各自的网络环境都很不单纯等等原因,造成测试环境无法模拟实际情况。测试环境中程序一切正常,但是在部署到生产环境之后发现FTP操作不规律性出现“卡死”现象:程序捕获不到任何异常一直卡着,导致轮巡无法正常工作(由于担心在轮巡时间间隔内处理不能完成,我没有采用类似quartz或者crontab的定时任务,而是采用while-true然后sleep的方式)。
为了解决这个问题,我首先考虑的是对于FTPClient的使用上没有设置超时时间,于是设置了ConnectTimeout、DataTimeout、DefaultTimeout后在生产环境上继续观察,但是问题依旧没有解决。后来我有些怀疑FTPClient api本身是不是有什么问题,想实在不行自己实现一个超时机制吧,不过还是不甘心,还是想从FTPClient api本身去解决问题。又经过一翻研究之后发现:需要使用被动模式,以下摘抄别人的一段简单描述:
在项目中使用commons-net-3.0.1.jar实现FTP文件的下载,在windows xp上运行正常,但是放到linux上,却出现问题,程序运行到 FTPClient.listFiles()或者FTPClient.retrieveFile()方法时,就停止在那里,什么反应都没有,出现假死状态。google一把,发现很多人也出现了此类问题,最终在一个帖子里找到了解决办法。在调用这两个方法之前,调用FTPClient.enterLocalPassiveMode();这个方法的意思就是每次数据连接之前,ftp client告诉ftp server开通一个端口来传输数据。为什么要这样做呢,因为ftp server可能每次开启不同的端口来传输数据,但是在linux上,由于安全限制,可能某些端口没有开启,所以就出现阻塞。OK,问题解决。
于是我回滚了之前的修改,改为被动模式(关于FTP主动/被动模式的解释,这里我不多说了,关注的朋友可以自己查阅)。但是问题依旧。于是能想到的就是最有的绝招:实在不行自己实现一个超时机制吧。经过一翻研究最简单的方式就是使用:Future解决:
public static void main(String[] args) throws InterruptedException, ExecutionException {
final ExecutorService exec = Executors.newFixedThreadPool(1); Callable<String> call = new Callable<String>() {
public String call() throws Exception {
Thread.sleep(1000 * 5);
return "线程执行完成.";
}
}; try {
Future<String> future = exec.submit(call);
String obj = future.get(4 * 1000, TimeUnit.MILLISECONDS); // 任务处理超时时间设置
System.out.println("任务成功返回:" + obj);
} catch (TimeoutException ex) {
System.out.println("处理超时啦....");
ex.printStackTrace();
} catch (Exception e) {
System.out.println("处理失败.");
e.printStackTrace();
}
// 关闭线程池
exec.shutdown(); System.out.println("完毕");
}
当然了还有很多其他方式:
http://tech.sina.com.cn/s/2008-07-04/1051720260.shtml
http://itindex.net/blog/2010/08/11/1281486125717.html
http://darkmasky.iteye.com/blog/1115047
http://www.cnblogs.com/wasp520/archive/2012/07/06/2580101.html
http://coolxing.iteye.com/blog/1476289
http://www.cnblogs.com/chenying99/archive/2012/10/24/2737924.html
虽然找到了终极的“必杀技”,但是此时我还是不甘心,还是想从FTPClient api本身去解决问题,但此时看来也别无它他法。只能试试:即设置被动模式又设置超时时间。经过实际测试,发现问题得以解决。下面把我的FTP工具类贴给大家分享,希望能帮到遇到同样问题的人。
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List; public class FtpUtil {
public static final String ANONYMOUS_LOGIN = "anonymous";
private FTPClient ftp;
private boolean is_connected; public FtpUtil() {
ftp = new FTPClient();
is_connected = false;
} public FtpUtil(int defaultTimeoutSecond, int connectTimeoutSecond, int dataTimeoutSecond){
ftp = new FTPClient();
is_connected = false; ftp.setDefaultTimeout(defaultTimeoutSecond * 1000);
ftp.setConnectTimeout(connectTimeoutSecond * 1000);
ftp.setDataTimeout(dataTimeoutSecond * 1000);
} /**
* Connects to FTP server.
*
* @param host
* FTP server address or name
* @param port
* FTP server port
* @param user
* user name
* @param password
* user password
* @param isTextMode
* text / binary mode switch
* @throws IOException
* on I/O errors
*/
public void connect(String host, int port, String user, String password, boolean isTextMode) throws IOException {
// Connect to server.
try {
ftp.connect(host, port);
} catch (UnknownHostException ex) {
throw new IOException("Can't find FTP server '" + host + "'");
} // Check rsponse after connection attempt.
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
disconnect();
throw new IOException("Can't connect to server '" + host + "'");
} if (user == "") {
user = ANONYMOUS_LOGIN;
} // Login.
if (!ftp.login(user, password)) {
is_connected = false;
disconnect();
throw new IOException("Can't login to server '" + host + "'");
} else {
is_connected = true;
} // Set data transfer mode.
if (isTextMode) {
ftp.setFileType(FTP.ASCII_FILE_TYPE);
} else {
ftp.setFileType(FTP.BINARY_FILE_TYPE);
}
} /**
* Uploads the file to the FTP server.
*
* @param ftpFileName
* server file name (with absolute path)
* @param localFile
* local file to upload
* @throws IOException
* on I/O errors
*/
public void upload(String ftpFileName, File localFile) throws IOException {
// File check.
if (!localFile.exists()) {
throw new IOException("Can't upload '" + localFile.getAbsolutePath() + "'. This file doesn't exist.");
} // Upload.
InputStream in = null;
try { // Use passive mode to pass firewalls.
ftp.enterLocalPassiveMode(); in = new BufferedInputStream(new FileInputStream(localFile));
if (!ftp.storeFile(ftpFileName, in)) {
throw new IOException("Can't upload file '" + ftpFileName + "' to FTP server. Check FTP permissions and path.");
} } finally {
try {
in.close();
} catch (IOException ex) {
}
}
} /**
* Downloads the file from the FTP server.
*
* @param ftpFileName
* server file name (with absolute path)
* @param localFile
* local file to download into
* @throws IOException
* on I/O errors
*/
public void download(String ftpFileName, File localFile) throws IOException {
// Download.
OutputStream out = null;
try {
// Use passive mode to pass firewalls.
ftp.enterLocalPassiveMode(); // Get file info.
FTPFile[] fileInfoArray = ftp.listFiles(ftpFileName);
if (fileInfoArray == null) {
throw new FileNotFoundException("File " + ftpFileName + " was not found on FTP server.");
} // Check file size.
FTPFile fileInfo = fileInfoArray[0];
long size = fileInfo.getSize();
if (size > Integer.MAX_VALUE) {
throw new IOException("File " + ftpFileName + " is too large.");
} // Download file.
out = new BufferedOutputStream(new FileOutputStream(localFile));
if (!ftp.retrieveFile(ftpFileName, out)) {
throw new IOException("Error loading file " + ftpFileName + " from FTP server. Check FTP permissions and path.");
} out.flush();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException ex) {
}
}
}
} /**
* Removes the file from the FTP server.
*
* @param ftpFileName
* server file name (with absolute path)
* @throws IOException
* on I/O errors
*/
public void remove(String ftpFileName) throws IOException {
if (!ftp.deleteFile(ftpFileName)) {
throw new IOException("Can't remove file '" + ftpFileName + "' from FTP server.");
}
} /**
* Lists the files in the given FTP directory.
*
* @param filePath
* absolute path on the server
* @return files relative names list
* @throws IOException
* on I/O errors
*/
public List<String> list(String filePath) throws IOException {
List<String> fileList = new ArrayList<String>(); // Use passive mode to pass firewalls.
ftp.enterLocalPassiveMode(); FTPFile[] ftpFiles = ftp.listFiles(filePath);
int size = (ftpFiles == null) ? 0 : ftpFiles.length;
for (int i = 0; i < size; i++) {
FTPFile ftpFile = ftpFiles[i];
if (ftpFile.isFile()) {
fileList.add(ftpFile.getName());
}
} return fileList;
} /**
* Sends an FTP Server site specific command
*
* @param args
* site command arguments
* @throws IOException
* on I/O errors
*/
public void sendSiteCommand(String args) throws IOException {
if (ftp.isConnected()) {
try {
ftp.sendSiteCommand(args);
} catch (IOException ex) {
}
}
} /**
* Disconnects from the FTP server
*
* @throws IOException
* on I/O errors
*/
public void disconnect() throws IOException { if (ftp.isConnected()) {
try {
ftp.logout();
ftp.disconnect();
is_connected = false;
} catch (IOException ex) {
}
}
} /**
* Makes the full name of the file on the FTP server by joining its path and
* the local file name.
*
* @param ftpPath
* file path on the server
* @param localFile
* local file
* @return full name of the file on the FTP server
*/
public String makeFTPFileName(String ftpPath, File localFile) {
if (ftpPath == "") {
return localFile.getName();
} else {
String path = ftpPath.trim();
if (path.charAt(path.length() - 1) != '/') {
path = path + "/";
} return path + localFile.getName();
}
} /**
* Test coonection to ftp server
*
* @return true, if connected
*/
public boolean isConnected() {
return is_connected;
} /**
* Get current directory on ftp server
*
* @return current directory
*/
public String getWorkingDirectory() {
if (!is_connected) {
return "";
} try {
return ftp.printWorkingDirectory();
} catch (IOException e) {
} return "";
} /**
* Set working directory on ftp server
*
* @param dir
* new working directory
* @return true, if working directory changed
*/
public boolean setWorkingDirectory(String dir) {
if (!is_connected) {
return false;
} try {
return ftp.changeWorkingDirectory(dir);
} catch (IOException e) {
} return false;
} /**
* Change working directory on ftp server to parent directory
*
* @return true, if working directory changed
*/
public boolean setParentDirectory() {
if (!is_connected) {
return false;
} try {
return ftp.changeToParentDirectory();
} catch (IOException e) {
} return false;
} /**
* Get parent directory name on ftp server
*
* @return parent directory
*/
public String getParentDirectory() {
if (!is_connected) {
return "";
} String w = getWorkingDirectory();
setParentDirectory();
String p = getWorkingDirectory();
setWorkingDirectory(w); return p;
} /**
* Get directory contents on ftp server
*
* @param filePath
* directory
* @return list of FTPFileInfo structures
* @throws IOException
*/
public List<FfpFileInfo> listFiles(String filePath) throws IOException {
List<FfpFileInfo> fileList = new ArrayList<FfpFileInfo>(); // Use passive mode to pass firewalls.
ftp.enterLocalPassiveMode();
FTPFile[] ftpFiles = ftp.listFiles(filePath);
int size = (ftpFiles == null) ? 0 : ftpFiles.length;
for (int i = 0; i < size; i++) {
FTPFile ftpFile = ftpFiles[i];
FfpFileInfo fi = new FfpFileInfo();
fi.setName(ftpFile.getName());
fi.setSize(ftpFile.getSize());
fi.setTimestamp(ftpFile.getTimestamp());
fi.setType(ftpFile.isDirectory());
fileList.add(fi);
} return fileList;
} /**
* Get file from ftp server into given output stream
*
* @param ftpFileName
* file name on ftp server
* @param out
* OutputStream
* @throws IOException
*/
public void getFile(String ftpFileName, OutputStream out) throws IOException {
try {
// Use passive mode to pass firewalls.
ftp.enterLocalPassiveMode(); // Get file info.
FTPFile[] fileInfoArray = ftp.listFiles(ftpFileName);
if (fileInfoArray == null) {
throw new FileNotFoundException("File '" + ftpFileName + "' was not found on FTP server.");
} // Check file size.
FTPFile fileInfo = fileInfoArray[0];
long size = fileInfo.getSize();
if (size > Integer.MAX_VALUE) {
throw new IOException("File '" + ftpFileName + "' is too large.");
} // Download file.
if (!ftp.retrieveFile(ftpFileName, out)) {
throw new IOException("Error loading file '" + ftpFileName + "' from FTP server. Check FTP permissions and path.");
} out.flush(); } finally {
if (out != null) {
try {
out.close();
} catch (IOException ex) {
}
}
}
} /**
* Put file on ftp server from given input stream
*
* @param ftpFileName
* file name on ftp server
* @param in
* InputStream
* @throws IOException
*/
public void putFile(String ftpFileName, InputStream in) throws IOException {
try {
// Use passive mode to pass firewalls.
ftp.enterLocalPassiveMode(); if (!ftp.storeFile(ftpFileName, in)) {
throw new IOException("Can't upload file '" + ftpFileName + "' to FTP server. Check FTP permissions and path.");
}
} finally {
try {
in.close();
} catch (IOException ex) {
}
}
}
}
[JAVA]Apache FTPClient操作“卡死”问题的分析和解决的更多相关文章
- 关于[JAVA] Apache FTPClient.listFiles()操作“卡死”问题的分析和解决
项目中使用commons-net-3.0.1.jar实现FTP文件的下载,程序运行到 FTPClient.listFiles()或者FTPClient.retrieveFile()方法时,就停止在那里 ...
- Windows服务器java.exe占用CPU过高问题分析及解决
最近在测试一个用java语言实现的数据采集接口时发现,接口一旦运行起来,CPU利用率瞬间飙升到85%-95%,一旦停止就恢复到40%以下,这让我不得不面对以前从未关注过的程序性能问题. 在硬着头皮查找 ...
- aliyun oss 文件上传 java.net.SocketTimeoutException Read timed out 问题分析及解决
upload ClientException Read timed out com.aliyun.openservices.ClientException: Read timed out ...
- java和javascript日期校验和闰年问题分析和解决方式
1.闰年的介绍 地球绕太阳执行周期为365天5小时48分46秒(合365.24219天)即一回归年.公历的平年仅仅有365日,比回归年短约0.2422 日,所余下的时间约为四年累计一天.故四年于2月加 ...
- Java EE中的容器和注入分析,历史与未来
Java EE中的容器和注入分析,历史与未来 java中的容器 java中的注入 容器和注入的历史和展望 一.java中的容器 java EE中的注入,使我们定义的对象能够获取对资源和其他依赖项的引用 ...
- 浏览器与服务器交互原理以及用java模拟浏览器操作v
浏览器应用服务器JavaPHPApache * 1,在HTTP的WEB应用中, 应用客户端和服务器之间的状态是通过Session来维持的, 而Session的本质就是Cookie, * 简单的讲,当浏 ...
- Java安全之Shiro 550反序列化漏洞分析
Java安全之Shiro 550反序列化漏洞分析 首发自安全客:Java安全之Shiro 550反序列化漏洞分析 0x00 前言 在近些时间基本都能在一些渗透或者是攻防演练中看到Shiro的身影,也是 ...
- Java Spring mvc 操作 Redis 及 Redis 集群
本文原创,转载请注明:http://www.cnblogs.com/fengzheng/p/5941953.html 关于 Redis 集群搭建可以参考我的另一篇文章 Redis集群搭建与简单使用 R ...
- Java中创建操作文件和文件夹的工具类
Java中创建操作文件和文件夹的工具类 FileUtils.java import java.io.BufferedInputStream; import java.io.BufferedOutput ...
随机推荐
- Python 操作 Excel 、txt等文件
#xlrd 读取excel import xlrd import os #获取文件路径 filepath = os.path.join(os.getcwd(),'user_info') #获取文件名称 ...
- Java泛型(泛型接口、泛型类、泛型方法)
转载 转载出处:https://www.cnblogs.com/JokerShi/p/8117556.html 泛型接口: 定义一个泛型接口: 通过类去实现这个泛型接口的时候指定泛型T的具体类型. 指 ...
- Mysql企业实战
==========================业务垂直分割:1>介绍说明: 随着公司的业务规模扩展,DBA需要根据企业数据业务进行切割,垂直切割又称为纵向切割,垂直数据切割是根据企业网站业 ...
- github注册流程
- C#Zxing.net生成条形码和二维码
下载Zxing.net官网:https://archive.codeplex.com/?p=zxingnet 或者去VS程序包下载 封装好的代码: using System; using System ...
- Windows7中7种不同关机模式介绍
在Win7关机选项中一共有7种关闭方式,分别为 Switch user(切换用户), Log off(登出), Lock(锁定), Restart(重启), Sleep(睡眠), Hibernate( ...
- WPF 控件库——仿制Windows10的进度条
WPF 控件库系列博文地址: WPF 控件库——仿制Chrome的ColorPicker WPF 控件库——仿制Windows10的进度条 WPF 控件库——轮播控件 WPF 控件库——带有惯性的Sc ...
- c# Quartz.net的简单封装
分享一个以前封装的Quartz.net类. 新建一个QuartzClass类库项目.nuget控制台输入 image.png 添加Quartz.net的引用. 我们新建一个JobBase.cs文件,里 ...
- 【QTP专题-优化】VBS脚本启动QTP并运行测试
使用vbs脚本启动QTP并运行测试,startQTP.vbs '******************************************************************** ...
- 4.Python的版本
Python2: 英文支持没问题,中文报错,默认编码:ascii码 显示中午需要添加代码在首行:# -*- encoding:utf -8 -*- 用户交互 raw_input python2 里还 ...