While working with a FTP server using Apache Commons Net API, it’s important to keep in mind that many operations are working based on the working directory (or current directory), such as create directories, upload files, download files, … Remember this rule:

    •           Pathname that starts with slash “/”, is considered as absolute path.
    •           Pathname that does not start with a slash is considered as relative path.
The FTPClient class provides the following method to change working directory: 

boolean changeWorkingDirectory(String pathname) 

where pathname represents path of the directory to change, which can be either absolute or relative. For example, the following statement: 

changeWorkingDirectory(“/upload”) 

will change the current working directory to the upload directory under server’s root directory, regardless of what previous working directory is.

But this statement: 

changeWorkingDirectory(“upload”) 

will change the current working directory to the upload directory relative to the previous working directory.

And you should check return value of the method, which is true if the server changed the directory successfully, and false otherwise.

The following example program logins to a FTP server, changes working directory to “upload” and logs out. Full source code:

package net.codejava.ftp;

import java.io.IOException;

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

/**
 * This program demonstrates how to change current working directory
 * from a FTP client program using Apache Commons Net API.
 * @author www.codejava.net
 */
public class FTPChangeDirDemo {

	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("Connect failed");
				return;
			}

			boolean success = ftpClient.login(user, pass);
			showServerReply(ftpClient);

			if (!success) {
				System.out.println("Could not login to the server");
				return;
			}

			// Changes working directory
			success = ftpClient.changeWorkingDirectory("/upload");
			showServerReply(ftpClient);

			if (success) {
				System.out.println("Successfully changed working directory.");
			} else {
				System.out.println("Failed to change working directory. See server's reply.");
			}

			// logs out
			ftpClient.logout();
			ftpClient.disconnect();

		} catch (IOException ex) {
			System.out.println("Oops! Something wrong happened");
			ex.printStackTrace();
		}
	}

	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);
			}
		}
	}
}
Compile and run the program:



compile and run FTP change dir program

If operation succeeded, the server replies a message:

250 OK. Current directory is /upload

In case the directory does not exist, this message is responded:

550 Can't change directory to /upload: No such file or directory

cannot change dir

The FTPClient class also has another useful method to change to parent directory of the working directory: changeToParentDirectory()

 

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 (FTPChangeDirDemo.java)FTPChangeDirDemo.java[Demo program]1 kB

Add comment

   


Comments 

#4Dave2021-01-18 09:21
thank you very much.
Quote
#3Vladi2020-08-24 06:08
Thank you very much.
EXCELLENT
Quote
#2Jane2019-03-01 16:08
I think this article is misleading as the absolute path of the change work directory is not from the "server's root directory" but the user's root directory.
I also would like to know if there is any api from the ftpclient lib changes the directory to the "server's root directory" as chroot does.
Quote
#1Amith2015-07-15 23:11
This was helpful!!
Thanks
Quote