Connecting to an FTP server in Java
This article implements Java code examples that show how to connect and identify yourself to an FTP server using the Apache Commons Net. Make sure that the commons-net.jar file exists in your project's classpath.
The net.ftp.FTPClient provides the necessary basis for interacting with the server via the FTP protocol. To connect to the server, use this method:
void connect(String server, int port): The server can be set with the hostname or IP address, and the port is a number that is used to listen for incoming connections for a given application. FTP uses port 21.
After connecting, use this method to identify yourself:
boolean login(String username, String password): this method returns true if the identification was successful, otherwise false.
A response from the FTP server must be obtained after calling the connect():
The net.ftp.FTPClient provides the necessary basis for interacting with the server via the FTP protocol. To connect to the server, use this method:
void connect(String server, int port): The server can be set with the hostname or IP address, and the port is a number that is used to listen for incoming connections for a given application. FTP uses port 21.
After connecting, use this method to identify yourself:
boolean login(String username, String password): this method returns true if the identification was successful, otherwise false.
A response from the FTP server must be obtained after calling the connect():
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