import com.enterprisedt.net.ftp.FTPConnectMode;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import zzvcom.cms.ccm.commons.JavaXmlTookit;
import zzvcom.cms.ccm.commons.SysParamsTookit;
/** * FTP配置 * * @author leizhimin 2008-12-5 22:49:39 */ public class FtpServerConfigration {
private static final Log log = LogFactory.getLog(FtpServerConfigration.
class);
private String username;
//用户名 private String password;
//密码 private String ip;
//ip private Integer port;
//端口 private Integer timeout;
//超时时间 private Integer buffersize;
//缓存大小 private Integer notifytime;
//通知时间 private String connectMode;
//连接模式 private String encoding;
//编码方式 public FtpServerConfigration(String username, String password, String ip, Integer port) {
this.username = username;
this.password = password;
this.ip = ip;
this.port = port;
this.timeout = Integer.valueOf(SysParamsTookit.getProperty(
"timeout",
"36000000"));
this.buffersize = Integer.valueOf(SysParamsTookit.getProperty(
"buffersize",
"2048000"));
this.notifytime = Integer.valueOf(SysParamsTookit.getProperty(
"notifytime",
"5000"));
this.connectMode = SysParamsTookit.getProperty(
"connectMode",
"PASV");
this.encoding = SysParamsTookit.getProperty(
"encoding",
"GBK");
}
public FtpServerConfigration(String ftpConfigXml) {
FtpServerConfigration config = (FtpServerConfigration) JavaXmlTookit.xml2Java(ftpConfigXml, FtpServerConfigration.
class);
if (StringUtils.isBlank(config.getUsername())
|| StringUtils.isBlank(config.getPassword())
|| StringUtils.isBlank(config.getIp())
|| config.getPort() ==
null) {
log.error(
"FTP最基本的配置属性(username、password、ip、port)不能为空,请检查!");
}
else {
this.username = config.getUsername();
this.password = config.getPassword();
this.ip = config.getIp();
this.port = config.getPort();
}
if (config.getTimeout() ==
null)
this.timeout = Integer.valueOf(SysParamsTookit.getProperty(
"timeout",
"36000000"));
if (config.getBuffersize() ==
null)
this.buffersize = Integer.valueOf(SysParamsTookit.getProperty(
"buffersize",
"2048000"));
if (config.getNotifytime() ==
null)
this.notifytime = Integer.valueOf(SysParamsTookit.getProperty(
"notifytime",
"5000"));
if (StringUtils.isBlank(config.getConnectMode()))
this.connectMode = SysParamsTookit.getProperty(
"connectMode",
"PASV");
if (StringUtils.isBlank(config.getEncoding()))
this.encoding = SysParamsTookit.getProperty(
"encoding",
"GBK");
}
/** * 获取当前FTP连接配置 * * @return 当前FTP连接配置 */ public FtpServerConfigration getConfigration() {
return this;
}
/** * 构建FTP客户端连接,并进行连接 * * @return FTP客户端连接 * @throws Exception 当构建客户端失败时抛出 */ public UltraFTPClient buildFtpClient()
throws Exception {
UltraFTPClient client =
new UltraFTPClient();
try {
client.setUserName(username);
client.setPassword(password);
client.setRemoteHost(ip);
client.setRemotePort(port);
client.setTimeout(timeout);
client.getAdvancedSettings().setTransferBufferSize(buffersize);
client.getAdvancedSettings().setTransferNotifyInterval(notifytime);
client.getAdvancedSettings().setControlEncoding(encoding);
// client.setEventListener(
new UploadListener(client));
//设置事件监听器 if (connectMode.equalsIgnoreCase(
"ACTIVE")) {
client.getAdvancedFTPSettings().setConnectMode(FTPConnectMode.ACTIVE);
//设置为被动模式 }
else if (connectMode.equalsIgnoreCase(
"PASV")) {
client.getAdvancedFTPSettings().setConnectMode(FTPConnectMode.PASV);
//设置为被动模式 }
else {
log.error(
"标识为" + connectMode +
"的FTP连接模式配置错误,连接模式仅有两种ACTIVE和PASV,请检查!");
}
client.connect();
log.info(
"FTP连接成功!详细信息(远程主机:" + ip +
",用户名:" + username +
")");
}
catch (Exception e) {
log.info(
"FTP创建连接发生异常!", e);
throw e;
}
return client;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public Integer getTimeout() {
return timeout;
}
public void setTimeout(Integer timeout) {
this.timeout = timeout;
}
public Integer getBuffersize() {
return buffersize;
}
public void setBuffersize(Integer buffersize) {
this.buffersize = buffersize;
}
public Integer getNotifytime() {
return notifytime;
}
public void setNotifytime(Integer notifytime) {
this.notifytime = notifytime;
}
public String getConnectMode() {
return connectMode;
}
public void setConnectMode(String connectMode) {
this.connectMode = connectMode;
}
public String getEncoding() {
return encoding;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
}