Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Dauda Ainoo
Added: May 7, 2020 3:05 PM
Views: 16
Tags: no tags
  1. package com.journaldev.inheritance;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.PrintWriter;
  8.  
  9. import org.apache.commons.net.PrintCommandListener;
  10. import org.apache.commons.net.ftp.FTP;
  11. import org.apache.commons.net.ftp.FTPClient;
  12. import org.apache.commons.net.ftp.FTPReply;
  13.  
  14. public class FTPUploader {
  15.        
  16.         FTPClient ftp = null;
  17.        
  18.         public FTPUploader(String host, String user, String pwd) throws Exception{
  19.                 ftp = new FTPClient();
  20.                 ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
  21.                 int reply;
  22.                 ftp.connect(host);
  23.                 reply = ftp.getReplyCode();
  24.                 if (!FTPReply.isPositiveCompletion(reply)) {
  25.                         ftp.disconnect();
  26.                         throw new Exception("Exception in connecting to FTP Server");
  27.                 }
  28.                 ftp.login(user, pwd);
  29.                 ftp.setFileType(FTP.BINARY_FILE_TYPE);
  30.                 ftp.enterLocalPassiveMode();
  31.         }
  32.         public void uploadFile(String localFileFullName, String fileName, String hostDir)
  33.                         throws Exception {
  34.                 try(InputStream input = new FileInputStream(new File(localFileFullName))){
  35.                 this.ftp.storeFile(hostDir + fileName, input);
  36.                 }
  37.         }
  38.  
  39.         public void disconnect(){
  40.                 if (this.ftp.isConnected()) {
  41.                         try {
  42.                                 this.ftp.logout();
  43.                                 this.ftp.disconnect();
  44.                         } catch (IOException f) {
  45.                                 // do nothing as file is already saved to server
  46.                         }
  47.                 }
  48.         }
  49.         public static void main(String[] args) throws Exception {
  50.                 System.out.println("Start");
  51.                 FTPUploader ftpUploader = new FTPUploader("ftp.journaldev.com", "ftpUser", "ftpPassword");
  52.                 //FTP server path is relative. So if FTP account HOME directory is "/home/pankaj/public_html/" and you need to upload
  53.                 // files to "/home/pankaj/public_html/wp-content/uploads/image2/", you should pass directory parameter as "/wp-content/uploads/image2/"
  54.                 ftpUploader.uploadFile("D:\\Pankaj\\images\\MyImage.png", "image.png", "/wp-content/uploads/image2/");
  55.                 ftpUploader.disconnect();
  56.                 System.out.println("Done");
  57.         }
  58.  
  59. }