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 類型,不需要手動關閉文件流,還是挺好用的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM