When programming FTP in Java using the Apache Commons Net API, we can obtain messages replied from a FTP server after each command sent by a FTP client. To do so, call either of the following two methods from the FTPClientclass:

    • String getReplyString(): returns last response messages from the FTP server in a single String object. The String will contain end of line markers if the response has multiple lines. This method is suitable if we just want to display the response to the standard output console and don’t care about the end of line markers. For example:
      ftpClient.login(user, pass);
      String serverReply = ftpClient.getReplyString();
      
      System.out.println(serverReply);

       

    • String[] getReplyStrings(): returns last response messages from the FTP server in an array of String objects. Each String element represents a line of response. The end of line markers are removed. So this method is suitable if we want to process the lines of response separately. For example:
      ftpClient.changeWorkingDirectory("/photos");
      
      String[] replies = ftpClient.getReplyStrings();
      
      if (replies != null && replies.length > 0) {
      	for (String aReply : replies) {
      		System.out.println("SERVER: " + aReply);
      	}
      }

       

Here’s an example program that connects and logins to a FTP server, then logouts and disconnects. The server’s response is displayed after each command using the getReplyString()method:

package net.codejava.ftp;

import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;

/**
 * This program demonstrates how to get and display reply messages from
 * a FTP server.
 * @author www.codejava.net
 *
 */
public class FTPGetServerReplyDemo {

	public static void main(String[] args) {
		String server = "www.myserver.com";
		int port = 21;
		String user = "username";
		String pass = "password";

		FTPClient ftpClient = new FTPClient();

		try {
			ftpClient.connect(server, port);
			System.out.println(ftpClient.getReplyString());

			ftpClient.login(user, pass);
			System.out.println(ftpClient.getReplyString());

			ftpClient.logout();
			System.out.println(ftpClient.getReplyString());

			ftpClient.disconnect();

		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			if (ftpClient.isConnected()) {
				try {
					ftpClient.disconnect();
				} catch (IOException ex) {
					ex.printStackTrace();
				}
			}
		}
	}
}

And here’s the sample output when running the above program:

220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 12 of 50 allowed.
220-Local time is now 06:02. Server port: 21.
220 You will be disconnected after 15 minutes of inactivity.

230 OK. Current restricted directory is /

221-Goodbye. You uploaded 0 and downloaded 0 kbytes.
221 Logout.

 

Other Java FTP Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Attachments:
Download this file (FTPGetServerReplyDemo.java)FTPGetServerReplyDemo.java[Demo program]0.9 kB

Add comment

   


Comments 

#1Ed Sowell2019-06-25 10:27
Love your stuff. Thanks!
Quote