1

我一直在使用教程从这里列出来自 FTP 服务器的目录。但是它给出了异常,因为java.net.SocketException: Software caused connection abort: socket write error 图像也附上在此处输入图像描述 了这背后的原因,可能是我必须为 JVM 启用一些安全限制吗?(只是一个想法)温柔地谢谢你

4

1 回答 1

0

假设您尝试使用任何 FTP 客户端FileZilla(如

        FTPClient ftp = new FTPClient();
        DateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 

        FTPClientConfig config = new FTPClientConfig();
        //optional - set timezone of the server 
        config.setServerTimeZoneId("America/New_York");

        ftp.configure(config );

        try {

            int reply;

            ftp.connect(SERVER_NAME);
            ftp.login(USER_NAME, PASSWORD);
            System.out.println("Connected to " + SERVER_NAME + ".");
            System.out.println(ftp.getReplyString());

            //After connection attempt, check the reply code to verify success.
            reply = ftp.getReplyCode();

            if(!FTPReply.isPositiveCompletion(reply)) {
              ftp.disconnect();
              System.out.println("Failed to connect to FTP server");
              System.exit(1);
            }

             //use binary mode
             ftp.setFileType(FTP.BINARY_FILE_TYPE);
             //use passive mode for firewalls
             ftp.enterLocalPassiveMode();

             FTPListParseEngine engine = ftp.initiateListParsing(".");

             while (engine.hasNext()) {
                FTPFile[] files = engine.getNext(25);  
                for (FTPFile file : files) {
                    String details = file.getName();
                    if (file.isDirectory()) {
                        details = "[" + details + "]";
                    }
                    details += "\t\t" + file.getSize();
                    details += "\t\t" + dateFormater.format(file.getTimestamp().getTime());
                    System.out.println(details);
                }

             }

          ftp.logout();

        } catch(IOException e) {
               e.printStackTrace();
        } finally {
          if(ftp.isConnected()) {
            try {
              ftp.disconnect();
            } catch(IOException ioe) {
              // do nothing
            }
          }
        }

教程中未处理的一些要点:

  1. 处理防火墙的被动模式
  2. FTP文件的分页
  3. 检查回复代码
于 2013-07-23T08:49:35.447 回答