7

I am trying to list all the files under a specific directory in a ftp server.

FTPFile[] subFiles = ftpClient.listFiles("directory");

Although the directory is a valid one , but the code gets stuck while calling listFiles , what may be the reason. ? Further i would like to mention that a seperate netbeans project accessing the same FTP server is working fine with the same code , but a maven project is having the problem. please help.

Raja
  • 305
  • 2
  • 4
  • 14
  • Eich's answer worked for me. And I deeply suspect you are doing something wrong Raja as you stated 1.4.1 was the current version but that version was released in 2005, 3.2 was the current version the day of your comment. – TedTrippin Aug 27 '13 at 17:03

3 Answers3

11

Try to use passive mode. I assume that you are using the newest commons net library (you didn't write which lib you are using).


Next approach, try to change the file list layout. The commons lib uses auto-detection but in some cases this doesn't work. You can change (and test) another file list layout as followed:

FTPClientConfig configuration = new FTPClientConfig(FTPClientConfig.TEST_YOURSELF);

FTPClient yourClient = FTPClient(...);
client.configure(conf);
Eich
  • 3,728
  • 1
  • 21
  • 34
  • Yes , I am using passive mode before login . The apache commons net is version 1.4.1 , which is the latest one. – Raja Apr 03 '13 at 15:04
  • Can you access (and list files) with your ftp software (like [filezilla](https://filezilla-project.org//))? Have you also tried `listFiles("/directory")`? – Eich Apr 03 '13 at 15:21
  • Using filezilla I can see the files , and yes in ftpClient.listFiles("directory") , i have tried all possible cases , have taken the root path ("/") and have also tried with a folder inside the root ftp directory ( "/AnyFolder"). – Raja Apr 03 '13 at 15:41
  • I've edited my answer, maybe if you change the file list layout you can get the list of files. – Eich Apr 03 '13 at 15:47
  • **This is what i did..still not working** `System.out.println("Enter Testing..");` `ftpClient.enterLocalPassiveMode(); FTPClientConfig conf = new` `FTPClientConfig(FTPClientConfig.SYST_UNIX);` `conf.setServerLanguageCode("fr"); ftpClient.configure(conf);` `FTPFile[] subFiles = ftpClient.listFiles("/");` `System.out.println("Exit Testing");` – Raja Apr 03 '13 at 16:14
  • Where is your `ftpClient.login(...)`? – Eich Apr 03 '13 at 16:45
  • The initial login configuration has been done in a seperate function , the listing one was being called therein , using ftpClient as one of the parameters. – Raja Apr 03 '13 at 18:40
  • Modified the ques http://stackoverflow.com/questions/15809593/ftp-connection-timed-out-421-when-running-project-via-maven – Raja Apr 04 '13 at 11:32
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27559/discussion-between-raja-and-eich) – Raja Apr 04 '13 at 11:32
  • 2
    I just added a call to enterLocalPassiveMode after connect and it solved my problem. I'm using commons-net 3.3. – TedTrippin Aug 27 '13 at 17:00
  • 1
    Same problem here. I had enterLocalPassiveMode() call BEFORE connect(), while it was supposed to be AFTER. thanks – Kirill Yunussov Dec 27 '16 at 17:29
3

To add to above answers the enterLocalPassiveMode() method should be called after connect() and before login(). Any other way I couldn't get mine to work. This testing was based on another answer as specified here: https://stackoverflow.com/a/5183296/11971304

            client.connect(host, port);
            client.enterLocalPassiveMode();
            if (!client.login(username, password)) {
                throw new LoginException("wrong credentials");
            }

mahee96
  • 705
  • 6
  • 15
0

I have same issue, with same source code, it's running fine when I run it via IDE (IntelliJ), but when run in server, it always return no file while trying to call ftpClient.listFiles()

My fix is active Local Passive Mode after connect to FTP server.

ftpClient.connect("FTP_server_address");
ftpClient.enterLocalPassiveMode();
Tinh Cao
  • 67
  • 6