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

依赖

<!--sFTP连接-->
<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.53</version>
</dependency>

 import com.jcraft.jsch.*;

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

/**
     * 登陆SFTP服务器
     *
     * @return boolean
     */
    public static boolean isFtpConn = false;
    /**
     * Session
     */
    private Session session = null;
    /**
     * Channel
     */
    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<ChannelSftp> t = new ThreadLocal<ChannelSftp>();

    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;
    }

    /**
     * 关闭连接
     * @param channel
     */
    public static void closeResource(ChannelSftp channel) {
        if (channel != null) {
            channel.disconnect();
            t.remove();
        }
    }

 下载

    public void downloadFile(String sourcePath, List<String> 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下载

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
可以使用JSch库来实现在Java中上传文件到SFTP服务器的功能,以下是一个简的示例代码: ```java import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; public class SftpUploader { private String host; private int port; private String username; private String password; private String remoteDirectory; public SftpUploader(String host, int port, String username, String password, String remoteDirectory) { this.host = host; this.port = port; this.username = username; this.password = password; this.remoteDirectory = remoteDirectory; } public void uploadFile(String localFilePath, String remoteFilename) throws Exception { JSch jsch = new JSch(); Session session = jsch.getSession(username, host, port); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; sftpChannel.cd(remoteDirectory); File localFile = new File(localFilePath); InputStream inputStream = new FileInputStream(localFile); sftpChannel.put(inputStream, remoteFilename); inputStream.close(); sftpChannel.exit(); session.disconnect(); } } ``` 在上面的代码中,`SftpUploader`类的构造函数接收SFTP服务器的主机名、端口号、用户名、密码和远程目录路径。`uploadFile`方法接收本地文件路径和远程文件名,然后将文件上传到远程SFTP服务器。可以在多个线程中创建`SftpUploader`实例并调用`uploadFile`方法来同时上传多个文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值