Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1838368
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:28:06+00:00 2026-05-17T06:28:06+00:00

Trying to create a simple plugin that simply connects to an ftp site, looks

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T06:28:07+00:00Added an answer on May 17, 2026 at 6:28 am

    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! 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a simple page that enters data in to a
I'm playing with the ASP.NET MVC Framework, trying to create a simple site. My
I'm new to the CKEditor and I'm trying to create a simple plugin. What
I'm trying to create a simple application that is only supposed to be called
I am trying to create a simple dialog in MFC using Visual C++. My
I'm trying to create a simple BaSH-like grammar on ANTLRv3 but haven't been able
I'm trying to create a simple toggling sidebar using jquery, where it expands and
I am trying to create a rather simple effect on a set of images.
I'm new to postgreSQL and I have a simple question: I'm trying to create
Im using Eclipse 3.4, EclipseMe 1.7.9. Im trying to deploy/create package a simple project

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.