当前位置:编程学习 > JAVA >>

org.apache.commons.net.ftp.FTPClient连接服务器超时

在用org.apache.commons.net.ftp.FTPClient访问服务器是报如下错误:

java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:163)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:184)
at com.asb.test.FtpTest.main(FtpTest.java:34)

但我用IE是可以访问FTP服务器目录的,也可以查看文件内容,但我用程序连接时就连接不上,请问是什么原因,急切盼望您的解答!!! --------------------编程问答-------------------- 相同的问题困扰中...  不过我用相同的程序连接ftp服务器有的可以连接上,有的不行。可以连上的就那么1,2个。我是了5,6个服务器。 commons-net文档写的那个例子里的ftp.foobar.com我是可以连接上的。不知道你的行不。 --------------------编程问答-------------------- 连接超时的原因很多,首先你得保证你的代码的正确性。
--------------------编程问答-------------------- 最近写的一个FTP,你可以参考下。

public class FtpUtils {
 
 
 /**
  * 向FTP服务器上传文件
  * 
  * @param ip
  *            FTP服务器ip e.g:192.168.8.22
  * @param port
  *            FTP服务器端口 
  * @param username
  *            FTP登录账号 
  * @param password
  *            FTP登录密码  
  * @param serverpath
  *            FTP服务器保存目录(相对路径) 默认缺省时指向主目录
  * @param file
  *            上传到FTP服务器上的文件的绝对路径 e.g: E:/log/log.txt OR E:\log\log.txt
  *            
  * @return 
  *      成功返回true,否则返回false
  */
 public boolean uploadFile(String ip, int port, String username,
   String password, String serverpath, String file) {
  // 初始表示上传失败
  boolean success = false;
  // 创建FTPClient对象
  FTPClient ftp = new FTPClient();
  ftp.setControlEncoding("GBK"); 
  try {
   int reply=0;
   // 连接FTP服务器
   // 如果采用默认端口,可以使用ftp.connect(ip)的方式直接连接FTP服务器
   ftp.connect(ip, port);
   // 登录ftp
   ftp.login(username, password);
   // 看返回的值是不是reply>=200&&reply<300 如果是,表示登陆成功
   reply = ftp.getReplyCode();
   // 以2开头的返回值就会为真
   if (!FTPReply.isPositiveCompletion(reply)) {
    ftp.disconnect();
    return success;
   }
   // 转到指定上传目录
   serverpath=gbkToIso8859(serverpath);
   ftp.changeWorkingDirectory(iso8859ToGbk(serverpath));
   
   checkPathExist(ftp,iso8859ToGbk(serverpath));
   
   //输入流
   InputStream input=null;
   try {
    file=gbkToIso8859(file);
    input = new FileInputStream(iso8859ToGbk(file));
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   }
   // 将上传文件存储到指定目录
   file=iso8859ToGbk(file);
   ftp.storeFile(iso8859ToGbk(serverpath)+"/"+iso8859ToGbk(getFilename(file)), input);
   // 关闭输入流
   input.close();
   // 退出ftp
   ftp.logout();
   // 表示上传成功
   success = true;
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   if (ftp.isConnected()) {
    try {
     ftp.disconnect();
    } catch (IOException ioe) {
    }
   }
  }
  return success;
 }
 
 /**
  * 从FTP服务器下载文件
  * 
  * @param ip
  *            FTP服务器ip e.g:192.168.8.22
  * @param port
  *            FTP服务器端口
  * @param username
  *            FTP登录账号
  * @param password
  *            FTP登录密码
  * @param serverpath
  *            FTP服务器上的相对路径 默认缺省时指向主目录
  * @param fileName
  *            要下载的文件名
  * @param localPath
  *            下载后保存到本地的路径 不含文件名
  * @return
  *      成功返回true,否则返回false
  */
 public boolean downFile(String ip, int port, String username,
   String password, String serverpath, String fileName,
   String localPath) {
  // 初始表示下载失败
  boolean success = false;
  // 创建FTPClient对象
  FTPClient ftp = new FTPClient();
  ftp.setControlEncoding("GBK"); 
  try {
   int reply;
   // 连接FTP服务器
   // 如果采用默认端口,可以使用ftp.connect(ip)的方式直接连接FTP服务器
   ftp.connect(ip, port);
   // 登录ftp
   ftp.login(username, password);
   reply = ftp.getReplyCode();
   if (!FTPReply.isPositiveCompletion(reply)) {
    ftp.disconnect();
    return success;
   } 
   // 转到指定下载目录
   serverpath=gbkToIso8859(serverpath);
   ftp.changeWorkingDirectory(this.iso8859ToGbk(serverpath));
   // 列出该目录下所有文件
   FTPFile[] fs = ftp.listFiles();
   fileName=this.gbkToIso8859(fileName);
   localPath=this.gbkToIso8859(localPath);
   
   // 遍历所有文件,找到指定的文件
   for (FTPFile f : fs) {
    if (f.getName().equals(iso8859ToGbk(fileName))) {
     // 根据绝对路径初始化文件
     File localFile = new File(iso8859ToGbk(localPath) + "/" + f.getName());
     File localFileDir = new File(iso8859ToGbk(localPath));
     //保存路径不存在时创建
     if(!localFileDir.exists()){
      localFileDir.mkdirs();
     }
     // 输出流
     OutputStream is = new FileOutputStream(localFile);
     // 下载文件
     ftp.retrieveFile(f.getName(), is);
     is.close();
    }
   }
   // 退出ftp
   ftp.logout();
   // 下载成功
   success = true;
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   if (ftp.isConnected()) {
    try {
     ftp.disconnect();
    } catch (IOException ioe) {
    }
   }
  }
  return success;
 }
 
 /**
  *  
  * 查找指定目录是否存在  不存在创建目录
  * 
  * @param FTPClient 
  *    ftpClient 要检查的FTP服务器
  * @param String 
  *    filePath 要查找的目录 
  * @return 
  *    boolean:存在:true,不存在:false 
  * @throws IOException 
  */
 private  boolean checkPathExist(FTPClient ftpClient, String filePath)
   throws IOException {
  boolean existFlag = false;
  try {
   if (filePath != null && !filePath.equals("")) {
    if (filePath.indexOf("/") != -1) {
     int index = 0;
     while ((index = filePath.indexOf("/")) != -1) {
      if (!ftpClient.changeWorkingDirectory(filePath.substring(0,index))) {
       ftpClient.makeDirectory(filePath.substring(0,index));
      }
      ftpClient.changeWorkingDirectory(filePath.substring(0,index));
      filePath = filePath.substring(index + 1, filePath.length());
     }
     if (!filePath.equals("")) {
      if (!ftpClient.changeWorkingDirectory(filePath)) {
       ftpClient.makeDirectory(filePath);
      }
     }
    } 
    existFlag = true;
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return existFlag;
 }
 
 
 /**
  * 根据绝对路径获得文件名
  * @param file
  *      文件绝对路径 e.g: e.g: E:/log/log.txt OR E:\\log\\log.txt
  * @return
  *      转码后的文件名 
  */
 private String getFilename(String file){
  //文件名
  String filename="";
  if(file!=null&&!file.equals("")){
   file=file.replaceAll(Matcher.quoteReplacement("\\"), "/");
   String[] strs=file.split("/");
   filename=strs[strs.length-1];
  }
  filename=gbkToIso8859(filename);//转码
  return filename;
 }
 
 /** 
  * 转码[ISO-8859-1 ->  GBK] 
  * 不同的平台需要不同的转码 
  * @param obj 
  * @return 
  */
 private  String iso8859ToGbk(Object obj) {
  try {
   if (obj == null)
    return "";
   else
    return new String(obj.toString().getBytes("iso-8859-1"), "GBK");
  } catch (Exception e) {
   return "";
  }
 }

 /** 
  * 转码[GBK ->  ISO-8859-1] 
  * 不同的平台需要不同的转码 
  * @param obj 
  * @return 
  */
 private  String gbkToIso8859(Object obj) {
  try {
   if (obj == null)
    return "";
   else
    return new String(obj.toString().getBytes("GBK"), "iso-8859-1");
  } catch (Exception e) {
   return "";
  }
 }
--------------------编程问答-------------------- 我也遇到了同样的问题,不知道你是怎么解决的!
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,