标签:style blog http io color ar os sp java
FTPClient ftpClient = new FTPClient();
ftpClient.setConnectTimeout(10*1000); // 10s,如果超过就判定超时了
ftpClient.connect(hostName, 21);
void |
setConnectTimeout(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 password 11 * 密码 12 * @throws Exception 13 * 登录失败 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 }
标签:style blog http io color ar os sp java
原文地址:http://www.cnblogs.com/ihongyan/p/4104687.html