从ftp获取最新的文件 [英] get latest file from ftp

查看:114
本文介绍了从ftp获取最新的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图创建一个简单的插件,只需连接到一个FTP站点,查找最新的文件,然后下载它。但是,它没有得到最新的文件。



我使用org.apache.commons.net.ftp.ftpclient作为一切。



这里是我的代码

  public static void main(String [] args)
{
FTPClient client = new FTPClient();
尝试
{
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自动生成的catch块
e.printStackTrace();

catch(IOException e)
{
// TODO自动生成的catch块
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();
}
}
返回选项;
}

它获取文件列表,然后返回一个文件,它只是isn最新的文件。是否有任何其他方式来比较文件修改日期使用FTPClient或任何人都可以指出我在做什么错了方向。感谢。

解决方案

不是你的lastFileModified方法,我会创建一个Comparator。编写排序方法会更容易:

  public class LastModifiedComparator实现比较器< FTPFile> {

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


$ / code>

然后,获得最后FTPFile就容易多了:

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






要回到您的问题:lastModified时间戳未链接到FTP上载订单。当您通过FTP协议上传文件时,文件的原始时间戳可能会被保留。



因此,如果file1早于file2,那么您的方法将始终返回file2 ,即使file2在FTP服务器上的file1之前上传。



我认为确定最后上传的文件是不可能的。此信息不是由FTP协议存储的。
只有当您重载FTP客户端的put方法时才可以这样做:

  public void put文件文件){
//上传代码
FTPFile ftpFile = getJustUploadedFile(file);
ftpFile.setTimestamp(new Calendar()); //现在!
}


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.

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

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. Thanks.

解决方案

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

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

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. When you upload a file through the FTP protocol, the original timestamp of the file may be preserved.

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

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

这篇关于从ftp获取最新的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆