设置FtpClient的连接超时

str*_*cer 6 java ftp-client apache-commons-net

ftpClient.connect与没有活动ftp服务的现有主机一起使用时,超时仅在5分钟后发生,这太长了。

我尝试设置各种超时(setDefaultTimeout,setDataTimeout),但没有任何改变。

FtpClientSocketClient那里继承有一个setConnectTiemout方法,但是当我使用java.lang.NoSuchMethodError: org/apache/commons/net/ftp/FTPClient.setConnectTimeout它时,运行它时会得到一个。这似乎是由于某些J2SW 1.2兼容性,如Commons-net FAQ中所述:

问:如何设置连接超时?http://wiki.apache.org/commons/Net/FrequentlyAskedQuestions

他们建议SocketFactory使用特定的超时从扩展的Socket类实现自己的创建对象。但是,当尝试使用时,ftpClient.setSocketFactory我也会得到一个java.lang.NoSuchMethodError

有什么帮助我可以减少连接超时吗?

Ily*_*man 6

    FTPClient ftp = new FTPClient();

    ftp.setDefaultTimeout();
    ftp.setDataTimeout();
    ftp.setConnectTimeout();
    ftp.setSoTimeout();
    ftp.setControlKeepAliveTimeout();
    ftp.setControlKeepAliveReplyTimeout();
Run Code Online (Sandbox Code Playgroud)

从Apache文档:

   /**
     * Set the default timeout in milliseconds to use when opening a socket.
     * This value is only used previous to a call to
     * {@link #connect connect()}
     * and should not be confused with {@link #setSoTimeout setSoTimeout()}
     * which operates on an the currently opened socket.  _timeout_ contains
     * the new timeout value.
     * <p>
     * @param timeout  The timeout in milliseconds to use for the socket
     *                 connection.
     */
    void setDefaultTimeout(int timeout);


    /**
     * Sets the timeout in milliseconds to use when reading from the
     * data connection.  This timeout will be set immediately after
     * opening the data connection, provided that the value is &ge; 0.
     * <p>
     * <b>Note:</b> the timeout will also be applied when calling accept()
     * whilst establishing an active local data connection.
     * @param  timeout The default timeout in milliseconds that is used when
     *        opening a data connection socket. The value 0 means an infinite timeout.
     */
    void setDataTimeout(int timeout)
    /**
     * Sets the connection timeout in milliseconds, which will be passed to the {@link java.net.Socket} object's
     * connect() method.
     * @param connectTimeout The connection timeout to use (in ms)
     * @since 2.0
     */
    void setConnectTimeout(int connectTimeout);
    /**
     * Set the timeout in milliseconds of a currently open connection.
     * Only call this method after a connection has been opened
     * by {@link #connect connect()}.
     * <p>
     * To set the initial timeout, use {@link #setDefaultTimeout(int)} instead.
     *
     * @param timeout  The timeout in milliseconds to use for the currently
     *                 open socket connection.
     * @exception SocketException If the operation fails.
     * @throws NullPointerException if the socket is not currently open
     */
    void setSoTimeout(int timeout) throws SocketException;
    /**
     * Set the time to wait between sending control connection keepalive messages
     * when processing file upload or download.
     *
     * @param controlIdle the wait (in secs) between keepalive messages. Zero (or less) disables.
     * @since 3.0
     * @see #setControlKeepAliveReplyTimeout(int)
     */
    void setControlKeepAliveTimeout(long controlIdle);

    /**
     * Set how long to wait for control keep-alive message replies.
     *
     * @param timeout number of milliseconds to wait (defaults to 1000)
     * @since 3.0
     * @see #setControlKeepAliveTimeout(long)
     */
    void setControlKeepAliveReplyTimeout(int timeout)
Run Code Online (Sandbox Code Playgroud)


Yur*_*nyy 3

尽管对于旧版本的 Commons Net 库有可能的解决方案,但我建议弄清楚为什么使用了错误版本的 Commons Net。为此,您可以将以下代码添加到FTPClient您的网络应用程序中使用的位置:

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
}
Run Code Online (Sandbox Code Playgroud)

如果FTPClient此时尚未加载java.net.URLClassLoaderif ,则检查类路径可能会变得更加复杂,但这应该不是问题。

希望这可以帮助...

  • 嗯,这很奇怪。从未使用过 weblogic,但看起来以下问题/答案接近您所需要的:http://stackoverflow.com/questions/3376046/weblogic-10-3-1-0-is-using-com-bea- core-apache-commons-net-1-0-0-0-1-4-1-jar (2认同)