Search code examples
javaftpapache-commons-net

Apache FTPClient: Read welcome message


With my Java-Program I'm connecting to a FTP server with Apache Commons Net. The FTP server works as the update server for my software and currently everytime I check for updates, the updater downloads a .txt and checks if the version number written in the file is greater than the version number currently installed on the machine.

Is there a way to get the version number of the update for the software on the machine from the welcome-message of the FTP server? Then I don't have to download the .txt to check for updates instead I'm able to only connect to the server and check the welcome-message for the number?


Solution

  • The welcome message is effectively a "response" to a connection.

    So after you connect using the FTPClient.connect(), use the FTPClient.getReplyStrings() to retrieve the welcome message.

    ftp.connect(server);
    
    // After connection attempt, you should check the reply code to verify success.
    reply = ftp.getReplyCode();
    
    if (!FTPReply.isPositiveCompletion(reply))
    {
        ftp.disconnect();
        System.err.println("FTP server refused connection.");
        System.exit(1);
    }
    
    // read the initial response (aka "Welcome message")
    String[] welcomeMessage = ftp.getReplyStrings();