我写了个ftp下载程序,在本机测试运行时是正常的。但是访问第三方ftp服务器时出现了问题:程序停在 ftpClient.cd("/home/jboss/a/"); 不动了。请高手八卦下,谢谢!程序片段如下:
               ……程序成功登录ftp服务器后……                               try { 
  
  if(list.size()>0){   //list是文件集合
  for(int i=0;i<list.size();i++){
  filename = (String)list.get(i);
  if (remoteDir.length() != 0)   //remoteDir是ftp目录
 
                    ftpClient.cd(remoteDir); //程序卡在这不动了

                ftpClient.binary(); 
               
                TelnetInputStream is = ftpClient.get(filename); 
              
                File file_out = new File(localpath + File.separator + filename);   
              
                FileOutputStream os = new FileOutputStream(file_out); 
              
                byte[] bytes = new byte[1024];   
                int c;   
                while ((c = is.read(bytes)) != -1) {
                    os.write(bytes, 0, c);   
                }   
                is.close();   
                os.close();
  }
  }
  
                ftpClient.closeServer();   
                System.out.println("----登出服务器---------------");
        } catch (Exception ex) {   
            throw new Exception("ftp download file error:" + ex.getMessage());   
        } 

解决方案 »

  1.   

    查看了一下源码,估计issueCommand里发生try again,可能跟网络有关吧,防火墙设置一类的,是不是会导致ftp访问延时或失败/**
      665        * CD to a specific directory on a remote FTP server
      666        *
      667        * @param   remoteDirectory path of the directory to CD to
      668        *
      669        * @exception       <code>FtpProtocolException</code>
      670        */
      671       public void cd(String remoteDirectory) throws IOException {
      672           if (remoteDirectory == null ||
      673               "".equals(remoteDirectory))
      674               return;
      675           issueCommandCheck("CWD " + remoteDirectory);
      676       }
    /**
      191        * Send a command to the FTP server.
      192        *
      193        * @param   cmd     String containing the command
      194        * @return          reply code
      195        *
      196        * @exception       FtpProtocolException if an error occured
      197        */
      198       protected int issueCommand(String cmd) throws IOException {
      199           command = cmd;
      200   
      201           int reply;
      202   
      203           while (replyPending) {
      204               replyPending = false;
      205               if (readReply() == FTP_ERROR)
      206                   throw new FtpProtocolException("Error reading FTP pending reply\n");
      207           }
      208           do {
      209               sendServer(cmd + "\r\n");
      210               reply = readReply();
      211           } while (reply == FTP_TRY_AGAIN);
      212           return reply;
      213       }
      214   
      215       /**
      216        * Send a command to the FTP server and check for success.
      217        *
      218        * @param   cmd     String containing the command
      219        *
      220        * @exception       FtpProtocolException if an error occured
      221        */
      222       protected void issueCommandCheck(String cmd) throws IOException {
      223           if (issueCommand(cmd) != FTP_SUCCESS)
      224               throw new FtpProtocolException(cmd + ":" + getResponseString());
      225       }/**
      228        * Read the reply from the FTP server.
      229        *
      230        * @return          FTP_SUCCESS or FTP_ERROR depending on success
      231        * @exception       FtpProtocolException if an error occured
      232        */
      233       protected int readReply() throws IOException {
      234           lastReplyCode = readServerResponse();
      235   
      236           switch (lastReplyCode / 100) {
      237           case 1:
      238               replyPending = true;
      239               /* falls into ... */
      240   
      241           case 2:
      242           case 3:
      243               return FTP_SUCCESS;
      244   
      245           case 5:
      246               if (lastReplyCode == 530) {
      247                   if (!loggedIn) {
      248                       throw new FtpLoginException("Not logged in");
      249                   }
      250                   return FTP_ERROR;
      251               }
      252               if (lastReplyCode == 550) {
      253                   throw new FileNotFoundException(command + ": " + getResponseString());
      254               }
      255           }
      256   
      257           /* this statement is not reached */
      258           return FTP_ERROR;
      259       }
      

  2.   

    第三方允许你访问他的ftp端口吗?我以前遇到过,需要第三方给你开放权限你才能访问~
      

  3.   

    可以访问他的端口
     ftpClient.openServer(hostName, port);---- port=21
    ftpClient.login(userName, passWord);
    都能登录成功在进入文件目录时就卡住了。但是 用命令就可以直接cd进去:
    ftp> cd /home/jboss/a/
    250 CWD command successful.