Java FTP example - Get server's reply messages
- Details
- Written by Nam Ha Minh
- Last Updated on 19 July 2019   |   Print Email
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); } }
- 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:
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:
- Connect and login to a FTP server
- Java FTP example - Change working directory
- Java FTP create directory example
- Java FTP list files and directories example
- Java FTP file download tutorial and example
- Java FTP file upload tutorial and example
- Java FTP example - Search for files and directories
- How to upload a directory to a FTP server
- How to download a complete folder from a FTP server
Comments