免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 11910 | 回复: 5
打印 上一主题 下一主题

请教sun.net.ftp.FtpClient能否设置连接超时时间呢? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-19 17:16 |只看该作者 |倒序浏览
10可用积分
请教sun.net.ftp.FtpClient能否设置连接超时时间呢?
以前没用过这个类,不知道默认时间是多少,但是由于现在ftp网络有时候不太好,所以想把连接超时判断时间设置长一些,比如10分钟之类的,不知道有没有可行的方法呢?
谢谢大家了!

论坛徽章:
0
2 [报告]
发表于 2010-01-19 17:20 |只看该作者
看一下API DOC就OK了吧

论坛徽章:
0
3 [报告]
发表于 2010-01-25 20:24 |只看该作者
呵呵,看来是没法在客户端控制了。

论坛徽章:
0
4 [报告]
发表于 2010-06-21 13:05 |只看该作者
有一个connectTimeout 如.setConnectTimeout(1000); // 一秒钟,如果超过就判定超时了

论坛徽章:
0
5 [报告]
发表于 2010-06-21 17:00 |只看该作者
本帖最后由 @zheng 于 2010-06-21 17:03 编辑

  1. import java.io.*;
  2. import java.net.*;
  3. import sun.net.ftp.FtpClient;

  4. class CustomFtpClient extends FtpClient {

  5.     public CustomFtpClient(String host) throws IOException {
  6.         super(host);
  7.     }

  8.     public CustomFtpClient(String host, int port) throws IOException {
  9.         super(host, port);
  10.     }

  11.     public CustomFtpClient() {
  12.         super();
  13.     }

  14.     public void setTimeout(int timeout) {
  15.         if (serverSocket != null) {
  16.             try {
  17.                 serverSocket.setSoTimeout(timeout);
  18.             } catch (SocketException socketException) {
  19.             }
  20.         }
  21.     }
  22. }

  23. public class Test {

  24.     public static void main(String[] args) {
  25.         String host = "127.0.0.1";
  26.         String user = "username";
  27.         String password = "password";
  28.         try {
  29.             CustomFtpClient ftpClient = new CustomFtpClient();
  30.             ftpClient.openServer(host);
  31.             ftpClient.setTimeout(5000); // set timeout in 5 seconds
  32.             ftpClient.login(user, password);
  33.             ftpClient.binary();
  34.             ftpClient.closeServer();
  35.             System.out.println("Connect is OK");
  36.         } catch (Exception exp) {
  37.             exp.printStackTrace();
  38.             System.out.println("Connect is Timeout");
  39.         }
  40.     }
  41. }
复制代码

论坛徽章:
0
6 [报告]
发表于 2010-06-21 17:05 |只看该作者
:wink:
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP