Download files from my FTP Server

3

I have an FTP server from which I need to download some images.

I have already searched several codes on the Internet where they tell me to download thousands of bookstores, show the code and none of them work for me.

I'm doing this application with the Java programming language in Netbeans.

The code I found on the Internet:

package com.beingjavaguys.testftp;  

import java.io.FileOutputStream;  
import java.io.IOException;  
import java.net.SocketException;  
import org.apache.commons.net.ftp.FTPClient;  

public class DownloadFile {  
 public static void main(String args[]) {  

  // get an ftpClient object  
  FTPClient ftpClient = new FTPClient();  
  FileOutputStream fos = null;  

  try {  
   // pass directory path on server to connect  
   ftpClient.connect("nagesh12.5gbfree.com");  

   // pass username and password, returned true if authentication is  
   // successful  
   boolean login = ftpClient.login("username", "password");  

   if (login) {  
    System.out.println("Connection established...");  

    fos = new FileOutputStream("files/downloadedFile.txt");  
    boolean download = ftpClient.retrieveFile("uploadedFile.txt",  
      fos);  
    if (download) {  
     System.out.println("File downloaded successfully !");  
    } else {  
     System.out.println("Error in downloading file !");  
    }  

    // logout the user, returned true if logout successfully  
    boolean logout = ftpClient.logout();  
    if (logout) {  
     System.out.println("Connection close...");  
    }  
   } else {  
    System.out.println("Connection fail...");  
   }  

  } catch (SocketException e) {  
   e.printStackTrace();  
  } catch (IOException e) {  
   e.printStackTrace();  
  } finally {  
   try {  
    ftpClient.disconnect();  
   } catch (IOException e) {  
    e.printStackTrace();  
   }  
  }  
 }  
}  
    
asked by Marcus Declementi 11.02.2016 в 18:58
source

2 answers

1

You can use org.apache.commons.net.ftp.FTPClient

The code would be similar to:

FTPClient cliente = new FTPClient();//Iniciamos el cliente del FTP
FileOutputStream stream = null;

client.connect("192.168.0.tuIP");
client.login("usuario", "pass");

String archivo = "/archivo.doc";
stream = new FileOutputStream("archivoLocal");

cliente.retrieveFile(archivo, stream);//pone el archivo en tu stream
stream.close();
cliente.disconnect();

The import would be at org.apache.commons.net.ftp.FTPClient

FTPClient documentation

    
answered by 11.02.2016 / 19:16
source
1

The code that works for me is the following:

private static String ip = "ftp.xxx.xx";
private static String user = "user";
private static String pass = "password";
private static String localFileDownload = "C:\Users\userd\Desktop\archivo.xlsx";
private static String hostFile="/archivo.xlsx";

public static void main(String[] args) throws SocketException, IOException {
    conectar(ip,user,pass);
    desrcargarArchivoFTP(localFileDownload, hostFile);
    desconectar();
}    

public static void conectar(String ip, String user, String pass) throws SocketException, IOException{
    ftp = new FTPClient();
    ftp.connect(ip);

    if(ftp.login(user, pass))
        System.out.println("login OK");
    else
        System.out.println("login Error");
}

public static void desrcargarArchivoFTP(String localFile, String hostFile) throws FileNotFoundException, IOException{
    //fos = new FileOutputStream(localFile);
    BufferedOutputStream buffOut = new BufferedOutputStream(new FileOutputStream(localFile));
    if(ftp.retrieveFile(hostFile, buffOut))
        System.out.println("Descarga correcta");
    else
        System.out.println("Error Descarga");

    buffOut.close();
    //fos.close();
}
    
answered by 26.10.2016 в 02:50