久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

從 ftp 獲取最新文件

get latest file from ftp(從 ftp 獲取最新文件)
本文介紹了從 ftp 獲取最新文件的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

嘗試創(chuàng)建一個(gè)簡(jiǎn)單的插件,它可以簡(jiǎn)單地連接到一個(gè) ftp 站點(diǎn),查找最新的文件然后下載它.但是,它沒有獲取最新的文件.

Trying to create a simple plugin that simply connects to an ftp site, looks up the latest file and then downloads it. However, it isn't getting the latest file.

我正在使用 org.apache.commons.net.ftp.ftpclient 處理一切.

I'm using the org.apache.commons.net.ftp.ftpclient for everything.

這是我的代碼

public static void main(String[] args)
  {
  FTPClient client = new FTPClient();
  try
  {
     client.connect(host);
     client.login(user, pwd);
     FTPFile[] files = client.listFiles();
     FTPFile lastFile = lastFileModified(files); 
     System.out.println(lastFile.getName());
     client.disconnect();
  }
  catch(SocketException e)
  {
     // TODO Auto-generated catch block
     e.printStackTrace();
  }
  catch(IOException e)
  {
     // TODO Auto-generated catch block
     e.printStackTrace();
  }

}

public static FTPFile lastFileModified(FTPFile[] files) {
  Date lastMod = files[0].getTimestamp().getTime();
  FTPFile choice = null;
  for (FTPFile file : files) {
          if (file.getTimestamp().getTime().after(lastMod)) {
                  choice = file;
                  lastMod = file.getTimestamp().getTime();
          }
   }
   return choice;
}

它正在獲取文件列表,然后返回一個(gè)文件,它不是最新的文件.有沒有其他方法可以使用 FTPClient 比較文件修改日期,或者任何人都可以指出我做錯(cuò)了什么的方向.謝謝.

It's getting the list of files, and then returning a file, it just isn't the latest file. Is there any other way to compare file modification dates using FTPClient or can anyone point me in a direction on what I'm doing wrong. Thanks.

推薦答案

我將創(chuàng)建一個(gè)比較器,而不是您的lastFileModified"方法.寫排序方法會(huì)更容易:

Instead of your "lastFileModified" method, I would create a Comparator. It would be easier to write the sort method:

public class LastModifiedComparator implements Comparator<FTPFile> {

    public int compare(FTPFile f1, FTPFile f2) {
        return f1.getTimestamp().compareTo(f2.getTimeStamp());
    }
}

那么,獲取最后一個(gè)"FTPFile 就容易多了:

Then, getting the "last" FTPFile is much easier:

public FTPFile getMaxLastModified(FTPFile[] ftpFiles) {
    return Collections.max(Arrays.asList(ftpFiles), new LastModifiedComparator());
}

<小時(shí)>

回到您的問(wèn)題:lastModified"時(shí)間戳未鏈接到 FTP 上傳順序.當(dāng)您通過(guò) FTP 協(xié)議上傳文件時(shí),可能會(huì)保留文件的原始時(shí)間戳.


To come back to your problem: the "lastModified" timestamp is not linked to the FTP upload order. When you upload a file through the FTP protocol, the original timestamp of the file may be preserved.

因此,如果 file1 早于 file2,您的方法將始終返回 file2,即使 file2 在 FTP 服務(wù)器上的 file1 之前上傳也是如此.

So, if file1 is older than file2, your method will always return file2, even if file2 is uploaded before file1 on the FTP server.

我認(rèn)為不可能確定最后上傳的文件.FTP 協(xié)議不存儲(chǔ)此信息.只有當(dāng)您重載 FTP 客戶端的put"方法時(shí),您才能這樣做:

I think that it is impossible to determine the last uploaded file. This information is not stored by the FTP protocol. You can do that only if you overload the "put" method of your FTP client:

public void put(File file) {
    // upload code
    FTPFile ftpFile = getJustUploadedFile(file);
    ftpFile.setTimestamp(new Calendar()); // Now! 
}

這篇關(guān)于從 ftp 獲取最新文件的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫(kù))
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 无毛av| 欧美日韩亚 | 国产日韩一区二区三区 | 国产精品毛片一区二区在线看 | 欧美成人精品 | 伊人精品视频 | 97av视频| 欧美综合视频在线 | 国产一区久久久 | 欧美黄色免费网站 | 欧美日韩一区在线播放 | 91精品一区| 久久国产精品一区二区三区 | 国产精品夜色一区二区三区 | 久久一级免费视频 | 日韩高清一区 | 日韩精品在线观看网站 | 欧美一级二级三级视频 | 男女羞羞视频在线 | 精产嫩模国品一二三区 | 日韩在线视频精品 | 91一区| 精品亚洲一区二区三区 | 久久国产精品色av免费观看 | 91精品国产色综合久久不卡蜜臀 | 久久精品亚洲精品 | 精品久久一区二区三区 | 成人免费一级 | 一区二区三区不卡视频 | 日韩午夜网站 | 午夜av成人 | 国产一极毛片 | 国产高潮好爽受不了了夜色 | 国产精品毛片一区二区在线看 | 日韩一区二区在线视频 | 成年网站在线观看 | 911精品国产 | 伊人伊人 | 天天av网| 欧美日韩久久 | 亚洲精品久久久久久国产精华液 |