Share on Google+Share on Google+

Mayank Soni
JAVA Code for Getting List of Directories and its SubDirectories from FTP server
2 Answer(s)      4 years and 11 months ago
Posted in : Java Beginners

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

Ads

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
List all directories on FTP Server
In this section you will learn how to list all the directories on FTP server using java
Advertisements
FTP Server : List Files and Directories
In this tutorial we will discuss how to list files and 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
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
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
FTP Server: List all files name
This tutorial represents how to list the entire files name on FTP server using java
FTP Programming in Java tutorials with example code
: Change working directory FTP Server : List Files and Directories... size on FTP Server List all directories on FTP Server FTP... will learn how to create program in Java that connects to FTP server and upload
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
Delete a file from FTP Server
In this section you will learn how to delete file from FTP server using java
FTP Server : Download file
This tutorial contains description of file downloading from the FTP server using java
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
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
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
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
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
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...("MKD"+directoryName); Change the working directory : // Here "ftp" is the Object
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
FTP Server : Create Directory
In this section you will learn how to create directory on FTP server using java
Tutorials   
Java Spring Hibernate Struts Training What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized how to convert war file into .exe file using java code

Ads

 
Advertisement null

Ads