2012-12-21 50 views
3

使用Apache FTPClient的代理连接到FTP服务器,我可以用下面的语句通常连接:通过使用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 [email protected] 
password myPassword 

我试图直接使用命令提示符连接,我可以连接到ftp.myProxyServer.com主机和它向前我预期的ftp站点如果我指定[email protected]作为主机用户名。问题是,上述类型的连接在Java中使用Apache FTPClient不被接受:

FTPClient client = new FTPClient(); 
client.connect("ftp.myProxyServer.com"); 
client.login("[email protected]", "myPassword"); 
client.changeWorkingDirectory("/fileFeed"); 
client.setFileType(FTPClient.BINARY_FILE_TYPE); 
client.setFileTransferMode(FTPClient.BLOCK_TRANSFER_MODE); 

有什么我失踪或者将上述不工作?我尝试了直接连接,并且工作正常。

+0

'client.connect(“ftp.myProxyServer.com”);'此功能连接到FTP服务器,而不是连接到代理服务器。 – Disa

+0

Apache VFS如何? http://commons.apache.org/vfs/api.html – theMarceloR

+1

更改所使用的FTP库不是一个真正的选择。 – ziggy

回答

0

的FTPClient通常是活跃 FTP模式,并且如果您的代理无法启动TCP连接返回到客户端计算机(防火墙/ DMZ原因),那么你必须切换到被动模式:

FTPClient client = new FTPClient(); 
client.connect("ftp.myProxyServer.com"); 
client.login("[email protected]", "myPassword"); 
client.enterLocalPassiveMode(); //switch from active to passive 
client.changeWorkingDirectory("/fileFeed"); 
... 

(此外,我想推荐给经常检查方法调用的返回代码,但可能他们ommitted为清楚起见)

对不起,我尝试末回答你的问题。 ..