Search code examples
javaftp-clientapache-commons-net

Setting connect timeout for FtpClient


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?


Solution

  • Though there is possible solution for that older version of Commons Net library, I suggest to figure out why wrong version of Commons Net is used. To do this you may include following code to the place where FTPClient is used in your webapp:

    FTPClient ftpClient = ...;
    if(ftpClient.getClass().getClassLoader() instanceof java.net.URLClassLoader) {
        URL[] urls = ((java.net.URLClassLoader) ftpClient.getClass().getClassLoader()).getURLs();
        // log these urls somewhere and check - these are urls from where your FTPClient may be loaded from
    }
    

    In case if FTPClient is loaded not by java.net.URLClassLoader then it may get more complicated to check the classpath, though it should not be a problem.

    Hope this helps...