Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Commons FTPClient.listFiles

Tags:

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();
like image 429
Shyam Avatar asked Feb 26 '10 06:02

Shyam


People also ask

What is FTPClient in Java?

FTPClient encapsulates all the functionality necessary to store and retrieve files from an FTP server. This class takes care of all low level details of interacting with an FTP server and provides a convenient higher level interface.

How do I import org Apache Commons Net FTPClient FTP?

Here's the official Apache download site for Commons Net. Once you've picked and extracted the appropriate version, set a dependency in your Eclipse project: Right-click the project and choose "Properties" Choose "Java Build Path" from the resulting popup.

Does Apache FTPClient support SFTP?

Apache FTPClient doesn't support SFTP at the moment.

How do I connect to an FTP server using Java?

To start using FTP with Java, you will need to create a new FTPClient and then connect and login to the server using . connect(String server, int port) and . login(String username, String password) .


2 Answers

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

Apache Commons Net FTPClient and listFiles()

like image 117
PapaFreud Avatar answered Sep 18 '22 18:09

PapaFreud


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

like image 45
Shyam Avatar answered Sep 19 '22 18:09

Shyam