Interview Questions

FTP4j – Basic FTP Operations

Arun's picture

In this article, Let us see how to perform the basic FTP operations using FTP4J Client library.

Establish Remote FTP Connection :
Create the FTP Client object (as ftpClient) using Remote IP address, FTP Username and password as below.

FTPClient ftpClient = new FTPClient();
ftpClient.connect(ipAddress);
ftpClient.login(userName, password);

Basic FTP Operations :
The basic FTP operations covered in this section are as below.

1. Change to another directory

ftpClient.changeDirectory(remote_directory_name)

2. Rename a directory

ftpClient.rename (remote_directory_name, new_remote_directory_name)

3. Delete a directory

ftpClient.deleteDirectory (remote_directory_name)

4. Create a directory

ftpClient.createDirectory (new_remote_directory_name)

The below Java FTP client program connects to the remote FTP server and does the following operations.

FTP Utility Class

/**
    Copyright (C) 2011 TECHDIVE.IN

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
 */

import it.sauronsoftware.ftp4j.FTPClient;

public class FTPUtility
{
        public static FTPClient connect(String ipAddress, String userName, String password)
        {
                FTPClient client = new FTPClient();
                System.out.println("Connecting to " + ipAddress + " as " + userName + "/" + password);
                try
                {
                        client.setType(FTPClient.TYPE_BINARY);
                        client.connect(ipAddress);
                        client.login(userName, password);
                        return client;
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                        return null;
                }
        }
}

FTP Operations Class

 /**
    Copyright (C) 2011 TECHDIVE.IN

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
 */

import it.sauronsoftware.ftp4j.FTPClient;

/**
 * This class connects to the remote Server using FTP protocol and performs the below operations.
 * 1. Changes the Directory
 * 2. Renames a Directory
 * 3. Deletes a Directory
 * 4. Create a Directory
 */

public class FTP_Operations
{
        public static void main(String[] args)
        {
                /* Check for input data */
                if (args.length != 3)
                {
                        System.err.println("Usage: java FTP_Operations " + "<IpAddress> <UserName> <Password>");
                        System.err.println("Example: java FTP_Operations 1.2.3.4 other other");
                        System.exit(1);
                }

                // Assignment
                String ipAddress = args[0];
                String userName = args[1];
                String password = args[2];

                System.out.println("Ip Address = " + ipAddress);
                System.out.println("User = " + userName);
                System.out.println("Pass = " + password);

                // FTP Program operations start from here
                FTPClient client = null;
                try
                {
                        // Get the FTP Connection from the Utility class
                        client = FTPUtility.connect(ipAddress, userName, password);

                        if (client != null)
                        {
                                /* Get the current directory */
                                String currentDirectory = client.currentDirectory();
                                System.out.println("Current Working directory = " + currentDirectory);

                                /* Change the directory */
                                client.changeDirectory("dir-1"); // This operation is similar to "cd test"

                                /* Rename a file */
                                client.rename("dir-1", "dir-4"); //Renames the directory "dir-1" to "dir-4"

                                /* Delete a file */
                                // client.deleteDirectory("dummy_dir");
                                // client.deleteFile("dummy_file");

                                /* Create a directory */
                                client.createDirectory("new_dir_created"); //creates a new directory named as "new_dir_created"
                        }
                }
                catch (Exception e)
                {
                        System.err.println("ERROR : Error in Connecting to Remote Machine");
                        System.exit(1);
                }

                finally
                {
                        if (client != null)
                        {
                                try
                                {
                                        client.disconnect(true);
                                }
                                catch (Exception e)
                                {
                                        System.err.println("ERROR : Error in disconnecting the Remote Machine");
                                }
                        }
                }
                System.exit(0);
        }
}