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.
Ads
Related Tutorials/Questions & Answers:
Advertisements
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
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
:
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
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
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
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
FTP server How to store a series of files in a
ftp server using
java
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
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 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
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