Using FTP Proxy with apache commons-net
-
12-09-2019 - |
Question
I want to set up an FTP connection using a proxy server with Apache's commons-net.
But looking at this Does FTPClient support FTP connections through an FTP proxy server? has me worried.
I have to meddle with the system properties and the Sun docs state that "If socksProxyHost is specified then all TCP sockets will use the SOCKS proxy server to establish a connection or accept one."
WTH? All TCP sockets? What about my database connections? Or other FTP connections i might want to open at the same time not using a proxy? Will they all be affected?
Is there some other way to do it that doesn't mess with the rest of my application?
Solution
You have several ways of using proxies in Java, especially from version 1.5.
- Using System Properties: quick & powerfull but limited flexibility
- You can use use a SOCKS proxy for all TCP connections.
- You can also set a proxy per protocol, doable for HTTP, FTP and HTTPS
- For both methods, you can specify a list of hosts that will not use proxy
- Using the
java.net.Proxy
class (Java 1.5+) to set (or not) aProxy
perConnection
- Impleting a
java.net.ProxySelector
(idem) which will determine aProxy
for eachConnection
according to your criteria
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow