Interview Questions

FTP4j - File Upload

Arun's picture

In this section, Let us see how to upload file from local directory to the remote FTP Server.

Perform the below steps to upload files to remote FTP Server.
1. Get the FTP Client object using IP address, username and password of the remote FTP server.
2. Create the File object to upload

File fileUpload = new File ("C:\sample.txt");

3. Use the below FTP4j api to upload files.
ftpClient.upload(fileUpload); // This will upload the local file to the remote server

FTP - File Upload

/**
    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;

import java.io.File;

/**
 * This class uploads file in the local machine to the remote FTP Server using FTP4j client library
 */

public class FTP_FileUpload
{
        public static void main(String[] args)
        {
                /* Check for input data */
                if (args.length != 3)
                {
                        System.err.println("Usage: java FTP_FileUpload " + "<IpAddress> <UserName> <Password>");
                        System.err.println("Example: java FTP_FileUpload 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)
                        {
                                try
                                {
                                        //Define the File with complete path to be uploaded
                                        String filePath = "sample.tar";
                                        File fileUpload = new File(filePath);
                                        System.out.println("Uploading the " + filePath + " to Remote Machine");
                                       
                                        //Upload the file
                                        client.upload(fileUpload);
                                        System.out.println("Successfully Uploaded the " + filePath + " File to Remote Machine");
                                }
                                catch (Exception e)
                                {
                                        System.err.println("ERROR : Error in Transfering File to Remote Machine");
                                        System.exit(3);
                                }
                        }
                }
                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);
        }
}