SFTP工具

工具类


@Slf4j
@Component
public class SFTPUtils {

    @Resource
    private SftpConfig sftpConfig;

    Session session = null;
    Channel channel = null;

    /**
     * 网络图片url
     *
     * @param fileUrl
     * @throws JSchException
     */
    public String uploadFileSFTP(String fileUrl) {
        if (StringUtils.isBlank(fileUrl)) {
            throw new RuntimeException("图片路径不能为空");
        }
        String[] split = fileUrl.split("=");
        String fileName = split[split.length - 1];
        URL url = null;
        try {
            url = new URL(fileUrl);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        ChannelSftp chSftp = null;
        try {
            String dst = sftpConfig.getVpsUrl() + LocalDate.now().format(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)) + "/"; // 目标文件名
            //登录sftp
            chSftp = this.getChannel(60000);
            //创建文件夹
            this.createDir(dst, chSftp);
            chSftp.put(url.openStream(), dst + fileName, ChannelSftp.OVERWRITE); // 代码段2(常用)
            return dst + fileName;
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        } finally {
            if (null != chSftp) {
                chSftp.quit();
            }
            if (channel != null) {
                channel.disconnect();
            }
            if (session != null) {
                session.disconnect();
            }

        }
        return null;
    }

    /**
     * 上传SFTP
     *
     * @param inputStream 文件流
     * @throws JSchException
     */
    public String uploadFileSFTP(InputStream inputStream) {
        ChannelSftp chSftp = null;
        try {
            String dst = sftpConfig.getVpsUrl() + LocalDate.now().format(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)) + "/"; // 目标文件名
            //登录sftp
            chSftp = this.getChannel(60000);
            //创建文件夹
            this.createDir(dst, chSftp);
            String fileUrl = dst + System.currentTimeMillis() + ".png";
            chSftp.put(inputStream, fileUrl, ChannelSftp.OVERWRITE); // 代码段2(常用)
            return fileUrl;
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        } finally {
            if (null != chSftp) {
                chSftp.quit();
            }
            if (channel != null) {
                channel.disconnect();
            }
            if (session != null) {
                session.disconnect();
            }
        }
        return null;
    }

    //登录sftp
    public ChannelSftp getChannel(int timeout) throws JSchException {
        //创建JSch对象
        JSch jsch = new JSch();
        //根据用户名,主机ip,端口获取一个Session对象
        session = jsch.getSession(sftpConfig.getFtpUserName(), sftpConfig.getFtpHost(), sftpConfig.getPortL());
        session.setPassword(sftpConfig.getFtpPassword());
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config); // 为Session对象设置properties
        session.setTimeout(timeout); // 设置timeout时间
        session.connect(); // 通过Session建立链接
        channel = session.openChannel("sftp"); // 打开SFTP通道
        channel.connect(); // 建立SFTP通道的连接
        return (ChannelSftp) channel;
    }

    //创建目录
    private void createDir(String createpath, ChannelSftp sftp) {
        try {
            sftp.mkdir(createpath);
        } catch (SftpException e) {
            log.error(createpath + "文件夹已存在");
        }
    }

}

配置类

yml文件为
test:
  sftp:
    ftpHost: 127.0.0.1
    portL: 22
    ftpUserName: root
    ftpPassword: root
    vpsUrl: /data/test/img	


@Data
@Configuration
@ConfigurationProperties(prefix = "test.sftp")
public class SftpConfig {

    /*服务器IP*/
    private String ftpHost;

    /*服务器端口*/
    private Integer portL;

    /*服务器-用户名称*/
    private String ftpUserName;

    /*服务器用户密码*/
    private String ftpPassword;

    /*上传文件位置*/
    private String vpsUrl;

}

调用

 @Resource
 private SFTPUtils sftpUtils;

//这里两个方法分别可以传 网络图片的url地址或者流
String fqFm = sftpUtils.uploadFileSFTP('图片url');

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值