In the article Create a directory on FTP server, we provided an example of making a new directory on the FTP server like this:

ftpClient.makeDirectory("/projects/java/ftp/demo");

That will create the directory “demo” under the parent “/projects/java/ftp” with an assumption that the parent directory exists before. If not, the server will return this error message:

550 Can't create directory: No such file or directory

So we are going to tackle this issue by developing the following utility class:

import java.io.IOException;

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

/**
 * This utility class provides a method that creates a nested directory
 * structure on a FTP server, based on Apache Commons Net library.
 * @author www.codejava.net
 *
 */
public class FTPUtil {
	/**
	 * Creates a nested directory structure on a FTP server
	 * @param ftpClient an instance of org.apache.commons.net.ftp.FTPClient class.
	 * @param dirPath Path of the directory, i.e /projects/java/ftp/demo
	 * @return true if the directory was created successfully, false otherwise
	 * @throws IOException if any error occurred during client-server communication
	 */
	public static boolean makeDirectories(FTPClient ftpClient, String dirPath)
			throws IOException {
		String[] pathElements = dirPath.split("/");
		if (pathElements != null && pathElements.length > 0) {
			for (String singleDir : pathElements) {
				boolean existed = ftpClient.changeWorkingDirectory(singleDir);
				if (!existed) {
					boolean created = ftpClient.makeDirectory(singleDir);
					if (created) {
						System.out.println("CREATED directory: " + singleDir);
						ftpClient.changeWorkingDirectory(singleDir);
					} else {
						System.out.println("COULD NOT create directory: " + singleDir);
						return false;
					}
				}
			}
		}
		return true;
	}
}

The method makeDirectories() traverses every single elements of the path structure tries to change current working directory to the current path element:

    • If the operation succeeded, that means the directory exists, continue with the next element.
    • If the operation failed, that means the directory does not exist, so create the directory.

So that would result in many directories in the structure created at once. And here is a test program:

import java.io.IOException;

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

/**
 * This is a test program that demonstrates creating a nested directory
 * structure on a FTP server.
 * @author www.codejava.net
 *
 */
public class FTPMakeNestedDirectoryTest {

	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 dirPath = "/projects/java/ftp/demo/connect";

			FTPUtil.makeDirectories(ftpClient, dirPath);

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

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

Remember to change the server, user and pass according to your FTP account.

Compile the utility class and the test program as follows:

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

Run the test program:

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

Output:

output of test program to create nested directory

Reference: Download Apache Commons Net library

 

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 (FTPMakeNestedDirectoryTest.java)FTPMakeNestedDirectoryTest.java[Test program]1 kB
Download this file (FTPUtil.java)FTPUtil.java[Utility class]1 kB

Add comment

   


Comments 

#6Faisal2021-07-12 04:40
Perfectly Work!

Big Thanks!
Quote
#5yer2018-11-26 00:07
gracias, me ayudo mucho
Quote
#4Jorge2016-02-24 16:08
how to put limiete FTP directories created ?
Quote
#3Nam2015-12-28 01:56
Quoting cairuzhang:
i am sorry i think i had make a mistake.sorry,plz forget the former comments.


No problem, happy coding!
Quote
#2cairuzhang2015-12-27 21:27
i am sorry i think i had make a mistake.sorry,plz forget the former comments.
Quote