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

sd2018 :

I'm using Java Apache Commons Net library for downloading files from an FTP server. As a starting point, I am attempting to re-use code from https://www.codejava.net/java-se/networking/ftp/java-ftp-file-upload-tutorial-and-example. In general, the code executes without issue/exceptions however for one particular FTP server (ftp://ftp.nasdaqtrader.com/symboldirectory/nasdaqlisted.txt) I get the following error:

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

My code is as follows:

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();
    }
}

Connecting from my Windows terminal yields the following:

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.

For a similar FTP server (NOAA weather) where the code connects and downloads without exceptions connecting via the Windows terminal yields the following:

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.

So comparing the two replies it appears that ftp.nasdaqtrader.com simply doesn't provide the appropriate (standard?) 220 reply (i.e. truncated). So:

  1. have I correctly identified the issue and
  2. what is the appropriate way to deal with this issue?

Thanks!

Martin Prikryl :

The Apache Commons Net library believes that the 220 response from the server does not conform to RFC 959 (probably rightfully).

If you want to allow the library to talk to the server, call FTP.setStrictReplyParsing:

ftpClient.setStrictReplyParsing(false);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=77709&siteId=1