首页 > 代码库 > ftpClient的连接超时设置

ftpClient的连接超时设置

 

FTPClient ftpClient = new FTPClient();
 ftpClient.setConnectTimeout(10*1000); // 10s,如果超过就判定超时了
 ftpClient.connect(hostName, 21);

 

http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/SocketClient.html#setConnectTimeout(int)

Class org.apache.commons.net.SocketClient
voidsetConnectTimeout(int connectTimeout)
Sets the connection timeout in milliseconds, which will be passed to the Socket object‘s connect() method.

 

 

 

 1 /** 2      * 登录FTP服务器 3      *  4      * @param host 5      *            FTP主机地址 6      * @param port 7      *            FTP主机端口 8      * @param username 9      *            用户名10      * @param password11      *            密码12      * @throws Exception13      *             登录失败14      */15     public void ftpLogin(String host, int port, String username, String password)16             throws Exception {17         client = new FTPClient();18         // 设定连接超时时间19         client.setConnectTimeout(10*1000);20         try {21             client.connect(host, port);22             client.login(username, password);23             replyCode = client.getReplyCode();24             if (!FTPReply.isPositiveCompletion(replyCode)) {25                 reply = client.getReplyString().trim();26                 throw new Exception("FTP 登录失败,响应消息:" + reply);27             }28             log.info("FTP 登录成功");29 30             // 设置缓冲区31             client.setBufferSize(BufferSize);32             // 设置传输模式33             // client.setFileType(FTP.BINARY_FILE_TYPE);34         } catch (Exception e) {35             log.error(e.getMessage(), e);36             throw new Exception("FTP 登录失败");37         }38     }

 

ftpClient的连接超时设置