JAVA Code for Getting List of Directories and its SubDirectories from FTP server

JAVA Code for Getting List of Directories and its SubDirectories from FTP server

**Hi. I am trying to get list of all directories and sub directories and its files from "FTP" and want to show in Jtree but i am unable to do it.Please Give me some code and also explain it. Thanks**

> Mayank Soni

View Answers

August 29, 2012 at 6:09 PM

public class FTPManager
{

    private static FTPManager ftpManager;


    private FTPManager(){}

    public static FTPManager getInstance()
    {
          if(ftpManager!=null)
          {
              return ftpManager; 
          }
           else
           {      ftpManager= new FTPManager ();
                   return ftpManager;
            }
    }


    public static void main(String[] args) throws Exception
    {
        FTPConnector ftpConnector = new FTPConnector("ftp server url", "username", "password");
        FTPClient ftpClient = ftpConnector.getFTPConnection();
        FTPManager ftpManager= FTPManager.getInstance();
        directoryManager.scanDirectoryTree(ftpClient);
    }

    private  void scanDirectoryTree(FTPClient ftpClient) throws IOException
    {
        FTPFile[] ftpFiles = ftpClient.listFiles();
        System.out.println("Files inside FTP Base");
        for (FTPFile ftpFile : ftpFiles)
        {
            if (ftpFile.isFile())
            {
                System.out.println("File Name :" +ftpFile.getName());               
            }
        }
        System.out.println("Directories and Subdirectories inside FTP Base");
        for (FTPFile ftpFile : ftpFiles)
        {
             if (ftpFile.isDirectory())
            {
                String dirName=ftpFile.getName();
                System.out.println("Directory Name :"+dirName);
                scanDirectoryTree(ftpClient, dirName);
            }
        }
    }

    private static void scanDirectoryTree(FTPClient ftpClient,String directory) throws IOException
    {   

       FTPFile[] ftpFiles =ftpClient.listFiles(directory);

        if (ftpFiles.length == 0) {
            System.out.println("no files in " + directory);
        }
        else
        {
            System.out.println("Files and sub directories inside the directory :"+directory);
        }
        for (FTPFile ftpFile : ftpFiles)
        {
            if (ftpFile.isFile())
            {
                System.out.println("File Name :"+directory+"\\" + ftpFile.getName());   
            }
        }
        for (FTPFile ftpFile : ftpFiles)
        {
            if (ftpFile.isDirectory())
            {
                String dirName=directory+"\\"+ftpFile.getName();
                scanDirectoryTree(ftpClient, dirName);
            }
        }

    }

}

August 29, 2012 at 6:19 PM

Hi Soni,

This will list out the files,directories and sub directories inside ftp.You can utilize this code for your reference.









Related Tutorials/Questions & Answers:
JAVA Code for Getting List of Directories and its SubDirectories from FTP server
JAVA Code for Getting List of Directories and its SubDirectories from FTP server  **Hi. I am trying to get list of all directories and sub directories and its files from "FTP" and want to show in Jtree but i am unable to do
FTP Server : List Files and Directories
In this tutorial we will discuss how to list files and directories on FTP server using java
Advertisements
List all directories on FTP Server
In this section you will learn how to list all the directories on FTP server using java
Java list directories
Java list directories In this section, you will learn how to display the list of directories from a particular directory. Description of code: As you all... used the methods of File class to retrieve the list of directories from
Implementing FTP in Java Code
Implementing FTP in Java Code  Hi, My job is to write a program in Java in my project. I have to implement FTP in my Java Code. Share me some of the code of Implementing FTP in Java Code. Thanks   Hi, Apache ftp
FTP Programming in Java tutorials with example code
and Directories Delete a file from FTP Server FTP Server : Remove... Get all file size on FTP Server List all directories on FTP... will learn how to create program in Java that connects to FTP server and upload
Java FTP
be downloaded from the apache website. View examples of Java FTP at: FTP Server...; Hello, See this also List all directories on FTP Server Thanks... share me the sample Java FTP code? Thanks   Hi, There are many FTP
Delete a file from FTP Server
In this section you will learn how to delete file from FTP server using java
Java Connect to and FTP Server
Java Connect to and FTP Server  What is the code for Java Connect to and FTP Server? How to establish a connection to FTP server in Java? Thansk... the apache commons library to connect/login to FTP server. Link of complete example
getting values from dropdown list
getting values from dropdown list  I am having a dropdown list which has hardcoded values ( we need it hardcoded only and will not be populated from... to the action. My action is getting called however, i am not sure how to pass
FTP Server : Download file
This tutorial contains description of file downloading from the FTP server using java
FTP Server: List all files name
This tutorial represents how to list the entire files name on FTP server using java
connect ftp from java program
connect ftp from java program  Hi, I need to connect to the ftp and get the files through a java program.. I wrote the following code... import... client = new FTPClient(); client.connect("ftp://111.111.111.111
connect ftp from java program
connect ftp from java program  Hi, I need to connect to the ftp and get the files through a java program.. I wrote the following code... import... client = new FTPClient(); client.connect("ftp://111.111.111.111
how to creat ftp server on java
how to creat ftp server on java  ftp server investigation requirment ER diagram flow chart source code
how to creat ftp server on java
how to creat ftp server on java  ftp server investigation requirment ER diagram flow chart source code
how to creat ftp server on java
how to creat ftp server on java  ftp server investigation requirment ER diagram flow chart source code
how to creat ftp server on java
how to creat ftp server on java  ftp server investigation requirment ER diagram flow chart source code
how to creat ftp server on java
how to creat ftp server on java  ftp server investigation requirment ER diagram flow chart source code
how to creat ftp server on java
how to creat ftp server on java  ftp server investigation requirment ER diagram flow chart source code
how to creat ftp server on java
how to creat ftp server on java  ftp server investigation requirment ER diagram flow chart source code
how to creat ftp server on java
how to creat ftp server on java  ftp server investigation requirment ER diagram flow chart source code
ftp server
ftp server  ftp server
ftp server
ftp server  ftp server
ftp server
ftp server  ftp server
FTP server
FTP server  How to store a series of files in a ftp server using java
upload and download files from ftp server using servlet - Ajax
upload and download files from ftp server using servlet  Hi,Sir... for upload and download files from ftp server using servlet and how to use... to disconnect from FTP server " + "after server refused connection. "+e.toString
Java FTP Library
Java FTP Library        The Java Ftp Library Let's imagine a situation where we want to write a pure Java application that must download files from
FTP Server
FTP Server  i'm working on ftp server if one client has uploaded a file i need to save the details of the user who has uploaded the file like username,filename & ipaddress using java
FTP Server
This tutorial contains concept of FTP with its features
FTP Java example
to write code for uploading and downloading the files from FTP server. My FTP... examples of FTP at: FTP Programming in Java tutorials with example code. Thaks...FTP Java example  Where is the FTP Java example on your website? I am
Getting environment variable value from java
Getting environment variable value from java  Getting environment variable value from java
Java FTP Client Example
Java FTP Client Example  How to write Java FTP Client Example code? Thanks   Hi, Here is the example code of simple FTP client in Java which downloads image from server FTP Download file example. Thanks
Getting Current Directory from java - Java Beginners
Getting Current Directory from java  I'm designing an application in linux where i need to get the current directory of my java file... I'm using System.getProperty("user.dir") but its not working properly It's returning /opt
FTP Server : Connect and Login
In this tutorial we will discuss how to connect and login in FTP server with java programming
FTP Server : Create Directory
In this section you will learn how to create directory on FTP server using java
FTP Server : Remove Directory
In this section you will learn how to delete directory on FTP server using java
FTP Server : Upload file
This tutorial contains description of file uploading to the FTP server using java
Java FTP Download
Java FTP Download  How to write a program in Java for downloading a file from FTP server? Tell me the good and easy to understand code for Java FTP Download. Thanks   Hi, The easiest way to connect to FTP server
Image uploading in FTP Server
Image uploading in FTP Server  I want to upload images to a ftp server every 1 hour automatically using java..please help
FTP File Upload in Java
easy to write code for FTP File Upload in Java. I am assuming that you have FTP... will be able to create programs in Java that uploads the file on FTP server... to FTP server and uploads the file. Here is the complete code of the program
how to copy the directory in to ftp server using java
how to copy the directory in to ftp server using java  how to copy the directory in to ftp server using java ?   Hello venaktehs As much..._TO_REPLACE_2 // Here "ftp" is the Object of Ftp ftp.setDir(directoryName); Now
Web Based FTP
, it is not necessary now to install FTP client software in order to download files from... the web based services using several web-based FTP front-end programs and java... as the web server and the FTP server perform all functions. The only
How to upload file on FTP Server
How to upload file on FTP Server  In my application there is a requirement of uploading the file on FTP server. How to upload file on FTP Server... is good and can be used in Java applications. Read FTP File Upload in Java for more
How to implement FTP using java
want to implement FTP using Java to transfer files and exchange files between FTP client and FTP server. Could anyone help me for How to implement FTP using java... tutorials with example code FTP Server : Connect and Login Apache FTP Server FTP
getting unique value from java.util.random - Java Beginners
getting unique value from java.util.random  i am doing a shopping... for that i used java.util.random to get random productid from product table, but the value which i am getting contains duplicate values and it is not unique, my
Java FTP Tutorial
and download files from FTP server on Internet. I have IP address, username and password of the FTP server. Now I have to learn FTP programming in Java fast...Java FTP Tutorial  I am new to FTP programming in Java. I have lots
Java FTP Connection
if it works? How to make Java FTP Connection to FTP server hosted in my network? Thanks   See the example code at FTP Server : Connect and Login...Java FTP Connection  I am learning to develop a program in Java
Java FTP Upload
in Java. View the FTP Server : Upload file example for complete code example...Java FTP Upload  Is there any example code for Java FTP Upload? Which is the easy to use Java FTP Upload example program at roseindia.net? Thanks
Java upload file to ftp
Java upload file to ftp  Hi, How to uploaded file to a ftp server... that uploads files on FTP server. You have to use the Apache FTP client library in your program. Apache FTP Client library can be downloaded from apache website

Ads