The article List files and directories on a FTP server describes the Apache Commons NET’s FTP API for listing files and directories of the current working directory or a specific one on a FTP server. All the methods listFiles() and listDirectories() list only files and directories which are directly under the directory being listed, not all sub files and sub directories nested in the directory’s hierarchical structure.
The algorithm to list all nested sub files and directories is as follows:
The following method implements the algorithm described above:
static void listDirectory(FTPClient ftpClient, String parentDir, String currentDir, int level) throws IOException { String dirToList = parentDir; if (!currentDir.equals("")) { dirToList += "/" + currentDir; } FTPFile[] subFiles = ftpClient.listFiles(dirToList); if (subFiles != null && subFiles.length > 0) { for (FTPFile aFile : subFiles) { String currentFileName = aFile.getName(); if (currentFileName.equals(".") || currentFileName.equals("..")) { // skip parent directory and directory itself continue; } for (int i = 0; i < level; i++) { System.out.print("\t"); } if (aFile.isDirectory()) { System.out.println("[" + currentFileName + "]"); listDirectory(ftpClient, dirToList, currentFileName, level + 1); } else { System.out.println(currentFileName); } } } }
The method has 4 parameters:
There are some notes about the implementation of the method above:
And following is a sample program that connects, logins to lists a particular directory on a FTP server. After that it logs out and disconnects from the server.
import java.io.IOException; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; public class FTPListRecursiveDemo { static void listDirectory(FTPClient ftpClient, String parentDir, String currentDir, int level) throws IOException { String dirToList = parentDir; if (!currentDir.equals("")) { dirToList += "/" + currentDir; } FTPFile[] subFiles = ftpClient.listFiles(dirToList); if (subFiles != null && subFiles.length > 0) { for (FTPFile aFile : subFiles) { String currentFileName = aFile.getName(); if (currentFileName.equals(".") || currentFileName.equals("..")) { // skip parent directory and directory itself continue; } for (int i = 0; i < level; i++) { System.out.print("\t"); } if (aFile.isDirectory()) { System.out.println("[" + currentFileName + "]"); listDirectory(ftpClient, dirToList, currentFileName, level + 1); } else { System.out.println(currentFileName); } } } } public static void main(String[] args) { String server = "www.myserver.com"; int port = 21; String user = "username"; String pass = "password"; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); int replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) { System.out.println("Connect failed"); return; } boolean success = ftpClient.login(user, pass); if (!success) { System.out.println("Could not login to the server"); return; } String dirToList = "/public_html/images/articles"; listDirectory(ftpClient, dirToList, "", 0); } catch (IOException ex) { System.out.println("Oops! Something wrong happened"); ex.printStackTrace(); } finally { // logs out and disconnects from server try { if (ftpClient.isConnected()) { ftpClient.logout(); ftpClient.disconnect(); } } catch (IOException ex) { ex.printStackTrace(); } } } }
When running, the program’s output looks like the following screenshot:
You can download the sample program source code in the attachment section at the end of this article.
NOTE:
a