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:
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:
Reference: Download Apache Commons Net library