使用commons.net.ftp手动FTP

时间:2013-03-19 10:12:45

标签: java ftp apache-commons-net

我正在尝试使用apache commons发送手动FTP命令,因为我必须将非标准FTP命令发送到特定服务器(接受它们)

在我尝试发送这些非标准命令之前,我想通过commons.net.ftp手动使用FTP。不幸的是,我似乎错过了一些东西。

这很好用(即检索文件列表)

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("/");

这不是

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");

我得到ftp.sendCommand("PASV");的相应回复,但是ftp.sendCommand("NLST");最终给了我

425 Failed to establish connection.

我试图研究这个主题,但是关于这个错误的大多数建议都是针对人们设置服务器的(而且通常是防火墙问题)。

为什么net.ftp会这样做呢,而不是我手动发送命令时呢?

1 个答案:

答案 0 :(得分:0)

手动发送PASV命令是不够的,客户端必须打开与服务器指定的端口的数据连接以响应PASV命令。这是通过调用enterLocalPassiveMode()方法执行的。由于手动发送PASV不会初始化数据连接,因此很快就会出现错误。

有关FTP协议的详细信息,请参阅http://slacksite.com/other/ftp.html#passive