您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“springboot2.1.3配置sftp連接池的詳細(xì)過程”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
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 # 服務(wù)器ip port: 22 # ssh端口 username: demofile # 用戶名 password: demo # 密碼 # 連接池參數(shù) 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()); } /** * 獲取一個(gè)sftp連接對象 * @return sftp連接對象 */ public ChannelSftp borrowObject() { try { return pool.borrowObject(); } catch (Exception e) { throw new ProjectException("獲取ftp連接失敗", e); } } /** * 歸還一個(gè)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 遠(yuǎn)程目錄 * @param name 遠(yuǎn)程文件名 * @return 文件字節(jié)數(shù)組 */ 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下載文件出錯(cuò)", e); } finally { pool.returnObject(sftp); } } /** * 上傳文件 * @param dir 遠(yuǎn)程目錄 * @param name 遠(yuǎn)程文件名 * @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上傳文件出錯(cuò)", e); } finally { pool.returnObject(sftp); } } /** * 刪除文件 * @param dir 遠(yuǎn)程目錄 * @param name 遠(yuǎn)程文件名 */ 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刪除文件出錯(cuò)", e); } finally { pool.returnObject(sftp); } } /** * 遞歸創(chuàng)建多級目錄 * @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創(chuàng)建目錄出錯(cuò)", 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;
注入輔助類,直接調(diào)用方法即可。
“springboot2.1.3配置sftp連接池的詳細(xì)過程”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。