欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java使用sftp定時下載文件的示例代碼

 更新時間:2018年05月17日 16:28:15   作者:tianshl  
SFTP 為 SSH的其中一部分,是一種傳輸檔案至 Blogger 伺服器的安全方式。接下來通過本文給大家介紹了Java使用sftp定時下載文件的示例代碼,感興趣的朋友跟隨腳本之家小編一起看看吧

sftp簡介

sftp是Secure File Transfer Protocol的縮寫,安全文件傳送協(xié)議。可以為傳輸文件提供一種安全的網(wǎng)絡(luò)的加密方法。sftp 與 ftp 有著幾乎一樣的語法和功能。SFTP 為 SSH的其中一部分,是一種傳輸檔案至 Blogger 伺服器的安全方式。其實在SSH軟件包中,已經(jīng)包含了一個叫作SFTP(Secure File Transfer Protocol)的安全文件信息傳輸子系統(tǒng),SFTP本身沒有單獨的守護進(jìn)程,它必須使用sshd守護進(jìn)程(端口號默認(rèn)是22)來完成相應(yīng)的連接和答復(fù)操作,所以從某種意義上來說,SFTP并不像一個服務(wù)器程序,而更像是一個客戶端程序。SFTP同樣是使用加密傳輸認(rèn)證信息和傳輸?shù)臄?shù)據(jù),所以,使用SFTP是非常安全的。但是,由于這種傳輸方式使用了加密/解密技術(shù),所以傳輸效率比普通的FTP要低得多,如果您對網(wǎng)絡(luò)安全性要求更高時,可以使用SFTP代替FTP。

添加依賴

<dependency>
  <groupId>com.jcraft</groupId>
  <artifactId>jsch</artifactId>
  <version>0.1.54</version>
</dependency>

增加配置

sftp:
  ip: 192.168.1.60
  port: 22
  timeout: 60000
  retryTime: 3
  admin:
    username: admin
    password: 2k3xrYjbd930.

代碼示例

每天凌晨1點在多個用戶目錄中下載csv文件至本地tmp目錄

@Service
public class SftpTask extends Thread {
  private ChannelSftp sftp;
  private Session session;
  @Value("${sftp.admin.username}")
  private String username;
  @Value("${sftp.admin.password}")
  private String password;
  @Value("${sftp.host}")
  private String host;
  @Value("${sftp.port}")
  private Integer port;
  private SftpService sftpService;
  public EtlSftpTask (SftpService sftpService) {
    this.sftpService = sftpService;
  }
  /**
   * 建立sftp連接
   */
  private void connect(){
    try {
      JSch jSch = new JSch();
      session = jSch.getSession(username, host, port);
      session.setPassword(password);
      session.setConfig("StrictHostKeyChecking", "no");
      session.connect();
      Channel channel = session.openChannel("sftp");
      channel.connect();
      sftp = (ChannelSftp) channel;
    }catch (JSchException e) {
      e.printStackTrace();
    }
  }
  /**
   * 關(guān)閉sftp連接
   */
  public void close(){
    try {
      if (sftp != null) {
        if (sftp.isConnected()) sftp.disconnect();
      }
      if(session != null){
        if (session.isConnected()) session.disconnect();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * 下載文件到本地
   *
   * @param source          源文件
   * @param target          目標(biāo)文件
   * @throws SftpException      異常
   * @throws FileNotFoundException  異常
   */
  private void download(String source, String target) throws SftpException, FileNotFoundException {
    sftp.get(source, new FileOutputStream(new File(target)));
  }
  /**
   * 處理用戶數(shù)據(jù)文件
   *
   * @param root   數(shù)據(jù)文件根目錄
   * @param lastTime 上次處理文件的最后的時間
   * @return     本次處理文件的最后的時間
   */
  private Integer handle(String root, Integer lastTime) {
    String directory = root + "/event/";
    Vector files;
    try {
      files = sftp.ls(directory + "event_*.csv");
    } catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
    // 文件名
    String fileName;
    // 臨時文件
    String tmpFile;
    // 文件更新時間
    Integer mTime;
    // 文件最后更新時間
    Integer maxTime = lastTime;
    // 處理用戶文件
    for(Object o: files) {
      try {
        ChannelSftp.LsEntry f = (ChannelSftp.LsEntry) o;
        // 文件更新時間
        mTime = f.getAttrs().getMTime();
        if (mTime <= lastTime) continue;
        // 文件名
        fileName = f.getFilename();
        // 最后處理事件
        maxTime = Math.max(maxTime, mTime);
        // 下載文件
        tmpFile = "/tmp/" + fileName;
        download(directory + fileName, tmpFile);
      } catch (Exception e) {
        // TODO 錯誤日志
        e.printStackTrace();
      }
    }
    // 返回文件最后的處理時間
    return maxTime;
  }
  /**
   * 每天凌晨1點開始執(zhí)行
   */
  @Scheduled(cron = "0 0 1 * * *")
  public void task () {
    // 獲取sftp連接
    connect();
    String root;
    Integer lastTime;
    Long cid;
    Integer maxTime = lastTime;
    // 獲取用戶列表
    for (SftpDTO sftpDTO: sftpService.findAll()) {
      // 用戶主目錄
      root = sftpDTO.getSftpRoot();
      // 上次處理文件的最后時間
      lastTime = sftpDTO.getLastTime();
      maxTime = Math.max(maxTime, handle(root, lastTime));
      // 更新最后處理時間
      if (!maxTime.equals(lastTime)) {
        sftpDTO.setLastTime(maxTime);
        sftpService.update(sftpDTO);
      }
    }
    // 釋放sftp資源
    close();
  }
}

總結(jié)

以上所述是小編給大家介紹的Java使用sftp定時下載文件的示例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論