views:

902

answers:

3

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();
+1  A: 

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
Yes of course! :)
Shyam
A: 

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
+1  A: 

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

Shyam