Sample Programs

Sample java program to upload a file to FTP

package org.jusfortechies.example; 

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

public class SampleFtpUpload {
  
  public static void main(String[] args) { 
    
    FTPClient client = new FTPClient(); 
    FileInputStream fis = null; 

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

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

      //Create an InputStream of the file to be uploaded 
      String filename = "example.txt"; 
      
      fis = new FileInputStream(filename); 
      
      //Store file to server 
      client.storeFile(filename, fis); 
      client.logout(); 

    } catch (IOException e) { 
      e.printStackTrace(); 

    } finally { 

      try { 
        if (fis != null) { 
          fis.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