In this post, I will guide you how to write Java code that downloads only structure (excluding files) of a remote directory on a FTP server, using Apache Commons Net API.

There would be a case in which we just want to download structure of a directory from a FTP server, excluding the files. That means creating a copy structure the remote directory on the local computer, not downloading remote files.

Based on the article How to download a complete folder from a FTP server, with some slightly modifications we have a cut-down version of the utility class as follows:

import java.io.File;
import java.io.IOException;

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

/**
 * This utility class provides a method that downloads a structure
 * of a directory (excluding files) from a FTP server, using
 * Apache Commons Net API.
 *
 * @author www.codejava.net
 *
 */
public class FTPDownloadUtil {

	/**
	 * Download structure of a directory from a FTP server.
	 * @param ftpClient an instance of org.apache.commons.net.ftp.FTPClient class.
	 * @param parentDir Path of the parent directory of the current directory being
	 * downloaded.
	 * @param currentDir Path of the current directory being downloaded.
	 * @param saveDir path of directory where the whole remote directory will be
	 * downloaded and saved.
	 * @throws IOException if any network or IO error occurred.
	 */
	public static void downloadDirStructure(FTPClient ftpClient, String parentDir,
			String currentDir, String saveDir) 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 the directory itself
					continue;
				}

				if (aFile.isDirectory()) {
					String newDirPath = saveDir + parentDir + File.separator
						+ currentDir + File.separator + currentFileName;
					if (currentDir.equals("")) {
						newDirPath = saveDir + parentDir + File.separator
						+ currentFileName;
					}

					// create the directory in saveDir
					File newDir = new File(newDirPath);
					boolean created = newDir.mkdirs();
					if (created) {
						System.out.println("CREATED the directory: " + newDirPath);
					} else {
						System.out.println("COULD NOT create the directory: " + newDirPath);
					}

					// download the sub directory
					downloadDirStructure(ftpClient, dirToList, currentFileName,
							saveDir);
				}
			}
		}
	}
}

 

And here is code of a test program:

import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;

public class DownloadDirectoryStructureTest {

	public static void main(String[] args) {
		String server = "www.codejava.net";
		int port = 21;
		String user = "username";
		String pass = "password";

		FTPClient ftpClient = new FTPClient();

		try {
			// connect and login to the server
			ftpClient.connect(server, port);
			ftpClient.login(user, pass);

			// use local passive mode to pass firewall
			ftpClient.enterLocalPassiveMode();

			System.out.println("Connected");

			String remoteDirPath = "/Test";
			String saveDirPath = "E:\\Download";

			FTPDownloadUtil.downloadDirStructure(ftpClient, remoteDirPath, "",
					saveDirPath);

			// log out and disconnect from the server
			ftpClient.logout();
			ftpClient.disconnect();

			System.out.println("Disconnected");
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}

 

Compile the utility class and the test program:

javac -cp commons-net-VERSION.jar;. DownloadDirectoryStructureTest.java

Run the test program:

java -cp commons-net-VERSION.jar;. DownloadDirectoryStructureTest

Suppose the directory /Test on the FTP server has the following structure:

directory structure to download

Then the test program produces the following output:

output of download directory structure program

 

NOTES:Download the latest distribution of the Apache Commons Net library here.

 

Related Java FTP Tutorials:

 

Other Java FTP Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.

Attachments:
Download this file (DownloadDirectoryStructureTest.java)DownloadDirectoryStructureTest.java[Test program]0.9 kB
Download this file (FTPDownloadUtil.java)FTPDownloadUtil.java[Utility class]2 kB

Add comment