有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java FTP组织。阿帕奇。平民网格式错误的ServerReplyException:截断的服务器回复:“220”

我正在使用Java Apache Commons网络库从FTP服务器下载文件。作为一个起点,我试图重用来自https://www.codejava.net/java-se/networking/ftp/java-ftp-file-upload-tutorial-and-example的代码。一般来说,代码执行时没有问题/异常,但是对于一个特定的FTP服务器(ftp://ftp.nasdaqtrader.com/symboldirectory/nasdaqlisted.txt),我得到以下错误:

org.apache.commons.net.MalformedServerReplyException: Truncated server reply: '220 '

我的代码如下:

String server = "ftp.nasdaqtrader.com";
int port = 21;
String user = "anonymous";
String pass = "pw";

FTPClient ftpClient = new FTPClient();
try {

    ftpClient.connect(server, port);
    ftpClient.login(user, pass);
    ftpClient.enterLocalPassiveMode();
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

    // APPROACH #1: using retrieveFile(String, OutputStream)
    String remoteFile1 = "/symboldirectory/nasdaqlisted.txt";
    File downloadFile1 = new File("C:\\filedirectory\\nasdaqlisted.txt");
    OutputStream outputStream1 =
        new BufferedOutputStream(new FileOutputStream(downloadFile1));
    boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
    outputStream1.close();

    if (success) {
        System.out.println("File #1 has been downloaded successfully.");
    }

    } catch (IOException ex) {
    System.out.println("Error: " + ex.getMessage());
    ex.printStackTrace();
} finally {
    try {
        if (ftpClient.isConnected()) {
            ftpClient.logout();
            ftpClient.disconnect();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

从我的Windows终端连接会产生以下结果:

C:\Computer>ftp ftp.nasdaqtrader.com
Connected to ftp.nasdaqtrader.com.
220
200 OPTS UTF8 command successful - UTF8 encoding now ON.
User (ftp.nasdaqtrader.com:(none)): anonymous
331 Anonymous access allowed, send identity (e-mail name) as password.
Password:
230 User logged in.
ftp> quit
221 Goodbye.

对于类似的FTP服务器(NOAA weather),代码通过Windows终端连接和下载时会产生以下结果:

C:\Computer>ftp ftp.cdc.noaa.gov
Connected to ftp.cdc.noaa.gov.
220-**********************************************************************
220-**WARNING**WARNING**WARNING**WARNING**WARNING**WARNING**WARNING**    *
220-*                                                                    *
220-* This is a Department of Commerce (DOC) computer system.  DOC       *   
220-* computer systems are provided for the processing of official U.S.  *
220-* Government information only. Unauthorized access or use of this    *
220-* computer system may subject violators to criminal, civil, and/or   *
220-* administrative action. All data contained within DOC computer      *
220-* systems is owned by the DOC, and may be audited, intercepted,      *
220-* recorded, read, copied, or captured in any manner and disclosed in *
220-* any manner, by authorized personnel. THERE IS NO RIGHT OF PRIVACY  *
220-* IN THIS SYSTEM. System personnel may disclose any potential        *
220-* evidence of crime found on DOC computer systems to appropriate     *
220-* authorities. USE OF THIS SYSTEM BY ANY USER, AUTHORIZED OR         *
220-* UNAUTHORIZED CONSTITUTES CONSENT TO THIS AUDITING, INTERCEPTION,   *
220-* RECORDING, READING, COPYING, CAPTURING, and DISCLOSURE OF COMPUTER *
220-* ACTIVITY.                                                          *
220-*                                                                    *
220-**WARNING**WARNING**WARNING**WARNING**WARNING**WARNING**WARNING**    *
220-**********************************************************************
220
200 Always in UTF8 mode.
User (ftp.cdc.noaa.gov:(none)): anonymous
331 Please specify the password.
Password:
230 Login successful.

因此,比较这两个回复,似乎是ftp。纳斯达克雷达。com只是没有提供适当的(标准?)220回复(即截断)。所以:

  1. 我是否正确识别了问题并
  2. 处理这个问题的适当方式是什么

谢谢


共 (1) 个答案

  1. # 1 楼答案

    Apache Commons Net库认为来自服务器的220响应不符合RFC 959(可能是正确的)

    如果要允许库与服务器对话,请调用^{}

    ftpClient.setStrictReplyParsing(false);