FTPClientFactory.java
3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.viontech.ftp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import java.io.IOException;
/**
* ftpclient 工厂
*/
@Slf4j
public class FTPClientFactory extends BasePooledObjectFactory<FTPClient> {
private FtpPoolConfig ftpPoolConfig;
public FtpPoolConfig getFtpPoolConfig() {
return ftpPoolConfig;
}
public void setFtpPoolConfig(FtpPoolConfig ftpPoolConfig) {
this.ftpPoolConfig = ftpPoolConfig;
}
/**
* 新建对象
*/
@Override
public FTPClient create() throws Exception {
FTPClient ftpClient = new FTPClient();
ftpClient.setConnectTimeout(ftpPoolConfig.getConnectTimeOut());
try {
log.info("连接ftp服务器:" + ftpPoolConfig.getHost() + ":" + ftpPoolConfig.getPort());
ftpClient.connect(ftpPoolConfig.getHost(), ftpPoolConfig.getPort());
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
log.error("FTPServer 拒绝连接");
return null;
}
boolean result = ftpClient.login(ftpPoolConfig.getUsername(), ftpPoolConfig.getPassword());
if (!result) {
throw new Exception("ftpClient登录失败! userName:" + ftpPoolConfig.getUsername() + ", password:"
+ ftpPoolConfig.getPassword());
}
ftpClient.setControlEncoding(ftpPoolConfig.getControlEncoding());
ftpClient.setBufferSize(ftpPoolConfig.getBufferSize());
ftpClient.setFileType(ftpPoolConfig.getFileType());
ftpClient.setDataTimeout(ftpPoolConfig.getDataTimeout());
if (ftpPoolConfig.isPassiveMode()) {
log.info("进入ftp被动模式");
ftpClient.enterLocalPassiveMode();//进入被动模式
}
} catch (IOException e) {
log.error("FTP连接失败:", e);
}
return ftpClient;
}
@Override
public PooledObject<FTPClient> wrap(FTPClient ftpClient) {
return new DefaultPooledObject<FTPClient>(ftpClient);
}
/**
* 销毁对象
*/
@Override
public void destroyObject(PooledObject<FTPClient> p) throws Exception {
FTPClient ftpClient = p.getObject();
try {
if (ftpClient != null && ftpClient.isConnected()) {
ftpClient.logout();
}
} catch (IOException io) {
io.printStackTrace();
} finally {
// 注意,一定要在finally代码中断开连接,否则会导致占用ftp连接情况
try {
ftpClient.disconnect();
} catch (IOException io) {
io.printStackTrace();
}
}
}
/**
* 验证对象
*/
@Override
public boolean validateObject(PooledObject<FTPClient> p) {
FTPClient ftpClient = p.getObject();
boolean connect = false;
try {
connect = ftpClient.sendNoOp();
} catch (IOException e) {
e.printStackTrace();
}
return connect;
}
/**
* No-op.
*
* @param p ignored
*/
@Override
public void activateObject(PooledObject<FTPClient> p) throws Exception {
FTPClient ftpClient = p.getObject();
if (!validateObject(p)) {
ftpClient.reinitialize();
}
if (!validateObject(p)) {
throw new RuntimeException("ftp已经失效");
}
}
/**
* No-op.
*
* @param p ignored
*/
@Override
public void passivateObject(PooledObject<FTPClient> p) throws Exception {
}
}