Java FTP Create nested directory structure on FTP server
- Details
- Written by Nam Ha Minh
- Last Updated on 19 July 2019   |   Print Email
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.
- Change the current working directory to the newly created one. This is important, so that the next operation will be based on this current 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:
Reference: Download Apache Commons Net library
Related Java FTP Tutorials:
- Determine if a directory or file exists on FTP server
- Java FTP create directory example
- Java FTP example - Change working directory
- Remove an empty directory on a FTP server
- Rename file or directory on FTP server
Other Java FTP Tutorials:
- Java FTP list files and directories example
- Java FTP file download tutorial and example
- Java FTP file upload tutorial and example
- Java FTP delete file example
- Java FTP example - Search for files and directories
- How to upload a directory to a FTP server
- How to download a complete folder from a FTP server
Comments
Big Thanks!
No problem, happy coding!