简体   繁体   English

从ftp获取最新文件

[英]get latest file from ftp

Trying to create a simple plugin that simply connects to an ftp site, looks up the latest file and then downloads it. 尝试创建一个简单的插件,只需连接到ftp站点,查找最新的文件,然后下载它。 However, it isn't getting the latest file. 但是,它没有获取最新文件。

I'm using the org.apache.commons.net.ftp.ftpclient for everything. 我正在使用org.apache.commons.net.ftp.ftpclient来处理所有事情。

Here is my code 这是我的代码

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;
}

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. 有没有其他方法可以使用FTPClient比较文件修改日期,或者任何人都可以指出我正在做错的方向。 Thanks. 谢谢。

Instead of your "lastFileModified" method, I would create a Comparator. 而不是你的“lastFileModified”方法,我会创建一个比较器。 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());
    }
}

Then, getting the "last" FTPFile is much easier: 然后,获取“最后”FTPFile更容易:

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

To come back to your problem: the "lastModified" timestamp is not linked to the FTP upload order. 要回到您的问题:“lastModified”时间戳未链接到FTP上载顺序。 When you upload a file through the FTP protocol, the original timestamp of the file may be preserved. 通过FTP协议上载文件时,可能会保留文件的原始时间戳。

So, if file1 is older than file2, your method will always return file2, even if file2 is uploaded before file1 on the FTP server. 因此,如果file1比file2旧,则即使file2在FTP服务器上的file1之前上载,您的方法也将始终返回file2。

I think that it is impossible to determine the last uploaded file. 我认为无法确定上次上传的文件。 This information is not stored by the FTP protocol. FTP协议不存储此信息。 You can do that only if you overload the "put" method of your FTP client: 只有在重载FTP客户端的“put”方法时才能这样做:

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

I see only one mistake: 我只看到一个错误:

FTPFile choice = null;

If the first file were the latest modified file, then the method would return null , causing a potential NullPointerException . 如果第一个文件是最新的修改文件,则该方法将返回null ,从而导致潜在的NullPointerException

Change it to 将其更改为

FTPFile choice = files[0];

and the logic should be right. 逻辑应该是正确的。

If it still doesn't return the expected file, then most likely the file in question simply doesn't have the expected last modified date. 如果它仍然没有返回预期的文件,那么很可能该文件根本没有预期的最后修改日期。 Add something like this to the for loop in the method: 在方法中的for循环中添加类似这样的内容:

System.out.println(file.getTimestamp().getTime() + " - " + file.getName());

And look closer. 仔细看看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM