Advertisement
Guest User

Untitled

a guest
Aug 24th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package javaapplication3;
  7.  
  8. import org.apache.commons.net.ftp.FTP;
  9. import org.apache.commons.net.ftp.FTPClient;
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14.  
  15. /**
  16.  *
  17.  * @author Martin
  18.  */
  19. public class FTPManager {
  20.    
  21.     private String server;
  22.     private int port;
  23.     private String user;
  24.     private String pass;
  25.    
  26.     private FTPClient ftpClient;
  27.  
  28.     public FTPManager(String server, int port, String user, String password) {
  29.         this.server = server;
  30.         this.port = port;
  31.         this.user = user;
  32.         this.pass = password;
  33.         this.ftpClient = new FTPClient();
  34.     }
  35.    
  36.     public boolean connect() {
  37.         try {
  38.          ftpClient.connect(server, port);
  39.          ftpClient.login(user, pass);
  40.          ftpClient.enterLocalPassiveMode();
  41.  
  42.          ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
  43.          return true;
  44.        } catch (IOException ex) {
  45.             System.out.println("Error: " + ex.getMessage());
  46.             ex.printStackTrace();          
  47.             return false;
  48.        }
  49.     }
  50.    
  51.     public boolean disconnect() {
  52.         try {
  53.                 if (ftpClient.isConnected()) {
  54.                     ftpClient.logout();
  55.                     ftpClient.disconnect();
  56.                     return true;
  57.                 }
  58.                 return false;
  59.             } catch (IOException ex) {
  60.                 ex.printStackTrace();
  61.                 return false;
  62.             }
  63.     }
  64.    
  65.     public boolean transferFile(File file, String filePath) {
  66.         boolean done = false;
  67.         try {
  68.             // APPROACH #1: uploads first file using an InputStream
  69.             File firstLocalFile = file;
  70.  
  71.             String firstRemoteFile = filePath;
  72.             InputStream inputStream = new FileInputStream(firstLocalFile);
  73.  
  74.             System.out.println("Start uploading first file");
  75.          
  76.             done = ftpClient.storeFile(firstRemoteFile, inputStream);
  77.  
  78.             inputStream.close();
  79.             if (done) {          
  80.                 System.out.println("The first file is uploaded successfully.");
  81.             }
  82.         } catch (IOException ex) {
  83.             System.out.println("Error: " + ex.getMessage());
  84.             ex.printStackTrace();    
  85.         }
  86.         return done;
  87.     }
  88.    
  89.    
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement