java 使用sftp读取文件多线程

引入pom文件

<!--sftp-->
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.54</version>
        </dependency>
   /**
     * 连接sftp服务器
     *
     */
    public ChannelSftp sftpConnect() {
        ChannelSftp sftp = null;
        try {
            JSch jsch = new JSch();
            Session sshSession = jsch.getSession(username, host, port);//用户名,主机,端口号
            sshSession.setPassword(password);//密码
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            sshSession.setTimeout(30000); // 设置timeout时间
            sshSession.connect();
            Channel channel = sshSession.openChannel("sftp");
            channel.connect();
            sftp = (ChannelSftp) channel;
            log.info("sftp connected success");
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return sftp;
    }


    /**
     * 下载文件
     *
     * @param remoteFile 文件名称
     */
    public JSONArray sftpDownload(String remoteFile,) {
        ChannelSftp sftp=sftpConnect();
        if(null!=sftp){
            try {
                if(StringUtils.isNotBlank(remoteFile)){
                    //sftpPath 文件所在的路径
                    remoteFile = sftpPath +"/"+ remoteFile;
                    InputStream in=sftp.get(remoteFile);
                    String lineTxt=null;
                    BufferedReader br1 = new BufferedReader(new InputStreamReader(in,"utf-8"));//考虑到编码格式
                    while ((lineTxt = br1.readLine()) != null) {//读取下一行
                        if(!"".equals(lineTxt)){
                            //处理文件类容 入库等
                        }
                    }

                    if(br1!=null){
                        br1.close();
                    }
                    if(in!=null){
                        in.close();
                    }

                    if("normal".equals(reqType)){//对于正常请求 放入ltMap
                        ltMap.put(key,jsonArrayMap);
                    }
                    return jsonArrayMap.get(mk.getReqArea());
                }
            } catch (Exception e) {
                log.error("sftpDownload.remoteFile={},type={},error={}",remoteFile,mk.getDescription(),e);
            }finally {
                //关闭连接
                disconnect(sftp);
            }
        }
        return null;
    }



 /**
     * 下载文件
     *
     * @param directory     SFTP服务器的文件路径
     * @param downloadFile  SFTP服务器上的文件名
     * @param saveFile      保存到本地路径
     */
    public void download(String directory, String downloadFile, String saveFile){
        try{
            if(directory != null && !"".equals(directory)){
                sftp.cd(directory);
            }
            File file = new File(saveFile);
            sftp.get(downloadFile, new FileOutputStream(file));
        }catch (SftpException | FileNotFoundException e){
            logger.error("文件下载异常!", e);
        }
    }

多线程使用

package cn.witsky.hd_backend.common.utils;

import cn.witsky.hd_backend.project.business.domain.MappingAndKpiType;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jcraft.jsch.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Administrator
 * @date 2022/8/3 13:46
 */
@Slf4j
@Component
public class SftpUtil {
    ThreadLocal<ChannelSftp> channelThreadLocal = new ThreadLocal<>();
    ThreadLocal<Session> sessionThreadLocal = new ThreadLocal<>();


    /** 联通 */
    @Value(value = "${api.host}")
    private String host;
    @Value(value = "${api.port}")
    private int port;
    @Value(value = "${api.username}")
    private String username;
    @Value(value = "${api.password}")
    private String password;
    @Value(value = "${api.sftpPath}")
    private String sftpPath;
    @Value(value = "${api.sftpTimeOut}")
    private int sftpTimeOut;

    public void uploadFile(ChannelSftp channel, String src, String dest) throws SftpException {
        channel.put(src, dest);
    }

    public void uploadFile(String src, String dest) throws SftpException, JSchException {
        uploadFile(getChannel(), src, dest);
    }

    /**
     * 下载文件
     *
     * @param remoteFile 远程文件
     */
    public JSONArray sftpDownload(String remoteFile, String reqType, String key, MappingAndKpiType mk, ConcurrentHashMap<String,ConcurrentHashMap<Long,JSONArray>> ltMap, Map<String,Long> ltAreaMap) {
        ChannelSftp sftp= getChannel();
        if(null!=sftp){
            InputStream in=null;
            BufferedReader br1=null;
            try {
                if(org.apache.commons.lang3.StringUtils.isNotBlank(remoteFile)){
                    ConcurrentHashMap<Long,JSONArray> jsonArrayMap=new ConcurrentHashMap();//areaId,组装json数据
                    remoteFile = sftpPath +"/"+ remoteFile;
                    in=sftp.get(remoteFile);
                    String lineTxt=null;
                    br1 = new BufferedReader(new InputStreamReader(in,"utf-8"));//考虑到编码格式
                    while ((lineTxt = br1.readLine()) != null) {//读取下一行
                    }

                    if("normal".equals(reqType)){//对于正常请求 放入ltMap
                        ltMap.put(key,jsonArrayMap);
                    }
                    return jsonArrayMap.get(mk.getReqArea());
                }
            } catch (Exception e) {
                log.error("sftpDownload.remoteFile={},type={},error={}",remoteFile,mk.getDescription(),e);
            }finally {
                try {
                    if(br1!=null){
                        br1.close();
                    }
                    if(in!=null){
                        in.close();
                    }
                }catch (Exception e){

                }
            }
        }
        return null;
    }



    public void connect(){
        try {
            JSch jsch = new JSch();
            Session sshSession = jsch.getSession(username, host, port);
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            sshSession.setTimeout(sftpTimeOut); // 设置timeout时间
            sshSession.connect();
            Channel channel = sshSession.openChannel("sftp");
            channel.connect();
            ChannelSftp sftp = (ChannelSftp) channel;
            log.info("sftp connected success");
            channelThreadLocal.set(sftp);
            sessionThreadLocal.set(sshSession);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    public void disconnectAll(){
        try {
            ChannelSftp channel = channelThreadLocal.get();
            if (channel != null && channel.isConnected()) {
                channel.disconnect();
            }
            Session session = sessionThreadLocal.get();
            if (session != null && session.isConnected()) {
                session.disconnect();
            }
            log.info("disconnect.sftp");
        }catch (Exception e){
            log.error("disconnectAll.error={}",e);
        }
    }

    public ChannelSftp getChannel() {
        try {
            ChannelSftp channelSftp = channelThreadLocal.get();
            if (channelSftp == null||(!channelSftp.isConnected())) {
                connect();
            }
            return channelThreadLocal.get();
        }catch (Exception e){
            log.error("getChannel.error={}",e);
        }
      return null;
    }

    public Session getSession() throws JSchException, SftpException {
        Session session = sessionThreadLocal.get();
        if (session == null) {
            connect();
        }
        return sessionThreadLocal.get();
    }
}

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值