日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java ftp 连接超时时间_ftpClient的连接超时设置(setConnectTimeout,setSoTimeout) | 学步园...

發布時間:2024/8/23 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java ftp 连接超时时间_ftpClient的连接超时设置(setConnectTimeout,setSoTimeout) | 学步园... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

從 ftpClient的官方網的FAQ里面看到,實現這個需要用一個 自定義的SocketFactory

然后實現里面的 createSocket方法,有好多個。

http://wiki.apache.org/jakarta-commons/Net/FrequentlyAskedQuestions

原文如下:

Q: How can I set a connection timeout?

A: setDefaultTimeout does not set the connect

timeout. It provides a default socket timeout. Only in J2SE 1.4 was the

ability to specify a timeout on connect added to the Socket API. Since

Commons Net 1.2.x has a J2SE 1.2 compatibility requirement, the ability

to specify a connect timeout is not included. The way to workaround

this is to implement your own SocketFactory and set it with SocketClient.setSocketFactory (FTPClient is a subclass of SocketClient). When you implement the SocketFactory,

add a setConnectTimeout method or some such. Inside of the createSocket

methods, use the J2SE 1.4 connect method with the timeout. We could

actually provide socket factory that subclasses DefaultSocketFactory

to do this without breaking backward compatibility, but that would have

to be discussed further. The way to do it is to compile it only if J2SE

>= 1.4 is being used. SocketClient could check for

availability of the J2SE 1.4 connect method and instantiate the J2SE

>= 1.4 factory if available (using Class.forName and newInstance).

The setDefaultTimeout method could then be changed to also set the

connect timeout in the new factory if being used. If users want this

functionality enough, the best chance of getting it implemented soon is

to submit a patch.

不過我看了 ftpClient 2.0的源代碼。

ftpClient.connect(hostName, 21);

看看 connect的代碼如下

public void connect(String hostname, int port)

throws SocketException, IOException

{

_socket_= _socketFactory_.createSocket();

_socket_.connect(new InetSocketAddress(hostname, port), connectTimeout);

_connectAction_();

}

其中有一個connectTimeout正是我們需要的連接超時,我們看看它的定義

/** The socket's connect timeout (0 = infinite timeout) */

private static final int DEFAULT_CONNECT_TIMEOUT = 0;

protected int connectTimeout = DEFAULT_CONNECT_TIMEOUT;

可見默認是不限超時的。再搜索一下,找到如下這個方法

/**

* Sets the connection timeout in milliseconds, which will be passed to the {@link Socket} object's

* connect() method.

* @param connectTimeout The connection timeout to use (in ms)

* @since 2.0

*/

public void setConnectTimeout(int connectTimeout) {

this.connectTimeout = connectTimeout;

}

/**

* Get the underlying socket connection timeout.

* @return

* @since 2.0

*/

public int getConnectTimeout() {

return connectTimeout;

}

可見,我們完全可以直接設置超時時間就行了。具體代碼如下:

FTPClient ftpClient = new FTPClient();

ftpClient.setConnectTimeout(1000); // 一秒鐘,如果超過就判定超時了

ftpClient.connect(hostName, 21);

估計2,0這個版本對這個問題進行了完善和增強,可以這么簡單的實現了。

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的java ftp 连接超时时间_ftpClient的连接超时设置(setConnectTimeout,setSoTimeout) | 学步园...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。