Interview Questions

FTP4j - File Upload with Listener

Arun's picture

In the previous article, we have seen how to upload files from local directory to the remote FTP server. The previous example will try to upload the file to FTP server and doesn’t know the status of the FTP upload operation. For an error free application or system, we should know the status of the file upload or file download, only then we can retry the same FTP operation.

In this section, Let us see how to get the status of the File upload operation using Listener implementation. This can be easily achieved using the below steps.

1. Create a class named MyFTPListener which implements FTPDataTransferListener interface and implement the respective methods as described in the below sample code.
2. Upload the locale file as,

ftpClient.upload(file_to_be_Uploaded, new MyFTPListener());

3. Once the File upload is completed, the method completed() in MyFTPListener class gets executed. If the FTP operation fails, then failed() method in the same class gets executed.

Consider the below set of classes which does the above file upload operation with listeners.

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

package in.techdive.upload;
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;
                }
        }
}

2. FTP Listener 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.
 */

package in.techdive.upload;

import it.sauronsoftware.ftp4j.FTPDataTransferListener;

public class MyFTPListener implements FTPDataTransferListener
{

        public void started()
        {
                // Transfer started
                System.out.println("TRANSFER-STATUS: File transfer started...");
        }

        public void transferred(int length)
        {
                // Yet other length bytes has been transferred since the last time this
                // method was called
        }

        public void completed()
        {
                // Transfer completed
                System.out.println("TRANSFER-STATUS: File transfer completed...");
        }

        public void aborted()
        {
                // Transfer aborted
                System.err.println("TRANSFER-STATUS: File transfer aborted...");
        }

        public void failed()
        {
                // Transfer failed
                System.err.println("TRANSFER-STATUS: File transfer failed...");
        }
}

3. FTP - File Upload 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 uploads the file from the local machine to the remote FTP Server using FTP4j library. While uploading the
 * files, it registers the FTP Listener, which gets notified with the status of the FTP upload operation.
 */

public class FTP_FileUpload_WithListener
{
        public static void main(String[] args)
        {
                /* Check for input data */
                if (args.length != 3)
                {
                        System.err.println("Usage: java FTP_FileUpload_WithListener " + "<IpAddress> <UserName> <Password>");
                        System.err.println("Example: java FTP_FileUpload_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 uploaded
                                        String filePath = "C:/Sample.zip";
                                        File fileUpload = new File(filePath);
                                        System.out.println("Uploading the " + filePath + " to Remote Machine");
                                       
                                        //Upload the file
                                        client.upload(fileUpload, new MyFTPListener());
                                        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);
        }
}