欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot2.1.3配置sftp自定义sftp连接池的详细过程

程序员文章站 2022-03-12 22:40:34
项目地址项目地址:完整配置参考com.example.ftp包maven: com.jcr...

项目地址

项目地址:
完整配置参考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连接池内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!