Question

I am trying to use apache commons to send manual FTP commands as I have to send non-standard FTP commands to a specific server (that accepts them)

Before I try to send these non-standard commands I want to get FTP working manually with commons.net.ftp. Unfortunately I seem to be missing something.

This works fine (i.e. it retrieves the list of files)

FTPClient ftp = new FTPClient();
FTPClientConfig config = new FTPClientConfig();
ftp.configure(config);

ftp.connect("ftp.mozilla.org");
ftp.login("anonymous", "");

ftp.enterLocalPassiveMode();
FTPFile[] fileList = ftp.listFiles("/");

This doesn't

FTPClient ftp = new FTPClient();
FTPClientConfig config = new FTPClientConfig();
ftp.configure(config);

ftp.connect("ftp.mozilla.org");
ftp.login("anonymous", "");

ftp.sendCommand("PASV");
ftp.sendCommand("NLST");

I get the appropriate response for ftp.sendCommand("PASV"); but it times out on ftp.sendCommand("NLST"); finally giving me

425 Failed to establish connection.

I have tried to research the topic but most advice on this error is for people setting up servers (and it's usually a firewall problem).

Why does it work when net.ftp does it, but not when I send the commands manually?

Was it helpful?

Solution

Sending the PASV command manually is not enough, the client has to open the data connection to the port specified by the server in response to the PASV command. This is performed by calling the enterLocalPassiveMode() method. Since sending PASV manually doesn't initialize the data connection you get an error shortly after.

See http://slacksite.com/other/ftp.html#passive for more details on the FTP protocol.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top