ftpClient的连接超时设置(setConnectTimeout,setSoTimeout)

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               
从 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 connecttimeout. It provides a default socket timeout. Only in J2SE 1.4 was theability to specify a timeout on connect added to the Socket API. SinceCommons Net 1.2.x has a J2SE 1.2 compatibility requirement, the abilityto specify a connect timeout is not included. The way to workaroundthis 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 createSocketmethods, use the J2SE 1.4 connect method with the timeout. We couldactually provide socket factory that subclasses DefaultSocketFactoryto do this without breaking backward compatibility, but that would haveto be discussed further. The way to do it is to compile it only if J2SE>= 1.4 is being used. SocketClient could check foravailability 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 theconnect timeout in the new factory if being used. If users want thisfunctionality enough, the best chance of getting it implemented soon isto 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这个版本对这个问题进行了完善和增强,可以这么简单的实现了。


           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值