9

ftp サービスがアクティブでない既存のホストで使用ftpClient.connectする場合、タイムアウトは 5 分後にしか発生しません。これは長すぎます。

何も変わらないさまざまなタイムアウト(setDefaultTimeout、setDataTimeout)を設定してみました。

FtpClientSocketClientsetConnectTimeout メソッドを継承していますが、これを使用するとjava.lang.NoSuchMethodError: org/apache/commons/net/ftp/FTPClient.setConnectTimeout、実行時に a が取得されます。これは、Commons-net FAQ で説明されているように、J2SW 1.2 との互換性が原因のようです。

Q: 接続タイムアウトを設定するにはどうすればよいですか? http://wiki.apache.org/commons/Net/FrequentlyAskedQuestions

SocketFactory彼らは、特定のタイムアウトを使用して、拡張された Socket クラスからオブジェクトを作成する独自の実装を提案しています。ただし、使用しようとするftpClient.setSocketFactoryと、java.lang.NoSuchMethodError.

接続タイムアウトを減らす方法はありますか?

4

4 に答える 4

11
    FTPClient ftp = new FTPClient();

    ftp.setDefaultTimeout();
    ftp.setDataTimeout();
    ftp.setConnectTimeout();
    ftp.setSoTimeout();
    ftp.setControlKeepAliveTimeout();
    ftp.setControlKeepAliveReplyTimeout();

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)
于 2017-02-10T16:47:16.323 に答える
4

存在するため、setConnectTimeout を呼び出す方法にある必要があります。setConnectTimeout は静的呼び出しではありません。FTPClient オブジェクトを割り当てた後に呼び出し、接続前に設定を行う必要があります。

FTPClient ftp = new FTPClient();
ftp.setConnectTimeout(5000); // 5000 Milliseconds (5 Seconds)
... 
ftp.connect(server, port); 
于 2012-06-11T13:45:58.713 に答える
3

その古いバージョンのCommonsNetライブラリには可能な解決策がありますが、間違ったバージョンのCommonsNetが使用されている理由を理解することをお勧めします。FTPClientこれを行うには、Webアプリで使用されている場所に次のコードを含めることができます。

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
}

FTPClientそれまでにロードされていない場合はjava.net.URLClassLoader、クラスパスのチェックがより複雑になる可能性がありますが、問題にはなりません。

お役に立てれば...

于 2012-06-13T12:50:41.640 に答える