springboot2.1.3配置sftp,自定义sftp连接池(转)

更新时间:2023-05-15 17:37:06 阅读: 评论:0

springboot2.1.3配置sftp,⾃定义sftp连接池(转)springboot2.1.3配置sftp,⾃定义sftp连接池
项⽬地址
maven:
<!-- sftp -->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.56</version>
</dependency>
<!-- commons-pool2 -->
<dependency>
<groupId>s</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.11.1</version>
</dependency>
commons-pool2 配置详解: /p/e9194bd84e38
sftp:
host: rver02 # 服务器ip
port: 22 # ssh端⼝
urname: demofile # ⽤户名
password: demo # 密码
# 连接池参数
pool:
max-total: 10
max-idle: 10
min-idle: 5
or application.properties
sftp.urname = root
sftp.password = root
sftp.host = 192.168.80.51
sftp.port = 2222
sftp.pool.max-total = 10
sftp.pool.max-idle = 10
sftp.pool.min-idle = 3
sftp.poll.maxWaitMillis = 60000
SftpProperties
ample.ftp;
import com.jcraft.jsch.ChannelSftp;
import lombok.Data;
import s.pool2.impl.GenericObjectPoolConfig;
import org.t.properties.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "sftp")
public class SftpProperties {
private String host;
private int port = 22;
private String urname = "root";
private String password = "root";
private Pool pool = new Pool();
public static class Pool extends GenericObjectPoolConfig<ChannelSftp> {
private int maxTotal = DEFAULT_MAX_TOTAL;
private int maxIdle = DEFAULT_MAX_IDLE;
private int minIdle = DEFAULT_MIN_IDLE;
public Pool() {
super();
}
@Override
public int getMaxTotal() {
return maxTotal;
}
@Override
public void tMaxTotal(int maxTotal) {
this.maxTotal = maxTotal;
}
@Override
public int getMaxIdle() {
return maxIdle;
}
@Override
public void tMaxIdle(int maxIdle) {
this.maxIdle = maxIdle;
}
@Override
public int getMinIdle() {
return minIdle;
}
@Override
public void tMinIdle(int minIdle) {
this.minIdle = minIdle;
}
}
}
sftp连接⼯⼚
ample.ftp;
ption.ProjectException;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import lombok.Data;
slf4j.Slf4j;
import s.pool2.BaPooledObjectFactory;
import s.pool2.PooledObject;
import s.pool2.impl.DefaultPooledObject;
import java.util.Properties;
@Data
@Slf4j
public class SftpFactory extends BaPooledObjectFactory<ChannelSftp> {
private SftpProperties properties;
public SftpFactory(SftpProperties properties) {
this.properties = properties;
}
@Override
public ChannelSftp create() {
try {
JSch jsch = new JSch();
negative是什么意思
Session sshSession = Urname(), Host(), Port());            sshSession.Password());
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.tConfig(sshConfig);
ChannelSftp channel = (ChannelSftp) sshSession.openChannel("sftp");
return channel;
} catch (JSchException e) {
throw new ProjectException("连接sfpt失败", e);
}
}
@Override
public PooledObject<ChannelSftp> wrap(ChannelSftp channelSftp) {
return new DefaultPooledObject<>(channelSftp);
}
// 销毁对象
@Override
public void destroyObject(PooledObject<ChannelSftp> p) {
ChannelSftp channelSftp = p.getObject();
channelSftp.disconnect();
}
}
sftp连接池
ample.ftp;
ption.ProjectException;
import com.jcraft.jsch.ChannelSftp;
import lombok.Data;
import s.pool2.impl.GenericObjectPool;
@Data
public class SftpPool {
private GenericObjectPool<ChannelSftp> pool;
public SftpPool(SftpFactory factory) {
this.pool = new GenericObjectPool<>(factory, Properties().getPool());    }
/**
* 获取⼀个sftp连接对象
* @return sftp连接对象
*/
public ChannelSftp borrowObject() {
try {
return pool.borrowObject();
} catch (Exception e) {
throw new ProjectException("获取ftp连接失败", e);
}
}
/**
* 归还⼀个sftp连接对象
* @param channelSftp sftp连接对象
*/
public void returnObject(ChannelSftp channelSftp) {
if (channelSftp!=null) {
}
}
}
sftptemplate 辅助类
import com.fig.SftpPool;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.SftpException;
import s.io.IOUtils;
import org.slf4j.Logger;
rarefactionimport org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Vector;
/**
* @PACKAGE : com.utils
* @Author :  Sea
* @Date : 8/23/21 9:31 AM
public class SFTPTemplate {
private transient Logger log = Class());
//结合springboot
private SftpPool pool;
public SFTPTemplate(SftpPool pool) {
this.pool = pool;
}
/**
* 将输⼊流的数据上传到sftp作为⽂件。⽂件完整路径=baPath+directory
* @param baPath  服务器的基础路径 eg: /upload
* @param directory  上传到该⽬录 eg:  img/  or img/cc/  ⼀定要带上最后的URL,否则返回的url⽆效
* @param sftpFileName  sftp端⽂件名  eg : xx.png
* @param input  输⼊流
*/
public String upload(String baPath,String directory, String sftpFileName, InputStream input) throws Exception {        ChannelSftp sftp = pool.borrowObject();
考研数学一国家线
try{
log.info("start upload file {}",sftpFileName);
mkdir(baPath,directory,sftp);
sftp.put(input, sftpFileName);  //上传⽂件
log.info("end upload file {}",sftpFileName);
} catch (Exception e)
{
throw new Message());
}finally
{
}
return directory+sftpFileName;
}
private void mkdir(String baPath,String directory,ChannelSftp sftp) throws SftpException {
try
{
sftp.cd(baPath);
sftp.cd(directory);
}
catch (SftpException e)
{
log.info("⽬录不存在,创建⽂件夹");
//⽬录不存在,则创建⽂件夹
String [] dirs=directory.split("/");
String tempPath=baPath;
for(String dir:dirs)
{
if(null== dir || "".equals(dir)) continue;
tempPath+="/"+dir;
try{
sftp.cd(tempPath);
}catch(SftpException ex){
sftp.mkdir(tempPath);
sftp.cd(tempPath);catto
}
}
rapping
}
}
/
**
* 下载⽂件。
* @param directory 下载⽬录
* @param downloadFile 下载的⽂件
* @param saveFile 存在本地的路径
*/
public void download(String directory, String downloadFile, String saveFile) throws Exception{
ChannelSftp sftp = pool.borrowObject();
try{
if (directory != null && !"".equals(directory)) {
sftp.cd(directory);
}
File file = new File(saveFile);
<(downloadFile, new FileOutputStream(file));
} catch (Exception e)
{
throw new Message());
} finally
{
/**
* 下载⽂件
* @param directory 下载⽬录
我的歌声里歌词 曲婉婷
* @param downloadFile 下载的⽂件名
* @return 字节数组
*/
public byte[] download(String directory, String downloadFile) throws Exception{        ChannelSftp sftp = pool.borrowObject();
try
{限制性定语从句
if (directory != null && !"".equals(directory)) {
sftp.cd(directory);
}
InputStream is = (downloadFile);
ByteArray(is);
} catch (Exception e)
{
throw new Message());
}finally
{
}
}
/**
* 删除⽂件
* @param directory 要删除⽂件所在⽬录
* @param deleteFile 要删除的⽂件
*/
public void delete(String directory, String deleteFile) throws Exception{
ChannelSftp sftp = pool.borrowObject();
try
{
sftp.cd(directory);
<(deleteFile);
} catch (Exception e)
{
throw new Message());
}finally
{
数量级}
}
英语绕口令带翻译
/**
* 列出⽬录下的⽂件
* @param directory 要列出的⽬录
*/
public Vector<?> listFiles(String directory) throws Exception {
ChannelSftp sftp = pool.borrowObject();
try
{
return sftp.ls(directory);
} catch (Exception e)
{
throw new Message());
}finally
{
}
}
}
主配置类
fig;
ample.ftp.SftpFactory;
ample.ftp.SftpHelper;
ample.ftp.SftpPool;
ample.ftp.SftpProperties;
import org.t.properties.EnableConfigurationProperties; import t.annotation.Bean;

本文发布于:2023-05-15 17:37:06,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/109608.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:连接   配置   对象   存在   创建   路径
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图