Merge remote-tracking branch 'origin/master'
commit
9cc6641313
@ -0,0 +1,50 @@
|
||||
package com.gccloud.dataroom.core.config.bean;
|
||||
|
||||
import com.gccloud.common.exception.GlobalException;
|
||||
import io.minio.MinioClient;
|
||||
import lombok.Data;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Minio 配置信息
|
||||
*
|
||||
* @author Acechengui
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "gc.starter.file.minio")
|
||||
public class MinioConfig
|
||||
{
|
||||
/**
|
||||
* 服务地址
|
||||
*/
|
||||
private String url;
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String accessKey;
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
private String secretKey;
|
||||
|
||||
/**
|
||||
* 存储桶名称
|
||||
*/
|
||||
private String bucketName;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(prefix = "gc.starter.file", name = "type", havingValue = "minio")
|
||||
public MinioClient getMinioClient() {
|
||||
if (StringUtils.isBlank(bucketName)) {
|
||||
throw new GlobalException("Minio bucketName 不能为空");
|
||||
}
|
||||
return MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build();
|
||||
}
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
package com.gccloud.dataroom.core.module.file.service.pool.ftp;
|
||||
|
||||
import com.gccloud.dataroom.core.config.bean.FtpConfig;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.net.ftp.FTPClient;
|
||||
import org.apache.commons.pool2.impl.GenericObjectPool;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* @author hongyang
|
||||
* @version 1.0
|
||||
* @date 2023/10/18 10:20
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@ConditionalOnProperty(prefix = "gc.starter.file", name = "type", havingValue = "ftp")
|
||||
public class FtpPoolServiceImpl {
|
||||
|
||||
/**
|
||||
* ftp 连接池生成
|
||||
*/
|
||||
private GenericObjectPool<FTPClient> pool;
|
||||
|
||||
/**
|
||||
* ftp 客户端配置文件
|
||||
*/
|
||||
@Resource
|
||||
private FtpConfig config;
|
||||
|
||||
/**
|
||||
* ftp 客户端工厂
|
||||
*/
|
||||
@Resource
|
||||
private FtpClientFactory factory;
|
||||
|
||||
/**
|
||||
* 初始化pool
|
||||
*/
|
||||
@PostConstruct
|
||||
private void initPool() {
|
||||
log.info("初始化FTP连接池");
|
||||
this.pool = new GenericObjectPool<FTPClient>(this.factory, this.config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取ftpClient
|
||||
*/
|
||||
public FTPClient borrowObject() {
|
||||
log.info("获取 FTPClient");
|
||||
if (this.pool != null) {
|
||||
try {
|
||||
return this.pool.borrowObject();
|
||||
} catch (Exception e) {
|
||||
log.error("获取 FTPClient 失败 ", e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 归还 ftpClient
|
||||
*/
|
||||
public void returnObject(FTPClient ftpClient) {
|
||||
if (this.pool == null || ftpClient == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
ftpClient.changeWorkingDirectory("/");
|
||||
} catch (Exception e) {
|
||||
log.error("FTPClient 重置目录失败 ", e);
|
||||
}
|
||||
this.pool.returnObject(ftpClient);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
package com.gccloud.dataroom.core.module.file.service.pool.sftp;
|
||||
|
||||
import com.gccloud.dataroom.core.config.bean.SftpConfig;
|
||||
import com.jcraft.jsch.ChannelSftp;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.pool2.impl.GenericObjectPool;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* Sftp 连接池服务类
|
||||
* @author hongyang
|
||||
* @version 1.0
|
||||
* @date 2023/10/18 15:21
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@ConditionalOnProperty(prefix = "gc.starter.file", name = "type", havingValue = "sftp")
|
||||
public class SftpPoolService {
|
||||
|
||||
/**
|
||||
* ftp 连接池生成
|
||||
*/
|
||||
private GenericObjectPool<ChannelSftp> pool;
|
||||
|
||||
/**
|
||||
* ftp 客户端配置文件
|
||||
*/
|
||||
@Resource
|
||||
private SftpConfig config;
|
||||
|
||||
/**
|
||||
* ftp 客户端工厂
|
||||
*/
|
||||
@Resource
|
||||
private SftpClientFactory factory;
|
||||
|
||||
/**
|
||||
* 初始化pool
|
||||
*/
|
||||
@PostConstruct
|
||||
private void initPool() {
|
||||
log.info("初始化SFTP连接池");
|
||||
this.pool = new GenericObjectPool<ChannelSftp>(this.factory, this.config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取sftp
|
||||
*/
|
||||
public ChannelSftp borrowObject() {
|
||||
if (this.pool != null) {
|
||||
try {
|
||||
return this.pool.borrowObject();
|
||||
} catch (Exception e) {
|
||||
log.error("获取 ChannelSftp 失败", e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 归还 sftp
|
||||
*/
|
||||
public void returnObject(ChannelSftp channelSftp) {
|
||||
if (this.pool != null && channelSftp != null) {
|
||||
this.pool.returnObject(channelSftp);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue