I do not know how to download a folder from an FTP from Java.
The library described below only allows the download of specific files. The entire code is based on FTPClient . Here is the method to make backup copies of the "www" folder of any FTP server.
/**
* Metodo que realizara la conexion a traves de FTP Buscara la carpeta "www" y
* la descargara en el path de destino indicado.
*
* @param host
* recibe el host a conectarse
* @param usr
* recibe el user a conectarse
* @param pass
* la contraseña del usuario
* @param carpetaRemota
* la carpeta a la que se le va a hacer la copia
* @throws InterruptedException
*/
public void metodoBackupFtp(String host, String usr, String pass, String carpetaRemota, String destino) {
boolean connected, disconnected;
try {
FTPClient clienteFtp = new FTPClient();
System.err.println("Datos de conexión\nHost:" + host + "\nUser:" + usr + "\nPass:" + pass);
clienteFtp.connect(host);
connected = clienteFtp.login(usr, pass);
clienteFtp.enterLocalPassiveMode();
clienteFtp.setFileType(FTP.BINARY_FILE_TYPE);
if (connected) {
System.out.println("Conectado al FTP!");
System.err.println("Descarga de carpeta Carpeta Remota: " + carpetaRemota + "Destino: " + pathDestino);
} else {
System.err.println("ERROR:Fallo la conexión al FTP => " + host + "\n");
}
clienteFtp.enterLocalPassiveMode();
FTPFile[] files = clienteFtp.listFiles();
String[] sfiles = null;
if (files != null) {
sfiles = new String[files.length];
for (int i = 0; i < files.length; i++) {
System.out.println(sfiles[i] = files[i].getName());
}
}
// Se descarga el archivo
Aquí está lo más importante
//Método por el que pregunto que descargara la carpeta
disconnected = clienteFtp.logout();
if (disconnected) {
System.out.println("Desconectado!");
}
clienteFtp.disconnect();
} catch (SocketException e) {
System.out.println("ERROR:SocketException");
} catch (IOException e) {
System.out.println("ERROR:IOException");
}
}
In this code you have how to make a basic connection. However, the problem comes when the FTPClient library (Described in the title) does not have a method to directly download a folder.
How can you download a folder directly?