Java FTP create directory example
- Details
- Written by Nam Ha Minh
- Last Updated on 19 July 2019   |   Print Email
boolean makeDirectory(String pathname)
pathname
is path of the directory to be created. Path can be relative or absolute. For example, the following statement:makeDirectory(“upload”)
makeDirectory(“/upload”)
makeDirectory(“/upload/projects/java”)
boolean created = makeDirectory("/upload/projects/java") if (created) { // the directory is created, everything is going well } else { // something unexpected happened... }
import java.io.IOException; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; public class FTPCreateDirDemo { private static void showServerReply(FTPClient ftpClient) { String[] replies = ftpClient.getReplyStrings(); if (replies != null && replies.length > 0) { for (String aReply : replies) { System.out.println("SERVER: " + aReply); } } } public static void main(String[] args) { String server = "www.yourserver.com"; int port = 21; String user = "username"; String pass = "password"; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); showServerReply(ftpClient); int replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) { System.out.println("Operation failed. Server reply code: " + replyCode); return; } boolean success = ftpClient.login(user, pass); showServerReply(ftpClient); if (!success) { System.out.println("Could not login to the server"); return; } // Creates a directory String dirToCreate = "/upload123"; success = ftpClient.makeDirectory(dirToCreate); showServerReply(ftpClient); if (success) { System.out.println("Successfully created directory: " + dirToCreate); } else { System.out.println("Failed to create directory. See server's reply."); } // logs out ftpClient.logout(); ftpClient.disconnect(); } catch (IOException ex) { System.out.println("Oops! Something wrong happened"); ex.printStackTrace(); } } }Compile and run the program:If you want to create a directory whose path like this: /projects/java/ftp/demo, but the parent directories do not exist, consult this article: Creating nested directory structure on a FTP server.Download latest version of Apache Commons Net library.
Related Java FTP Tutorials:
- Creating nested directory structure on a FTP server
- Determine if a directory or file exists on FTP server
- 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
Thank You Nam
When you upload a file, by default, it will be stored in the current directory (which is home directory of the logged user account).