FTP4j - List Files in Remote FTP Server

In this section, the following Java FTP client connects to the remote server and lists all the files in the remote directory.

The below api has to be used for listing files in the remote directory.

FTPFile[] fileArray = ftpClient.list();

Java FTP – List Files

/**
    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 it.sauronsoftware.ftp4j.FTPFile;

/**
 * This class connects to the remote Server using FTP protocol and lists all the file inside the directory
 */

public class FTP_ListFiles
{
        public static void main(String[] args)
        {
                /* Check for input data */
                if (args.length != 3)
                {
                        System.err.println("Usage: java FTP_ListFiles " + "<IpAddress> <UserName> <Password>");
                        System.err.println("Example: java FTP_ListFiles 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)
                        {
                                /* List all file inside the directory */
                                FTPFile[] fileArray = client.list();
                               
                                System.out.println("List of files...");
                                for (int i = 0; i < fileArray.length; i++)
                                {
                                        FTPFile file = fileArray[i];

                                        if (file != null)
                                        {
                                                if (file.TYPE_FILE == FTPFile.TYPE_FILE) // File
                                                {
                                                        System.out.println("File Name = " + file.getName() + " ; File Size = " + file.getSize()
                                                        + " ;Modified Date = " + file.getModifiedDate());
                                                }
                                                else if (file.TYPE_DIRECTORY == FTPFile.TYPE_DIRECTORY) // Directory
                                                {
                                                        System.out.println("Directory Name = " + file.getName() + " ; Directory Size = "
                                                        + file.getSize() + " ;Modified Date = " + file.getModifiedDate());
                                                }
                                                else if (file.TYPE_LINK == FTPFile.TYPE_LINK) // Link
                                                {
                                                        System.out.println("Link Name = " + file.getName() + " ;Modified Date = "
                                                        + file.getModifiedDate());
                                                }
                                        }
                                }
                        }
                }
                catch (Exception e)
                {
                        System.err.println("ERROR : Error in Connecting to Remote Machine... Hence exitting...");
                        // e.printStackTrace();
                        System.exit(2);
                }

                finally
                {
                        try
                        {
                                client.disconnect(true);
                        }
                        catch (Exception e)
                        {
                        }
                }
                System.exit(0);
        }
}

Technology: 

Search