sftp工具类

使用到的依赖

sftp工具的依赖

        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.55</version>
        </dependency>

使用到的工具的依赖

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.8</version>
        </dependency>

日志依赖

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

工具类

package cy.example.demo.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.jcraft.jsch.*;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.lang.reflect.Field;
import java.util.Properties;
import java.util.Vector;

/**
 * author: cy
 * date: 2022/11/25
 */
@Slf4j
public class SftpUtils {
   

    private Session session;
    private ChannelSftp channelSftp;

    private final String sftpUsername;
    private final String sftpIp;
    private final String sftpPassword;

    public SftpUtils(String sftpUsername, String sftpIp, String sftpPassword) {
   
        this.sftpUsername = sftpUsername;
        this.sftpIp = sftpIp;
        this.sftpPassword = sftpPassword;
    }

    /**
     * 远程下载文件 sftp 批量下载
     * 默认删除远程文件
     *
     * @param remotePath 远程文件目录
     * @param localPath  本地文件目录
     * @return 反馈错误信息
     */
    public String downloadFiles(String remotePath, String localPath) {
   
        String errMsg = "";
        try {
   
            ChannelSftp channelSftp = getChannelSftp();
            Vector v = channelSftp.ls(remotePath);
            for (Object o : v) {
   
                ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) o;
                String filename = entry.getFilename();
                if (!".".equals(filename) && !"..".equals(filename)) {
   
                    try (OutputStream os = new FileOutputStream(localPath + File.separator + filename);) {
   
                        channelSftp.get(remotePath + File.separator + filename, os);
                        log.info("success download file [{}] to [{}] ", filename, localPath + File.separator + filename);
                        //下载成功,删除远程文件
                        try {
   
                            channelSftp.rm(remotePath + File.separator + filename);
                            log.info("successfully deleted file [{}]", filename);
                        } catch (Exception e) {
   
                            log.error("remote file deletion failed: {}", e.getMessage(), e);
                        }
                    }
                }
            }
        } catch (Exception e) {
   
            errMsg = "file download failed";
            log.error("file download failed: {}", e.getMessage(), e);
        } finally {
   
            disconnectSftp();
        }
        return errMsg;
    }

    /**
     * 远程下载文件 sftp 批量下载
     *
     * @param remotePath 远程文件目录
     * @param localPath  本地文件目录
     * @param del        是否删除远程文件 true 删除
     * @return 反馈错误信息
     */
    public String downloadFiles(String remotePath, String localPath, boolean del) {
   
        String errMsg = "";
        try {
   
            ChannelSftp channelSftp = getChannelSftp();
            Vector v = channelSftp.ls(remotePath);
            for (Object o : v) {
   
                ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) o;
                String filename = entry.getFilename();
                if (!".".equals(filename) && !"..".equals(filename)) {
   
                    try (OutputStream os = new FileOutputStream(localPath + File.separator + filename);) {
   
                        channelSftp.get(remotePath + File.separator + filename, os);
                        log.info("success download file [{}] to [{}] ", filename, localPath + File.separator + filename);
                        //下载成功,删除远程文件
                        if (del) {
   
                            try {
   
                                channelSftp.rm(remotePath + File.separator + filename);
                                log.info("successfully deleted file [{}]", filename);
                            } catch (Exception e) {
   
                                log.error("remote file deletion failed: {}", e.getMessage(), e);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
   
            errMsg = "file download failed";
            log.error("file download failed: {}", e.getMessage(), e);
        } finally {
   
            disconnectSftp();
        }
        return errMsg;
    }

    /**
     * 远程下载文件 sftp 批量下载
     * 默认删除远程文件
     *
     * @param remotePath   远程文件目录
     * @param localPath    本地文件目录
     * @param localPathBak 本地备份文件目录 null为不备份
     * @return 反馈错误信息
     */
    public String downloadFiles(String remotePath, String localPath, String localPathBak) {
   
        String errMsg = "";
        try {
   
            ChannelSftp channelSftp = getChannelSftp();
            Vector v = channelSftp.ls(remotePath);
            for (Object o : v) {
   
                ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) o;
                String filename = entry.getFilename();
                if (!".".equals(filename) && !"..".equals(filename)) {
   
                    try (OutputStream os = new FileOutputStream(localPath + File.separator + filename);) {
   
                        channelSftp.get(remotePath + File.separator + filename
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值