Interview Questions

FTP4j - File download using Listener

Arun's picture

In the previous section, we have seen how to upload file using FTP Listener. Let us see how to perform FTP file download using Listener implementation.

1. FTP Utility class
Refer the previous article for the implementation of this class

2. FTP Listener class
Refer the previous article for the implementation of this class

3. FTP - File Download with Listener

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

package in.techdive.upload;

import it.sauronsoftware.ftp4j.FTPClient;

import java.io.File;

/**
 * This class downloads the file from the remote FTP Server to the local machine using FTP4j library. While downloading
 * the files, it registers the FTP Listener, which gets notified with the status of the FTP download operation once the
 * download completes/aborts.
 */

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

                // Variable 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 downloaded
                                        String filePath = "D:/Sample.zip";
                                        File fileDownload = new File(filePath);
                                        System.out.println("Downloading the " + filePath + " to the Local machine");

                                        // Download the file
                                        client.download("Sample.zip", fileDownload, new MyFTPListener());
                                        System.out.println("Successfully Downloaded the " + filePath + " file from the Remote Machine");
                                }
                                catch (Exception e)
                                {
                                        System.err.println("ERROR : Error in downloading file from 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);
        }
}