springboot2.1.3配置sftp自定義sftp連接池的詳細過程
項目地址
項目地址:https://gitee.com/xuelingkang/spring-boot-demo
完整配置參考com.example.ftp包
maven:
<!-- sftp --> <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.55</version> </dependency> <!-- commons-pool2 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.6.1</version> </dependency>
application.yml配置
sftp: host: server02 # 服務器ip port: 22 # ssh端口 username: demofile # 用戶名 password: demo # 密碼 # 連接池參數 pool: max-total: 10 max-idle: 10 min-idle: 5
SftpProperties
package com.example.ftp; import com.jcraft.jsch.ChannelSftp; import lombok.Data; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.boot.context.properties.ConfigurationProperties; @Data @ConfigurationProperties(prefix = "sftp") public class SftpProperties { private String host; private int port = 22; private String username = "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 setMaxTotal(int maxTotal) { this.maxTotal = maxTotal; } @Override public int getMaxIdle() { return maxIdle; } @Override public void setMaxIdle(int maxIdle) { this.maxIdle = maxIdle; } @Override public int getMinIdle() { return minIdle; } @Override public void setMinIdle(int minIdle) { this.minIdle = minIdle; } } }
sftp連接工廠
package com.example.ftp; import com.example.exception.ProjectException; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.apache.commons.pool2.BasePooledObjectFactory; import org.apache.commons.pool2.PooledObject; import org.apache.commons.pool2.impl.DefaultPooledObject; import java.util.Properties; @Data @Slf4j public class SftpFactory extends BasePooledObjectFactory<ChannelSftp> { private SftpProperties properties; public SftpFactory(SftpProperties properties) { this.properties = properties; } @Override public ChannelSftp create() { try { JSch jsch = new JSch(); Session sshSession = jsch.getSession(properties.getUsername(), properties.getHost(), properties.getPort()); sshSession.setPassword(properties.getPassword()); Properties sshConfig = new Properties(); sshConfig.put("StrictHostKeyChecking", "no"); sshSession.setConfig(sshConfig); sshSession.connect(); ChannelSftp channel = (ChannelSftp) sshSession.openChannel("sftp"); channel.connect(); 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連接池
package com.example.ftp; import com.example.exception.ProjectException; import com.jcraft.jsch.ChannelSftp; import lombok.Data; import org.apache.commons.pool2.impl.GenericObjectPool; @Data public class SftpPool { private GenericObjectPool<ChannelSftp> pool; public SftpPool(SftpFactory factory) { this.pool = new GenericObjectPool<>(factory, factory.getProperties().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) { pool.returnObject(channelSftp); } } }
sftp輔助類
package com.example.ftp; import com.example.exception.ProjectException; import com.example.util.ByteUtil; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.SftpException; import java.io.InputStream; // sftp輔助類 public class SftpHelper { private SftpPool pool; public SftpHelper(SftpPool pool) { this.pool = pool; } /** * 下載文件 * @param dir 遠程目錄 * @param name 遠程文件名 * @return 文件字節數組 */ public byte[] download(String dir, String name) { ChannelSftp sftp = pool.borrowObject(); try { sftp.cd(dir); InputStream in = sftp.get(name); return ByteUtil.inputStreamToByteArray(in); } catch (SftpException e) { throw new ProjectException("sftp下載文件出錯", e); } finally { pool.returnObject(sftp); } } /** * 上傳文件 * @param dir 遠程目錄 * @param name 遠程文件名 * @param in 輸入流 */ public void upload(String dir, String name, InputStream in) { ChannelSftp sftp = pool.borrowObject(); try { mkdirs(sftp, dir); sftp.cd(dir); sftp.put(in, name); } catch (SftpException e) { throw new ProjectException("sftp上傳文件出錯", e); } finally { pool.returnObject(sftp); } } /** * 刪除文件 * @param dir 遠程目錄 * @param name 遠程文件名 */ public void delete(String dir, String name) { ChannelSftp sftp = pool.borrowObject(); try { sftp.cd(dir); sftp.rm(name); } catch (SftpException e) { throw new ProjectException("sftp刪除文件出錯", e); } finally { pool.returnObject(sftp); } } /** * 遞歸創建多級目錄 * @param dir 多級目錄 */ private void mkdirs(ChannelSftp sftp, String dir) { String[] folders = dir.split("/"); try { sftp.cd("/"); for (String folder: folders) { if (folder.length()>0) { try { sftp.cd(folder); } catch (Exception e) { sftp.mkdir(folder); sftp.cd(folder); } } } } catch (SftpException e) { throw new ProjectException("sftp創建目錄出錯", e); } } }
主配置類
package com.example.config; import com.example.ftp.SftpFactory; import com.example.ftp.SftpHelper; import com.example.ftp.SftpPool; import com.example.ftp.SftpProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; // ftp配置 @Configuration @EnableConfigurationProperties(SftpProperties.class) public class SftpConfig { // 工廠 @Bean public SftpFactory sftpFactory(SftpProperties properties) { return new SftpFactory(properties); } // 連接池 @Bean public SftpPool sftpPool(SftpFactory sftpFactory) { return new SftpPool(sftpFactory); } // 輔助類 @Bean public SftpHelper sftpHelper(SftpPool sftpPool) { return new SftpHelper(sftpPool); } }
使用方法
@Autowired private SftpHelper sftpHelper;
註入輔助類,直接調用方法即可。
到此這篇關於springboot2.1.3配置sftp自定義sftp連接池的文章就介紹到這瞭,更多相關springboot自定義sftp連接池內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- SpringBoot集成SFTP客戶端實現文件上傳下載實例
- JSch教程使用sftp協議實現服務器文件載操作
- Linux雲服務器搭建SFTP服務器圖片服務器的操作
- Java使用Sftp和Ftp實現對文件的上傳和下載
- Java SSH 秘鑰連接mysql數據庫的方法