The following example demonstrates how to make a directory on a FTP server using Apache Commons Net library.

To create a directory on the FTP server, use the following method of the FTPClient class:

boolean makeDirectory(String pathname)

where pathname is path of the directory to be created. Path can be relative or absolute. For example, the following statement:

makeDirectory(“upload”)

will create a directory “upload” relative to the current directory. But the following statement:

makeDirectory(“/upload”)

will create the “upload” directory under server’s root directory, because if the path name begins with a forward slash, it denotes an absolute path.

We can also specify a path structure like this:

makeDirectory(“/upload/projects/java”)

That will create the directory “java” under the “/upload/projects” directory which must exist.

The method returns true if the directory is created successfully, otherwise returns false. So it’s recommended to check return value of this method as follows: 

boolean created = makeDirectory("/upload/projects/java")

if (created) {

	// the directory is created, everything is going well

} else {

	// something unexpected happened...
}
 



Following is a sample program that logins to a FTP server and creates a directory called “upload123” under server’s root directory:

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:

compile and run FTP create directory 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:

 

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 (FTPCreateDirDemmo.java)FTPCreateDirDemmo.java[Java source code]1 kB

Add comment

   


Comments 

#10imad2020-12-15 03:47
hi, how i can create a directory in local server instead ftp server
Quote
#9imad2020-12-14 08:51
hi, how i can create a directory in local server instead ftp server
Quote
#8Luca2020-06-08 13:34
Amazing, many less work hours to do.
Thank You Nam
Quote
#7Abdul2019-03-22 04:24
Not Create new folder
Quote
#6Nam2013-06-24 05:17
So try to play with the path, e.g. using relative path (without the slash at beginning).
When you upload a file, by default, it will be stored in the current directory (which is home directory of the logged user account).
Quote