关于FtpClient的defaultTimeout、connectTimeout、dataTimeout的区别

来源:互联网 发布:新发行的人工智能基金 编辑:程序博客网 时间:2024/06/07 21:11

在使用ftp中,往往为了程序不至于卡死,会设置timeout来保证程序正常运行。而FtpClient是一个非常实用的辅助类。好了,废话不多说。三个timeout的区别如下:

(1)defaultTimeout:默认超时,看源码可知,他是建立一个socket但还没调用connect()方法前才有效。可理解为建立socket的超时。

(2)connectTimeout:连接超时,可理解为调用connect()方法的超时。

(3)dataTimeout:数据超时,这个有点坑,一开始以为是发送文件的超时。看了源码的解释才知道,原来它是一个读超时。即从Ftp读数据的超时限制。源码如下:

  /**
     * 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 ≥ 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.
     */
    public void setDataTimeout(int timeout)
    {
        __dataTimeout = timeout;
    }



原创粉丝点击