使用 Apache FTPClient,我通常可以使用以下语句进行连接:
FTPClient client = new FTPClient();
client.connect("ftp.myhost.com");
client.login("myUsername", "myPassword");
client.changeWorkingDirectory("/fileFeed");
client.setFileType(FTPClient.BINARY_FILE_TYPE);
client.setFileTransferMode(FTPClient.BLOCK_TRANSFER_MODE);
以上工作正常,但现在我要连接到 FTP 站点,我必须使用代理服务器。我得到的指示是我应该连接到代理服务器并在用户名中指定实际的 ftp 服务器。因此,要登录,我将使用以下详细信息进行连接:
ftp ftp.myProxyServer.com
username myUsername@ftp.myhost.com
password myPassword
我尝试使用命令提示符直接连接,我可以连接到 ftp.myProxyServer.com 主机,如果我指定 myUsername@ftp.myhost.com 作为主机用户名,它会将我转发到预期的 ftp 站点。问题在于使用 Apache FTPClient 的 Java 不接受上述类型的连接:
FTPClient client = new FTPClient();
client.connect("ftp.myProxyServer.com");
client.login("myUsername@ftp.myhost.com", "myPassword");
client.changeWorkingDirectory("/fileFeed");
client.setFileType(FTPClient.BINARY_FILE_TYPE);
client.setFileTransferMode(FTPClient.BLOCK_TRANSFER_MODE);
有什么我遗漏的或者上面的方法不起作用吗?我尝试了直接连接,效果很好。
最佳答案
FTPClient 通常处于主动 ftp 模式,如果您的代理无法启动返回到您的客户端计算机的 tcp 连接(出于防火墙/DMZ 原因),那么您必须切换到被动模式:
FTPClient client = new FTPClient();
client.connect("ftp.myProxyServer.com");
client.login("myUsername@ftp.myhost.com", "myPassword");
client.enterLocalPassiveMode(); //switch from active to passive
client.changeWorkingDirectory("/fileFeed");
...
(此外,我建议始终检查方法调用的返回码,但为了清楚起见,可能会省略它们)
很抱歉这么晚才回答您的问题...
关于java - 使用 Apache FTPClient 通过代理连接到 FTP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13992759/