tags:

views:

34

answers:

2

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.

A: 

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.

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:

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

And look closer.

BalusC
Same result, adding the time in the system output shows Wed Sep 29 15:28:00 EDT 2010, which is the same time the FTP shows 9/29/2010 3:28:00 PM. The latest file is actually 9/29/2010 7:23:00 PM and there is about 50 files in between those dates.
Ryan
So, you're comparing the times at different places (in Java and in some FTP manager tool)? Did you check the timestamp of all files inside the `for (FTPFile file : files)` loop as suggested in my answer? The FTP manager tool may utilize a different timezone.
BalusC
This started out by me just displaying the file name so I could double check to see it was the correct file before I continued anything, low and behold this happened. The times are all coming in the same into the code. Visually when you output the time and then compare to what you see visually with the file the it's the same time, just one is in military time, the other isn't. But in the code they are all the same values.
Ryan
A: 

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! 
}
Benoit Courtine
Yeah the comparator didn't work either, I'll research the FTP client to see what that is doing, thanks for the information.
Ryan
the original timestamp _may_ be preserved.
Thorbjørn Ravn Andersen
It's true: it depends on the FTP client. I edited my post to include your precision. Thanks a lot.
Benoit Courtine