gpt4 book ai didi

java - 为 FtpClient 设置连接超时

转载 作者:搜寻专家 更新时间:2023-10-31 19:34:30 25 4
gpt4 key购买 nike

当对没有 Activity ftp 服务的现有主机使用 ftpClient.connect 时,超时仅在 5 分钟后发生,这太长了。

我尝试设置不同的超时(setDefaultTimeout、setDataTimeout),但没有任何改变。

FtpClient 继承自 SocketClient,它有一个 setConnectTimeout 方法,但是当我使用它时,我得到一个 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

对我如何减少连接超时有什么帮助吗?

最佳答案

    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)

关于java - 为 FtpClient 设置连接超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10979911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com