/**
* 设置FTP客服端的配置
*
* @return
*/
private FTPClientConfig getFtpConfig() {
FTPClientConfig ftpConfig = new FTPClientConfig(
FTPClientConfig.SYST_UNIX);
ftpConfig.setServerLanguageCode(FTP.DEFAULT_CONTROL_ENCODING);
return ftpConfig;
}
/**
* 连接到服务器
*
* @param ip
* @param user
* @param pwd
* @param defaultPort
* @return true 连接服务器成功,false 连接服务器失败
* @throws Exception
*/
public boolean connectServer(String ip, String user, String pwd,
int defaultPort) throws Exception {
boolean flag = true;
int reply;
try {
FTPClient ftpClient = new FTPClient();
ftpClient.setControlEncoding("GBK");
ftpClient.setDefaultPort(defaultPort);
ftpClient.configure(getFtpConfig());
ftpClient.connect(ip);
ftpClient.login(user, pwd);
reply = ftpClient.getReplyCode();
ftpClient.setDataTimeout(120000);
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
logger.debug("FTP 服务拒绝连接!");
flag = false;
}
} catch (SocketException e) {
flag = false;
logger.debug("登录ftp服务器【" + serverIp + "】失败,连接超时!");
throw e;
} catch (IOException e) {
flag = false;
logger.debug("登录ftp服务器【" + serverIp + "】失败,FTP服务器无法打开!");
throw e;
}
return flag;
}