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.