Change parent directory on ftp server

Ads
 

Change parent directory on ftp server

In this section you will learn how to change the parent directory on FTP server using java.

In this section you will learn how to change the parent directory on FTP server using java.

Change parent directory on ftp server

In this section you will learn how to change the parent directory on FTP server using java.

Change Parent Directory : FTPClient class provides method to replace the parent directory.

boolean changeToParentDirectory() : Working of this method is to replace the parent directory of the present working directory. It is of boolean type. Returns true if method completed properly otherwise returns false.

It throws FTPConnectionClosedException and IOException.

Example :

This example contains code to change the current working directory using client.changeWorkingDirectory(newDir) and change the parent directory using method client.changeToParentDirectory().

Here is program -

import java.io.IOException;

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

class FtpChangeParentDirectory {
	public static void main(String[] args) throws IOException {
		FTPClient client = new FTPClient();
		boolean result;
		try {
			// Connect to the localhost
			client.connect("localhost");

			// login to ftp server
			result = client.login("admin", "admin");
			if (result == true) {
				System.out.println("Successfully logged in!");
			} else {
				System.out.println("Login Fail!");
				return;
			}
			String newDir = "/filetest";

			// Changing working directory
			result = client.changeWorkingDirectory(newDir);

			if (result == true) {
				System.out.println("Working directory is changed. Your New working directory : "
								+ newDir);
			} else {
				System.out.println("Unable to change");
			}
			result = client.changeToParentDirectory();
			if (result == true) {
				System.out.println("Parent directory is changed.");
			} else {
				System.out.println("Unable to change");
			}
		} catch (FTPConnectionClosedException e) {
			e.printStackTrace();
		} finally {
			try {
				client.disconnect();
			} catch (FTPConnectionClosedException e) {
				System.out.println(e);
			}
		}

	}
}

Output :

Successfully logged in!
Working directory is changed. Your New working directory : /filetest
Parent directory is changed.

Ads

Tutorials   

Ads