When using ftpClient.connect
with an existing host who has no ftp service active, timeout occurs only after 5 minutes, which is much too long.
I tried setting diverse timeouts (setDefaultTimeout, setDataTimeout) which did not change anything.
FtpClient
inherits from SocketClient
which has a setConnectTimeout method, but when I use this I get a java.lang.NoSuchMethodError: org/apache/commons/net/ftp/FTPClient.setConnectTimeout
when running it. This seems to be because of some J2SW 1.2 compatibility, as described in Commons-net FAQ:
Q: How can I set a connection timeout? http://wiki.apache.org/commons/Net/FrequentlyAskedQuestions
They suggest to implement an own SocketFactory
creating objects from an extended Socket class using a specific timeout. However, when trying to use ftpClient.setSocketFactory
I also get a java.lang.NoSuchMethodError
.
Any help how I can reduce the connect timeout?
Since Commons Net 1.2.x has a J2SE 1.2 compatibility requirement, the ability to specify a connect timeout is not included.
meaning that Commons Net 1.2.x libraries doesn't have desired functionality. Do you need specifically that old version of library? Otherwise, try newer or newest version of it e.g. Commons Net 3.1 - thereFTPClient
class hassetConnectTimeout
method doing exactly what you need as mentioned in the answer(s) below. – Ravennapublic static void main(String[] args)
method from under Eclipse? Maybe the problem is in application server: it may provide older version of Commons Net library and your deployed application may use FTPClient from there ... – Ravenna