基于SpringBoot的Ftp连接池终极实现1、配置⽂件
package com.properties;
import lombok.Getter;
import lombok.Setter;
import org.t.properties.ConfigurationProperties;
/**
* @author liuchao
insiston
* @date 2020-03-28
*/
@Getter
@Setter
@ConfigurationProperties(prefix = FtpProperties.PREFIX, ignoreUnknownFields = fal)
5353>rdg濒危物种少女漫画public class FtpProperties {
public static final String PREFIX = "faea.ftp";
/**
* Ip
*/
private String host;
/**
* 端⼝
*/
private Integer port;
/
**
* 登录账号
*/
private String name;
/**www google con
* 登录密码
*/
private String password;
/**
* 访问前缀
*/
private String urlPrefix;
/**
* 是否被动模式
*/
private boolean passiveMode = fal;
/**
* 编码格式
*/
private String encoding = "UTF-8";
/**
* 连接超时时间
*/
private int connectTimeout = 30000;
/**
* 缓存
*/
private int bufferSize = 8096;
}
2、基于Apache commons-pool2 对象池实现ftpClient 池⼯⼚package com.ftp;
import util.StrUtil;
import com.xception.FaeaBusinessException;
import com.properties.FtpProperties;
slf4j.Slf4j;
import s.ftp.FTPClient;
import s.ftp.FTPReply;
import s.pool2.BaPooledObjectFactory;
import s.pool2.PooledObject;
import s.pool2.impl.DefaultPooledObject;
import java.io.IOException;
/**
* Ftp客户端⼯⼚
*
* @author liuchao
* @date 2020/6/28
*/
@Slf4j
public class FtpClientFactory extends BaPooledObjectFactory<FTPClient> {
private FtpProperties ftpProperties;
public FtpClientFactory(FtpProperties ftpProperties) {
this.ftpProperties = ftpProperties;
}
@Override
public FTPClient create() {
非主流英文歌曲
final FTPClient ftpClient = new FTPClient();
ftpClient.Encoding());
ftpClient.ConnectTimeout());
try {
// 连接ftp服务器
// 登录ftp服务器
ftpClient.Name(), Password());
} catch (IOException e) {
throw new FaeaBusinessException("创建ftp连接失败", e);
}
// 是否成功登录服务器
final int replyCode = ReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
try {
ftpClient.disconnect();
} catch (IOException e) {
// ignore
}
throw new FaeaBusinessException(StrUtil.format("Login failed for ur [{}], reply code is: [{}]", Name(), replyCode));
}
ftpClient.BufferSize());broadcasting
//设置模式
if (ftpProperties.isPassiveMode()) {
}
return ftpClient;
}
@Override
public PooledObject<FTPClient> wrap(FTPClient obj) {
return new DefaultPooledObject<>(obj);
return new DefaultPooledObject<>(obj);
}
/**
缝隙英文
* 销毁FtpClient对象
*/
@Override
public void destroyObject(PooledObject<FTPClient> ftpPooled) {
if (ftpPooled == null) {
return;
}
FTPClient ftpClient = Object();
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
}
} catch (IOException io) {
<("ftp client {}", io);
} finally {
try {
ftpClient.disconnect();
} catch (IOException io) {
<("clo ftp {}", io);
}
}
}hand hold
/
**
* 验证FtpClient对象是否还可⽤
*/
@Override
public boolean validateObject(PooledObject<FTPClient> ftpPooled) { try {
FTPClient ftpClient = Object();
return ftpClient.ndNoOp();
} catch (IOException e) {
<("Failed to validate client: {}", e);
}
return fal;
}
}
3、ftpClient 池
package com.ftp;
slf4j.Slf4j;
import s.ftp.FTPClient;meanshift
import s.pool2.BaObjectPool;
import org.springframework.util.ObjectUtils;
import java.io.IOException;
import urrent.ArrayBlockingQueue;
import urrent.BlockingQueue;
import urrent.TimeUnit;
/**
* ftp client 池
*
* @author liuchao
* @date 2020/6/28
*/
*/
@Slf4j
public class FtpClientPool extends BaObjectPool<FTPClient> {
private static final int DEFAULT_POOL_SIZE = 4;
private final BlockingQueue<FTPClient> ftpBlockingQueue;
private final FtpClientFactory ftpClientFactory;
/**
* 初始化连接池,需要注⼊⼀个⼯⼚来提供FTPClient实例
*
* @param ftpClientFactory ftp⼯⼚
* @throws Exception
*/
public FtpClientPool(FtpClientFactory ftpClientFactory) throws Exception {
this(DEFAULT_POOL_SIZE, ftpClientFactory);
}
public FtpClientPool(int poolSize, FtpClientFactory factory) throws Exception {
this.ftpClientFactory = factory;
ftpBlockingQueue = new ArrayBlockingQueue<>(poolSize);
initPool(poolSize);
}
/**
* 初始化连接池,需要注⼊⼀个⼯⼚来提供FTPClient实例
*
* @param maxPoolSize 最⼤连接数
* @throws Exception
*/
private void initPool(int maxPoolSize) throws Exception {
for (int i = 0; i < maxPoolSize; i++) {
// 往池中添加对象
addObject();
}
}
/**
* 获取连接
*
* @return
* @throws Exception
*/
@Override
public FTPClient borrowObject() throws Exception {
FTPClient client = ftpBlockingQueue.take();
if (ObjectUtils.isEmpty(client)) {
client = ate();
// 放⼊连接池
returnObject(client);
// 验证对象是否有效这⾥通过实践验证如果长时间不校验是否存活,则这⾥会报通道已断开等错误 } el if (!ftpClientFactory.validateObject(ftpClientFactory.wrap(client))) {
// 对⽆效的对象进⾏处理
invalidateObject(client);
// 创建新的对象
client = ate();
// 将新的对象放⼊连接池
returnObject(client);
}
return client;
}
@Override
public void returnObject(FTPClient client) {
try {
if (client != null && !ftpBlockingQueue.offer(client, 3, TimeUnit.SECONDS)) {
if (client != null && !ftpBlockingQueue.offer(client, 3, TimeUnit.SECONDS)) {
ftpClientFactory.destroyObject(ftpClientFactory.wrap(client));
}
} catch (InterruptedException e) {
<("return ftp client interrupted ...{}", e);
}
}
@Override
public void invalidateObject(FTPClient client) {
try {
if (client.isConnected()) {
client.logout();
}
} catch (IOException io) {
<("ftp client {}", io);
} finally {
try {
client.disconnect();
} catch (IOException io) {
<("clo ftp {}", io);
}
}
}
/**
* 增加⼀个新的链接,超时失效
*/
@Override
public void addObject() throws Exception {
// 插⼊对象到队列
ftpBlockingQueue.ate(), 3, TimeUnit.SECONDS);
}
/**
* 关闭连接池
*/
@Override
public void clo() {
try {
while (ftpBlockingQueue.iterator().hasNext()) {
FTPClient client = ftpBlockingQueue.take();
ftpClientFactory.destroyObject(ftpClientFactory.wrap(client));
}
} catch (Exception e) {
<("clo ftp client {}", e);
}
}
public BlockingQueue<FTPClient> getFtpBlockingQueue() {
return ftpBlockingQueue;pickup是什么意思
}
}
4、基于Hutool ftp⼯具类源码实现接⼊ftp连接池抽象类package com.ftp;
import ollection.CollUtil;
import io.FileUtil;
import util.StrUtil;
import java.io.File;