栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

单session多channel的sftp多线程并发下载

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

单session多channel的sftp多线程并发下载

依赖


    com.jcraft
    jsch
    0.1.53

 import com.jcraft.jsch.*;

连接   使用ThreadLocal管理channel,确保每个线程单独一份channel,互不干扰

    public static boolean isFtpConn = false;
    
    private Session session = null;
    
    private ChannelSftp channel = null;
    @PostConstruct
    public boolean login() {

        try {
            JSch jsch = new JSch();
            session = jsch.getSession(username, host, port);
            if (password != null) {
                session.setPassword(password);
            }
            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.setTimeout(16000);
            log.info(session.getHost() + session.getPort() + session.toString());
            session.connect();
            log.debug("sftp session connected");

            log.debug("opening channel");
            channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();

            log.info("connected successfully");
            isFtpConn = true;
            return true;
        } catch (Exception e) {
            isFtpConn = false;
            log.info("sftp login failed", e);
            return false;
        }
    }
 
    //ThreadLocal在每个线程中对连接会创建一个副本,且在线程内部任何地方都可以使用,线程之间互不影响

    private static ThreadLocal t = new ThreadLocal();

    public ChannelSftp getChannel() throws JSchException {
        ChannelSftp channel = t.get();
        if (channel == null){
            channel = (ChannelSftp) this.session.openChannel("sftp");
            channel.connect();
            t.set(channel);
        }
        return channel;
    }

    
    public static void closeResource(ChannelSftp channel) {
        if (channel != null) {
            channel.disconnect();
            t.remove();
        }
    }

 下载

    public void downloadFile(String sourcePath, List fileNameList, String                 descPath) throws RuntimeException {
        if (!isFtpConn) {
            if (!login()){
                throw new RuntimeException("login failed");
            }
        }
        ChannelSftp channel = null;
        try {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (String fileName : fileNameList) {
                String localFilePath = descPath + "/" + fileName;
                Files.createDirectories(Paths.get(descPath));
                channel = getChannel();
                channel.get(localFilePath, localFilePath);
            }
            log.info("download successful ==== descPath ===" + descPath);
        } catch (SftpException | IOException | JSchException e) {
            log.info("download file failed", e);
            throw new RuntimeException("download file failed");
        } finally {
            closeResource(channel);
        }
    }

 jmeter压测

 

channel.get(sourcePath+fileName,descPath+fileName)为下载方法,可参考

https://www.cnblogs.com/longyg/archive/2012/06/25/2561332.html 

不明白为什么好多人都要cd到当前目录,在根据当前目录get下载

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/310308.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号