int reponseFTP = clientFTP.getReplyCode();Après chaque appel de méthode, le serveur retourne des messages. La méthode suivante affiche les messages du serveur:
if (!reponseFTP.isPositiveCompletion(replyCode)) {
// L'opération a échoué. Le serveur refuse
//la connexion ou rejette l'opération
return;
}
private static void reponseServeur(FTPClient ftpClient) {Voici le code complet qui englobe la connexion, l'identification et la vérification:
String[] reponses = ftpClient.getReplyStrings();
if (reponses != null && reponses.length > 0) {
for (String reponse : reponses) {
System.out.println("SERVEUR: " + reponse);
}
}
}
import java.io.IOException;Compilation et exécution :
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FTPConnectionLogin {
private static void reponseServeur(FTPClient ftpClient) {
String[] reponses = ftpClient.getReplyStrings();
if (reponses != null && reponses.length > 0) {
for (String reponse : reponses) {
System.out.println("SERVEUR: " + reponse);
}
}
}
public static void main(String[] args) {
String server = "adresseDuServeur";
int port = 21;
String username = "nomUtilisateur";
String password = "motDePasse";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
reponseServeur(ftpClient);
int reponse = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reponse)) {
System.out.println("Operation échoué. Réponse Serveur: " + reponse);
return;
}
boolean etat = ftpClient.login(username, password);
reponseServeur(ftpClient);
if (!etat) {
System.out.println("Impossible d'accéder au serveur");
return;
} else {
System.out.println("Identification réussie");
}
} catch (IOException ex) {
System.out.println("Une erreur lors de la connexion a été détecté");
ex.printStackTrace();
}
}
}
SERVEUR: 220---------- Welcome to Pure-FTPd [privsep] ----------
SERVEUR: 220-You are user number 31 of 500 allowed.
SERVEUR: 220-Local time is now 06:02. Server port: 21.
SERVEUR: 220-This is a private system - No anonymous login
SERVEUR: 220 You will be disconnected after 3 minutes of inactivity.
SERVEUR: 230-OK. Current restricted directory is /
SERVEUR: 230-100 files used (1%) - authorized: 10000 files
SERVEUR: 230 5217 Kbytes used (0%) - authorized: 1536000 Kb
Identification réussie
Please disable your ad blocker and refresh the window to use this website.