SpringBoot SFTP 文件传输

介绍

SFTP 是一种通过 SSH 实现的安全文件传输协议,不需要安装任何的软件就能传输文件。

依赖

haibaracp-spring-boot-starter 支持密码、密钥连接以及多主机连接,本文只介绍一些基础 API(上传、下载),更多 API 详见 GitHub - hligaty/haibaracp-spring-boot-starter 的文档或示例 GitHub - hligaty/haibaracp-test

<dependency>
  <groupId>io.github.hligaty</groupId>
  <artifactId>haibaracp-spring-boot-starter</artifactId>
  <version>1.2.3</version>
</dependency>

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-pool2</artifactId>
  <!-- spring-boot-dependencies 控制版本 -->
</dependency>

配置

sftp:
  host: localhost
  port: 22
  username: haibara
  password: aptx4869
  connect-timeout: 1000
  pool:
    min-idle: 1
    max-idle: 4
    max-active: 4

使用

注入 SftpTemplate 即可。

@Resource
private SftpTemplate sftpTemplate;

上传

Path path = Paths.get(System.getProperty("user.dir"), "file");
try {
  // upload to /home/username/doc/aptx4869.pdf
  sftpTemplate.upload(path.resolve("aptx4869.pdf").toString(), "/home/haibara/doc/aptx4869.pdf");
  // upload to /home/username/doc/aptx4869.doc
  sftpTemplate.upload(path.resolve("aptx4869.doc").toString(), "doc/aptx4869.doc");
  // upload to /home/username/aptx4869.docx
  sftpTemplate.upload(path.resolve("aptx4869.docx").toString(), "aptx4869.docx");
} catch (SftpException e) {
  if (e.id == ChannelSftp.SSH_FX_FAILURE && e.getCause() instanceof FileNotFoundException) {
    System.out.println("local file not exists");
  }
  throw e;
}

下载

void download() throws SftpException {
  Path path = Paths.get(downloadDir);
  try {
    // download /home/username/doc/aptx4869.pdf
    sftpTemplate.download("/home/haibara/doc/aptx4869.pdf", path.resolve("aptx4869.pdf").toString());
    // download /home/username/doc/aptx4869.doc
    sftpTemplate.download("doc/aptx4869.doc", path.resolve("aptx4869.doc").toString());
    // download /home/username/aptx4869.pdf
    sftpTemplate.download("aptx4869.docx", path.resolve("aptx4869.docx").toString());
  } catch (SftpException e) {
    if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
      System.out.println("remote file not exists");
    } else if (e.id == ChannelSftp.SSH_FX_FAILURE && e.getCause() instanceof FileNotFoundException) {
      System.out.println("local path not exists");
    }
    throw e;
  }
}

判断文件存在

void exists() throws SftpException {
  // Test path /home/username/doc/aptx4869.pdf
  System.out.println(sftpTemplate.exists("/home/haibara/doc/aptx4869.pdf"));
  // Test path /home/username/doc/aptx4869.doc
  System.out.println(sftpTemplate.exists("doc/aptx4869.doc"));
  // Test path /home/username/aptx4869.docx
  System.out.println(sftpTemplate.exists("aptx4869.docx"));
}

查看文件列表

void list() throws SftpException {
  // view /home/username/doc/aptx4869.pdf
  sftpTemplate.list("/home/haibara/doc/aptx4869.pdf");
  // view /home/username/doc/aptx4869.doc
  sftpTemplate.list("doc/aptx4869.doc");
  // view /home/username/aptx4869.docx
  sftpTemplate.list("aptx4869.docx");
  // view /home/username/doc
  sftpTemplate.list("/home/haibara/doc");
  // view /home/username/doc
  sftpTemplate.list("doc");
}

自定义 SFTP 操作

有返回值

void execute() throws SftpException {
  String home = sftpTemplate.execute(ChannelSftp::getHome);
  System.out.println(home);
}

无返回值

void executeWithoutResult() throws SftpException {
  sftpTemplate.executeWithoutResult(channelSftp -> channelSftp.rm("/home/haibara/doc/aptx4869.pdf"));
  sftpTemplate.executeWithoutResult(channelSftp -> channelSftp.rm("doc/aptx4869.doc"));
  sftpTemplate.executeWithoutResult(channelSftp -> channelSftp.rm("aptx4869.docx"));
}

最后

haibaracp 所有参数都是 string 类型,不需要手动关闭文件流,还是挺好用的。

posted @ 2022-01-27 16:44  hligy  阅读(1113)  评论(0编辑  收藏  举报