20

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();
Shyam Kumar Sundarakumar
  • 5,649
  • 13
  • 42
  • 69
  • I've encountered the same issue. Thanks for posting this. – James P. Apr 05 '10 at 02:05
  • Possible duplicate of [Apache Commons Net FTPClient and listFiles()](http://stackoverflow.com/questions/2712967/apache-commons-net-ftpclient-and-listfiles) – approxiblue Mar 07 '17 at 22:34

7 Answers7

20

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

Apache Commons Net FTPClient and listFiles()

Community
  • 1
  • 1
PapaFreud
  • 3,636
  • 4
  • 34
  • 45
6

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

Shyam Kumar Sundarakumar
  • 5,649
  • 13
  • 42
  • 69
3

I added client.enterLocalPassiveMode() and it works:

client.connect("xxx.com");
boolean login = client.login("xxx", "xxx");
client.enterLocalPassiveMode();
Kamil Nękanowicz
  • 6,254
  • 7
  • 34
  • 51
1

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.

Paul
  • 4,812
  • 3
  • 27
  • 38
1

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.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • 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 Jan 07 '17 at 23:37
1

I had to same problem and it turned out to be that it couldn't parse what the server was returning for a file listing. I this line after connecting to the ftp server ftpClient.setParserFactory(new MyFTPFileEntryParserFactory());

public class MyFTPFileEntryParserFactory implements FTPFileEntryParserFactory {
private final static FTPFileEntryParser parser = new UnixFTPEntryParser() {
    @Override public FTPFile parseFTPEntry(String entry) {
        FTPFile ftpFile = new FTPFile();
        ftpFile.setTimestamp(getCalendar(entry));
        ftpFile.setSize(get(entry));
        ftpFile.setName(getName(entry));
        return ftpFile;
    }
};

@Override public FTPFileEntryParser createFileEntryParser(FTPClientConfig config) throws ParserInitializationException {
    return parser;
}

@Override public FTPFileEntryParser createFileEntryParser(String key) throws ParserInitializationException {
    return parser;
}

}

Lisa
  • 129
  • 1
  • 3
  • 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. – Noumenon Jan 08 '17 at 17:52
1

In my case, on top of applying enterLocalPassiveMode and indicating correct operation system, I also need to set UnparseableEntries to true to make the listFile method work.

FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
conf.setUnparseableEntries(true);
f.configure(conf);
boolean isLoginSuccess = client.login(username, password);
heinels
  • 189
  • 1
  • 6