Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Commons Net FTPClient and listFiles()

Can anyone explain me what's wrong with the following code? I tried different hosts, FTPClientConfigs, it's properly accessible via firefox/filezilla... The problem is I always get empty filelist without any exceptions (files.length == 0). I use commons-net-2.1.jar installed with Maven.

    FTPClientConfig config = new FTPClientConfig(FTPClientConfig.SYST_L8);      FTPClient client = new FTPClient();     client.configure(config);      client.connect("c64.rulez.org");     client.login("anonymous", "anonymous");     client.enterRemotePassiveMode();      FTPFile[] files = client.listFiles();     Assert.assertTrue(files.length > 0); 
like image 690
Vladimir Mihailenco Avatar asked Apr 26 '10 11:04

Vladimir Mihailenco


People also ask

What is FTPClient in Java?

FTPClient encapsulates all the functionality necessary to store and retrieve files from an FTP server. This class takes care of all low level details of interacting with an FTP server and provides a convenient higher level interface.

How do I import org Apache Commons Net FTPClient FTP?

Here's the official Apache download site for Commons Net. Once you've picked and extracted the appropriate version, set a dependency in your Eclipse project: Right-click the project and choose "Properties" Choose "Java Build Path" from the resulting popup.

Does Apache FTPClient support SFTP?

Apache FTPClient doesn't support SFTP at the moment.


2 Answers

Found it!

The thing is you want to enter passive mode after you connect, but before you log in. Your code returns nothing for me, but this works for me:

import org.apache.commons.net.ftp.FTPClient; import java.io.IOException; import org.apache.commons.net.ftp.FTPFile;  public class BasicFTP {      public static void main(String[] args) throws IOException {         FTPClient client = new FTPClient();         client.connect("c64.rulez.org");         client.enterLocalPassiveMode();         client.login("anonymous", "");         FTPFile[] files = client.listFiles("/pub");         for (FTPFile file : files) {             System.out.println(file.getName());         }     } } 

Gives me this output:

 c128 c64 c64.hu incoming plus4 
like image 153
PapaFreud Avatar answered Oct 06 '22 05:10

PapaFreud


Only using enterLocalPassiveMode() did not work for me.

I used following code, which worked.

    ftpsClient.execPBSZ(0);     ftpsClient.execPROT("P");     ftpsClient.type(FTP.BINARY_FILE_TYPE); 

Complete example is as below,

    FTPSClient ftpsClient = new FTPSClient();              ftpsClient.connect("Host", 21);      ftpsClient.login("user", "pass");      ftpsClient.enterLocalPassiveMode();      ftpsClient.execPBSZ(0);     ftpsClient.execPROT("P");     ftpsClient.type(FTP.BINARY_FILE_TYPE);      FTPFile[] files = ftpsClient.listFiles();      for (FTPFile file : files) {         System.out.println(file.getName());     } 
like image 34
suketup Avatar answered Oct 06 '22 06:10

suketup