0

I am unable to get the exact file list using FTPClient. Sample code as below :

FTPClient client = new FTPClient();
client.connect("x.x.x.x");
client.login("abcd", "abcd");
FTPFile[] ftpFiles = client.listFiles();
for (FTPFile ftpFile : ftpFiles) {
   System.out.println("FTPFile: " + ftpFile.getName());
}

I tried to set to PASV mode using enterLocalPassiveMode()/enterRemotePassiveMode()/pasv(). But, it doesnt work.

Please also check Apache Commons FTPClient.listFiles ..

Thank you

Community
  • 1
  • 1
Raghu
  • 11
  • 1
  • 3
  • 6
  • You'll need to be more descriptive about the problem. What happens when you run the code? Can you login? Do you get some of the data? None of it? – Eldad Mor Jan 10 '11 at 09:33
  • Please explain, what you *expect* and what you currently *observe* (iaw - what's missing on your result list) – Andreas Dolk Jan 10 '11 at 09:33
  • Hi Andreas, I am able to login and view the list of files but they are incomplete eg. if there are 5 files, I am able to view only couple of them. – Raghu Jan 10 '11 at 09:55

2 Answers2

2

I don't know what files is, but you're getting the results of client.listFiles in ftpFiles, and not in files. Then in your for loop you go over files.

Eldad Mor
  • 5,405
  • 3
  • 34
  • 46
  • Sorry typoerror.. But, my problem is incomplete file list. eg. if "ls" views me 4 files, using listfiles() I get a result of 1/2 files only. – Raghu Jan 10 '11 at 10:00
  • Did you try several folders? Maybe the problem is not in your code. What files are missing? Anything special about them? – Eldad Mor Jan 10 '11 at 12:01
  • The files are normal txt files with different file extn. When I use other FTP client programs it displays all the files properly. But my code displays only few of them. – Raghu Jan 11 '11 at 07:53
  • Can you do the same test on a different folder with different files? – Eldad Mor Jan 11 '11 at 09:02
  • Yes I tried with another folder .. It is unpredictable sometimes lists exact all files sometimes it doesnt.. BTW one observation is fewer files it lists properly but if the directory contains more than 20 or so.. some files are missing then. – Raghu Jan 12 '11 at 04:46
  • An anonymous user suggested _use ftpclient.listnames then you can get names of the files in the server_ – mplungjan Dec 13 '12 at 13:08
1


Try this.

String[] fileFtp = client.listNames();//if it is directory. then list of file names

//download file
for (int i =0;i<fileFtp.length;i++) {

   String fileName = fileFtp[i];

   OutputStream out = new FileOutputStream(new File("local temp file name"));

   if (!client.retrieveFile(fileName, out)) {       
        sysout("Could not download the file. "+ fileName);
    } else {
        sysout("Downloaded file @ : "+localFileName);   
    }       
} 

This should work.
Thanks.

Nikunj
  • 3,100
  • 2
  • 20
  • 19