How do I get list of files from FTP server?

This example demonstrate how to retrieve list of files from FTP server. First we create an instance of org.apache.commons.net.ftp.FTPClient. Connect to the FTP server and login with your username and password.

The listFiles() method of the FTPClient return the list of filenames contained in the current working directory. null if the list could not be obtained. If there are no filenames in the directory, a zero-length array is returned.

package org.kodejava.example.commons.net;

import org.apache.commons.io.FileUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import java.io.IOException;

public class FTPListDemo {
    public static void main(String[] args) {
        FTPClient client = new FTPClient();

        try {
            client.connect("ftp.example.org");
            client.login("admin", "admin123**");

            if (client.isConnected()) {
                // Obtain a list of filenames in the current working
                // directory. When no file found an empty array will
                // be returned.
                String[] names = client.listNames();
                for (String name : names) {
                    System.out.println("Name = " + name);
                }

                FTPFile[] ftpFiles = client.listFiles();
                for (FTPFile ftpFile : ftpFiles) {
                    // Check if FTPFile is a regular file
                    if (ftpFile.getType() == FTPFile.FILE_TYPE) {
                        System.out.printf("FTPFile: %s; %s%n",
                            ftpFile.getName(),
                            FileUtils.byteCountToDisplaySize(ftpFile.getSize()));
                    }
                }
            }
            client.logout();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                client.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Here is the example result of our code:

Name = .
Name = ..
Name = .data
Name = example.html
Name = Touch.dat
FTPFile: .data; 1 KB
FTPFile: examples.html; 1 bytes
FTPFile: Touch.dat; 0 bytes

Maven Dependencies

<!-- http://repo1.maven.org/maven2/commons-net/commons-net/3.6/commons-net-3.6.jar -->
<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>3.6</version>
</dependency>
<!-- http://repo1.maven.org/maven2/commons-io/commons-io/2.5/commons-io-2.5.jar -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>

How do I get the content of a directory?

In this example you’ll see how to read the list of files inside a directory. To get this functionality we can use the File.listFiles() method. This method return an array of File object which can be either an instance of file or directory.

package org.kodejava.example.io;

import java.io.File;
import java.io.FilenameFilter;

public class DirectoryContentExample {
    public static void main(String[] args) {
        File gamesDir = new File("D:/Games");

        // Get a list of file under the specified directory
        // above and return it as an abstract file object.
        File[] files = gamesDir.listFiles();

        // Iterates the content of gamesDir directory, print it
        // and check it whether it was a directory or a file.
        for (File file : files) {
            System.out.println(file + " is a "
                    + (file.isDirectory() ? "directory" : "file"));
        }

        // Here we also get the list of file in the directory but
        // return it just as an array of String.
        String[] strings = gamesDir.list();
        for (String file : strings) {
            System.out.println("File = " + file);
        }

        // Now we want to list the file in the directory but
        // we just want a file with a .doc extension. To do
        // this we first create a FilenameFilter which will
        // be given to the listFiles() method to filter the
        // listing process. The rule of filtering is
        // implemented in the accept() method of the
        // FilenameFilter interface.
        FilenameFilter filter = new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith(".doc");
            }
        };

        // Give me just a .doc files in your directory.
        File[] docs = gamesDir.listFiles(filter);
        for (File doc : docs) {
            System.out.println("Doc file = " + doc);
        }
    }
}

Here is the result of the program:

The File[] array returned:

D:\Games\AOE\ is a directory
D:\Games\Championship Manager 2007 is a directory
D:\Games\GameHouse is a directory
D:\Games\Sierra is a directory
D:\Games\testing.doc is a file
D:\Games\TTD is a directory

The String[] array returned:

File = AOE
File = Championship Manager 2007
File = GameHouse
File = Sierra
File = testing.doc
File = TTD

The File[] array using FilenameFilter result:

Doc file = D:\Games\testing.doc