BC OnLine Tech Team
BC OnLine Tech Team

Reputation: 53

Apache Commons Net FTPClient Will Not Execute listFiles()

I'm modifying some code that was previously working with an FTPS library I wrote myself. I've been asked to start using the Apache Commons Net library (FTPClient and FTPSClient mainly) and I'm running into problems doing a file listing. I've read other questions and it's not the enterLocalPassiveMode problem (Apache Commons Net FTPClient and listFiles()), as I'm using that after connecting, but before logging in. The same code works fine on a test server I set up (also using Apache FTP), but doesn't work on the server I need it for.

I've also tried using the "PBSZ 0" and "PROT P" commands, but they're not implemented on the remote system.

502 PBSZ Command not implemented.
502 PROT Command not implemented.

Code:

FTPSClient ftpsclient = new FTPSClient(true); // Implicit SSL
ftpsclient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
ftpsclient.connect(HOST_ADDR, HOST_PORT); // Using port 990
ftpsclient.enterLocalPassiveMode();
ftpsclient.user(USERID);
ftpsclient.pass(PASSWORD);
ftpsclient.setFileType(FTP.BINARY_FILE_TYPE);
ftpsclient.changeWorkingDirectory(REMOTE_DIR);
ftpsclient.printWorkingDirectory();
FTPFile[] ftpfiles = ftpsclient.listFiles(); // This is where it breaks

I've tried specifying the directory explicitly and using the default:

FTPFile[] ftpfiles = ftpsclient.listFiles();
FTPFile[] ftpfiles = ftpsclient.listFiles(REMOTE_DIR);

... but both give the same result. This is the output of the debugging info:

220 FTPS (Version  Thu Dec 10 17:23:00 2015) server ready.
USER ****
331 Password required for ****.
PASS ****
230 User **** logged in.
TYPE I
200 Type set to I
CWD outbound/directory
250 CWD Command successful.
PWD
257 "/usr/path/to/outbound/directory" is current directory.
SYST
215 UNIX
PASV
227 Entering Passive Mode (XX,XX,XX,XX,24,140) ***Edit: port 6284
LIST
150 Opening data connection for '/bin/ls'.

Then it times out after 30 seconds with this stack trace:

Stack Trace: org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
    at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:317)
    at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:294)
    at org.apache.commons.net.ftp.FTP.getReply(FTP.java:692)
    at org.apache.commons.net.ftp.FTPClient.completePendingCommand(FTPClient.java:1813)
    at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3308)
    at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3271)
    at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2930)

I've checked my firewall settings and that host is allowed to connect via port 990 and 6200-6300.

I've also read FTPClient.listFiles not working and Java application hanging during LIST command to FTP Server (Apache Commons FTPClient) and neither of these have helped with my problem.

EDIT: It looks as though FTPClient is not recognizing the data channel. I tried uploading a file instead of doing a listing, and it died after the "150 Opening data connection" message. I've confirmed that the ports assigned to the data connection from the PASV command are NOT blocked by our firewall.

Any ideas?

Upvotes: 2

Views: 2955

Answers (1)

BC OnLine Tech Team
BC OnLine Tech Team

Reputation: 53

I fixed it. Turns out I was doing a directory listing with an invalid path. Instead of returning an error message, Apache FTP closed the connection. Not sure why. Anyway, it's working now.

Upvotes: 1

Related Questions