Sample Programs

Sample java program to download a file from FTP

package org.jusfortechies.example; 

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

public class SampleFtpDownload {
  
  public static void main(String[] args) { 
    
    FTPClient client = new FTPClient(); 
    FileOutputStream fos = null; 

    try { 
      client.connect("ftp.domain.com");

      // Pass username and password 
      client.login("username", "password"); 

      // The remote filename to be downloaded. 
      String filename = "example.txt"; 
      fos = new FileOutputStream(filename); 

      // Download file from FTP server 
      client.retrieveFile("/" + filename, fos); 

    } catch (IOException e) { 
      e.printStackTrace(); 
    } finally { 
      try { 
        if (fos != null) { 
          fos.close(); 
        } 
        client.disconnect(); 
      } catch (IOException e) {
        e.printStackTrace(); 
      } 
    }

  } 

} 

 

You need to download below jars to run the above example
1. commons-net-1.4.1.jar



blog comments powered by Disqus