SpringBoot实战(二十六)集成SFTP

目录

    • 一、SFTP简介
    • 二、SpringBoot 集成
      • 2.1 Maven 依赖
      • 2.2 application.yml 配置
      • 2.3 DemoController.java 接口
      • 2.4 SftpService.java
      • 2.5 DemoServiceImpl.java 实现类
      • 2.6 SftpUtils.java 工具类
      • 2.7 执行结果
        • 1)上传文件
        • 2)下载文件
        • 3)重命名文件(移动)
        • 4)删除文件

一、SFTP简介

SFTP:全称 Secure File Transfer Protocol,是一种安全文件传输协议,它基于 SSH(Secure Shell)协议并为其提供了文件传输服务。相比于传统的 FTP,SFTP 提供了加密的数据传输通道,能够有效保护数据在传输过程中不被窃取和篡改,增强了安全性。

  • SFTP 服务器,在 LinuxMac 系统中是自带的,可以直接使用 Linux 的用户名/密码来登录 SFTP,windows 下默认只支持 SFTP 客户端,不支持 SFTP 服务器。

SFTP 登录命令:

# 和ssh命令一样:用户名@ip
sftp [email protected]

补充:由于 Linux 默认集成了 SFTP 服务器,所以测试 SpringBoot 集成 SFTP 的时候不需要再搭建 SFTP,直接用户名/密码连 Linux 系统即可。


二、SpringBoot 集成

2.1 Maven 依赖


<dependency>
    <groupId>com.jcraftgroupId>
    <artifactId>jschartifactId>
    <version>0.1.55version>
dependency>

2.2 application.yml 配置

sftp:
  protocol: sftp
  host: 192.168.1.10
  port: 22
  username: root
  password: root

2.3 DemoController.java 接口

import com.demo.common.Result;
import com.demo.service.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 

@Title DemoController *

@Description 测试Controller * * @author ACGkaka * @date 2023/4/24 18:02 */ @Slf4j @RestController @RequestMapping("/demo") public class DemoController { @Resource private DemoService demoService; /** * 上传文件 */ @PostMapping("/upload") public Result<Object> upload(@RequestParam String sftpPath, @RequestParam MultipartFile file) { demoService.upload(sftpPath, file); return Result.succeed(); } /** * 下载文件 */ @GetMapping("/download") public void download(@RequestParam String sftpPath, HttpServletResponse response) { demoService.download(sftpPath, response); } /** * 重命名文件(移动) */ @GetMapping("/rename") public Result<Object> rename(@RequestParam String oldPath, @RequestParam String newPath) { demoService.rename(oldPath, newPath); return Result.succeed(); } /** * 删除文件 */ @GetMapping("/delete") public Result<Object> delete(@RequestParam String sftpPath) { demoService.delete(sftpPath); return Result.succeed(); } }

2.4 SftpService.java

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 

@Title DemoService *

@Description 测试Service * * @author ACGkaka * @date 2023/4/24 18:13 */ public interface DemoService { /** * 上传文件 * @param sftpPath 路径 * @param file 文件 */ void upload(String sftpPath, MultipartFile file); /** * 下载文件 * @param sftpPath * @param response */ void download(String sftpPath, HttpServletResponse response); /** * 重命名文件(移动) * @param oldPath * @param newPath */ void rename(String oldPath, String newPath); /** * 删除文件 * @param sftpPath */ void delete(String sftpPath); }

2.5 DemoServiceImpl.java 实现类

import com.demo.service.DemoService;
import com.demo.util.SftpUtils;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

/**
 * 

@Title DemoServiceImpl *

@Description 测试ServiceImpl * * @author ACGkaka * @date 2023/4/24 18:14 */ @Slf4j @Service public class DemoServiceImpl implements DemoService { @Resource private SftpUtils sftpUtils; @Override public void upload(String sftpPath, MultipartFile file) { // 上传文件 ChannelSftp sftp = null; try (InputStream in = file.getInputStream()) { // 开启sftp连接 sftp = sftpUtils.createSftp(); // 进入sftp文件目录 sftp.cd(sftpPath); log.info("修改目录为:{}", sftpPath); // 上传文件 sftp.put(in, file.getOriginalFilename()); log.info("上传文件成功,目标目录:{}", sftpPath); } catch (SftpException | JSchException | IOException e) { log.error("上传文件失败,原因:{}", e.getMessage(), e); throw new RuntimeException("上传文件失败"); } finally { // 关闭sftp sftpUtils.disconnect(sftp); } } @Override public void download(String sftpPath, HttpServletResponse response) { // 下载文件 long start = System.currentTimeMillis(); ChannelSftp sftp = null; try { // 开启sftp连接 sftp = sftpUtils.createSftp(); // 判断sftp文件存在 File sftpFile = new File(sftpPath); boolean isExist = isFileExist(sftpPath, sftp); if (isExist) { // 下载文件 response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE); response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode(sftpFile.getName(), "utf-8")); sftp.get(sftpFile.getName(), response.getOutputStream()); // 记录日志 long time = System.currentTimeMillis() - start; log.info("sftp文件下载成功,目标文件:{},总耗时:{}ms.", sftpPath, time); } else { log.error("sftp文件下载失败,sftp文件不存在:" + sftpFile.getParent()); throw new RuntimeException("sftp文件下载失败,sftp文件不存在:" + sftpFile.getParent()); } } catch (SftpException | JSchException | IOException e) { log.error("sftp文件下载失败,目标文件名:{},原因:{}", sftpPath, e.getMessage(), e); throw new RuntimeException("sftp文件下载失败"); } finally { // 关闭sftp sftpUtils.disconnect(sftp); } } @Override public void rename(String oldPath, String newPath) { // 重命名文件(移动) ChannelSftp sftp = null; try { // 开启sftp连接 sftp = sftpUtils.createSftp(); // 修改sftp文件路径 sftp.rename(oldPath, newPath); log.info("sftp文件重命名成功,历史路径:{},新路径:{}", oldPath, newPath); } catch (SftpException | JSchException e) { log.error("sftp文件重命名失败,原因:{}", e.getMessage(), e); throw new RuntimeException("sftp文件重命名失败"); } finally { // 关闭sftp sftpUtils.disconnect(sftp); } } @Override public void delete(String sftpPath) { // 删除文件 ChannelSftp sftp = null; try { // 开启sftp连接 sftp = sftpUtils.createSftp(); // 判断sftp文件存在 boolean isExist = isFileExist(sftpPath, sftp); if (isExist) { // 删除文件 SftpATTRS sftpATTRS = sftp.lstat(sftpPath); if (sftpATTRS.isDir()) { sftp.rmdir(sftpPath); } else { sftp.rm(sftpPath); } log.info("sftp文件删除成功,目标文件:{}.", sftpPath); } else { log.error("sftp文件删除失败,sftp文件不存在:" + sftpPath); throw new RuntimeException("sftp文件删除失败,sftp文件不存在:" + sftpPath); } } catch (SftpException | JSchException e) { log.error("sftp文件删除失败,原因:{}", e.getMessage(), e); throw new RuntimeException("sftp文件删除失败"); } finally { // 关闭sftp sftpUtils.disconnect(sftp); } } /** * 判断目录是否存在 */ private boolean isFileExist(String sftpPath, ChannelSftp sftp) { try { // 获取文件信息 SftpATTRS sftpATTRS = sftp.lstat(sftpPath); return sftpATTRS != null; } catch (Exception e) { log.error("判断文件是否存在失败,原因:{}", e.getMessage(), e); return false; } } }

2.6 SftpUtils.java 工具类

import com.demo.config.SftpProperties;
import com.jcraft.jsch.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 

@Title SftpUtils *

@Description SFTP工具类 * * @author ACGkaka * @date 2024/1/31 17:41 */ @Slf4j @Component public class SftpUtils { @Resource private SftpProperties sftpProperties; /** * 创建SFTP连接 */ public ChannelSftp createSftp() throws JSchException { JSch jsch = new JSch(); log.info("Try to connect sftp[" + sftpProperties.getUsername() + "@" + sftpProperties.getHost() + "]"); Session session = createSession(jsch, sftpProperties.getHost(), sftpProperties.getUsername(), sftpProperties.getPort()); session.setPassword(sftpProperties.getPassword()); session.setConfig("StrictHostKeyChecking", "no"); // 默认情况下,JSch库本身并没有会话超时时间。 // 为了避免长时间无活动连接占用资源或因网络问题导致连接挂起而不被释放,通常建议设置会话超时,(单位:毫秒) session.setTimeout(30000); session.connect(); log.info("Session connected to {}.", sftpProperties.getHost()); Channel channel = session.openChannel(sftpProperties.getProtocol()); channel.connect(); log.info("Channel created to {}.", sftpProperties.getHost()); return (ChannelSftp) channel; } /** * 创建 Session */ public Session createSession(JSch jsch, String host, String username, Integer port) throws JSchException { Session session = null; if (port <= 0) { session = jsch.getSession(username, host); } else { session = jsch.getSession(username, host, port); } if (session == null) { throw new RuntimeException(host + "session is null"); } return session; } /** * 关闭连接 */ public void disconnect(ChannelSftp sftp) { try { if (sftp != null) { if (sftp.isConnected()) { sftp.disconnect(); } else if (sftp.isClosed()) { log.error("sftp 连接已关闭"); } if (sftp.getSession() != null) { sftp.getSession().disconnect(); } } } catch (JSchException e) { log.error("sftp 断开连接失败,原因:{}", e.getMessage(), e); } } }

2.7 执行结果

1)上传文件

请求地址:http://localhost:8080/demo/upload

执行结果:

SpringBoot实战(二十六)集成SFTP_第1张图片

2)下载文件

请求地址:http://localhost:8080/demo/download?sftpPath=/home/root/test.pdf

下载后,文件可以正常打开:

SpringBoot实战(二十六)集成SFTP_第2张图片

3)重命名文件(移动)

请求地址:http://localhost:8080/demo/rename?oldPath=/home/root/test1.pdf&newPath=/home/root/test/test.pdf

执行结果:

SpringBoot实战(二十六)集成SFTP_第3张图片

可以去sftp上面看下,文件已经被移动了:

在这里插入图片描述

4)删除文件

请求地址:http://localhost:8080/demo/delete?sftpPath=/home/root/test/test1.pdf

执行结果:

SpringBoot实战(二十六)集成SFTP_第4张图片

可以去sftp上面看下,文件已经被成功删除了:

在这里插入图片描述

整理完毕,完结撒花~





参考地址:

1.SFTP命令用法(上传和下载 ),https://blog.csdn.net/JacaCao/article/details/108190174

2.springBoot整合sftp,https://blog.csdn.net/winsanity/article/details/120665642

你可能感兴趣的:(SpringBoot实战,spring,boot,后端,java)