ziggy
ziggy

Reputation: 15876

Connecting to an FTP server via a proxy using Apache FTPClient

Using the Apache FTPClient, i can usually connect using the following statements:

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

The above works fine but now i am to connect to the FTP site, i have to use a proxy server. The instructions i got is that i should connect to the proxy server and specify the actual ftp server in the username. So to log on i would use the following details to connect:

ftp         ftp.myProxyServer.com
username    [email protected]
password    myPassword

I tried connecting directly using the command prompt and i can connect to the ftp.myProxyServer.com host and it does forward me to the intended ftp site if i specify [email protected] as the host username. The problem is that the above type of connection is not accepted in Java using 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);

Is there anything i am missing or would the above not work? I tried a direct connection and that works fine.

Upvotes: 3

Views: 4839

Answers (1)

Maksim
Maksim

Reputation: 521

The FTPClient usually is in active ftp mode, and in case your proxy is not able to initiate a tcp connection back to your client computer (for firewall/DMZ reasons) then you have to switch to passive mode:

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

(Furthermore I would like to recommend to always check the return codes of the method calls, but probably they are ommitted for sake of clarity)

Sorry for the late attempt to answer your question...

Upvotes: 1

Related Questions