Java FTP example - Change working directory
- Details
- Written by Nam Ha Minh
- Last Updated on 19 July 2019   |   Print Email
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:
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
The FTPClient class also has another useful method to change to parent directory of the working directory: changeToParentDirectory()
Related Java FTP Tutorials:
- Creating nested directory structure on a FTP server
- Java FTP example - Calculate total sub directories, files and size of a directory
- Determine if a directory or file exists on FTP server
- Remove an empty directory on a FTP server
- Rename file or directory on FTP server
Other Java FTP Tutorials:
- Connect and login to a FTP server
- 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
EXCELLENT
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.
Thanks