JAVA兼容SFTP传输文件

依赖包:com.jcraft.jsch

import com.common.exception.sftp.SftpConnectException;
import com.common.exception.sftp.SftpDowloadException;
import com.common.exception.sftp.SftpOperateException;
import com.common.exception.sftp.SftpUploadException;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;
import lombok.extern.slf4j.Slf4j;

import java.io.*;

/**
 * Sftp上传下载
 * @author lkq
 */
@Slf4j
public class SftpWorker extends Operate {

    private String host;

    private int port;

    private String userName;

    private String password;

    public static SftpWorker getInstance(){
        return new SftpWorker();
    }

    public SftpWorker option(String host, int port, String userName, String password){
        this.host = host;
        this.port = port;
        this.userName = userName;
        this.password = password;
        return this;
    }

    @Override
    public void upload(InputStream in, String module, String fileName) throws SftpUploadException, SftpConnectException, SftpOperateException {

        cdUpload();

        try {
            this.sftp.cd(module);
        } catch (SftpException e) {
            try {
                this.sftp.mkdir(module);
                this.sftp.cd(module);
            } catch (SftpException ex) {
                log.error("上传失败!原因:创建文件夹异常",e);
                throw new SftpUploadException("上传失败!原因:创建文件夹异常");
            }
        }
        try {
            this.sftp.put(in,fileName);
        } catch (SftpException e) {
            log.error("上传失败!原因:上传异常",e);
            throw new SftpUploadException("上传失败!原因:上传异常");
        }

        super.disConnect();

    }

    @Override
    public File dowload(String module, String fileName,String tmpPath) throws SftpConnectException, SftpOperateException, SftpDowloadException {
        cdUpload();
        try {
            File file = new File(tmpPath + File.separator + fileName);
            FileOutputStream outputStream = new FileOutputStream(file);
            this.sftp.cd(module);
            this.sftp.get(fileName,outputStream);
            outputStream.close();
            return file;
        } catch (Exception e1) {
            throw new SftpDowloadException("下载文件异常!");
        }

    }

    public File dowload(String module, String fileName, String id, String tmpPath) throws SftpConnectException, SftpOperateException, SftpDowloadException {
        cdUpload();
        try {
            File file = new File(tmpPath + File.separator + fileName);
            FileOutputStream outputStream = new FileOutputStream(file);
            this.sftp.cd(module);
            this.sftp.get(id, outputStream);
            outputStream.close();
            return file;
        } catch (Exception e1) {
            throw new SftpDowloadException("下载文件异常!");
        }

    }
    private void cdUpload() throws SftpConnectException, SftpOperateException {
        try {
            super.connect(this.host,this.port,this.userName,this.password);
            this.sftp.cd("upload");
        } catch (JSchException e) {
            log.error("操作失败!原因:sftp 连接异常",e);
            throw new SftpConnectException("上传失败!原因:sftp 连接异常");
        } catch (SftpException e) {
            log.error("操作失败!原因:上传操作出现异常",e);
            throw new SftpOperateException("上传失败!原因:上传操作出现异常");
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值