Apache Commons FTPClient.listFiles

42,501

Solution 1

This seems like the same issue I had (and solved), see this answer:

Apache Commons Net FTPClient and listFiles()

Solution 2

After I set the mode as PASV it is working fine now! Thanks for all your efforts and suggestions!

Solution 3

I added client.enterLocalPassiveMode() and it works:

client.connect("xxx.com");
boolean login = client.login("xxx", "xxx");
client.enterLocalPassiveMode();

Solution 4

Just a silly suggestion... can you do a listing on the /uploads folder using a normal FTP client. I ask this because some FTP servers are setup to not display the listing of an upload folder.

Solution 5

First, make sure the listing works in other programs. If so, one possibility is that the file listing isn't being parsed correctly. You can try explicitly specifying the parser to use with initiateListParsing.

Share:
42,501
Shyam Kumar Sundarakumar
Author by

Shyam Kumar Sundarakumar

I am a technologist working on Linux / Java. I love exploring and exploiting Open Source Software.

Updated on July 09, 2022

Comments

  • Shyam Kumar Sundarakumar
    Shyam Kumar Sundarakumar almost 2 years

    I am using org.apache.commons.net.ftp.FTPClient in one of my applications to work with a FTP server. I am able to connect, login, pwd and cwd. However, when I try to list the files it doesn't return the list of files in that directory, where I know for sure that there are files. I am using the method FTPFile[] listFiles(), it returns an empty array of FTPFile.

    Please find below the code snippet where I am trying this:

            String hostname = properties.getProperty("FTP_SERVER");
            String user = properties.getProperty("FTP_USER");
            String passwd = properties.getProperty("FTP_PASSWD");
            FTPClient client = new FTPClient();
            client.connect(hostname);
            client.login(user, passwd);
            String reply = client.getStatus();
            System.out.println(reply);
            client.enterRemotePassiveMode();
            client.changeWorkingDirectory("/uploads");
            FTPFile[] files = client.listFiles();
            System.out.println(files.length);
            for (FTPFile file : files) {
                System.out.println(file.getName());
            }
    
            String[] fileNames = client.listNames();
            if (fileNames != null) {
                for (String file : fileNames) {
                    System.out.println(file);
                }
            }
            client.disconnect();
    
  • Noumenon
    Noumenon over 7 years
    Yes -- for example, the FTPFileListParser implentation that's passed to listFilemight assume lines must be a certain length, then miss all the files on a server with a different implementation of the LIST command.
  • Noumenon
    Noumenon over 7 years
    Similarly, the code I was looking at called a custom parser every time instead of allowing the auto-detect parser when the system type was normal Unix.