int responseFTP = clientFTP.getReplyCode();After each method call, the server returns messages. The following method displays messages from the server:
if (!reponseFTP.isPositiveCompletion(replyCode)) {
// The operation failed. The server denies
//the connection or rejects the
return operation;
}
private static void serverresponse(FTPClient ftpClient) {Here is the complete code that encompasses login, identification and verification:
String[] responses = ftpClient.getReplyStrings();
if (answers != null & & answers.length > 0) {
for (String response: responses) {
System.out.println("SERVER: " + response);
}
}
}
import java.io.IOException;Compilation and execution:
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FTPConnectionLogin {
private static void serverresponse(FTPClient ftpClient) {
String[] responses = ftpClient.getReplyStrings();
if (answers != null & & answers.length > 0) {
for (String response: responses) {
System.out.println("SERVER: " + response);
}
}
}
public static void main(String[] args) {
String server = "serveraddress";
int port = 21;
String username = "username";
String password = "password";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
serverresponse(ftpClient);
int response = ftpClient.getReplyCode();
if (! FTPReply.isPositiveCompletion(response)) {
System.out.println("Operation failed. Server Answer: " + answer);
return;
}
boolean status = ftpClient.login(username, password);
serverresponse(ftpClient);
if (!state) {
System.out.println("Unable to access the server");
return;
} else {
System.out.println("Identification successful");
}
} catch (IOException ex) {
System.out.println("An error during connection was detected");
e.g.printStackTrace();
}
}
}
SERVER: 220---------- Welcome to Pure-FTPd [privsep] ----------
SERVER: 220-You are user number 31 of 500 allowed.
SERVER: 220-Local time is now 06:02. Server port: 21.
SERVER: 220-This is a private system - No anonymous login
SERVER: 220 You will be disconnected after 3 minutes of inactivity.
SERVER: 230-OK. Current restricted directory is /
SERVER: 230-100 files used (1%) - authorized: 10000 files
SERVER: 230 5217 Kbytes used (0%) - authorized: 1536000 Kb
Successful identification
Please disable your ad blocker and refresh the window to use this website.