Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package javaapplication3;
- import org.apache.commons.net.ftp.FTP;
- import org.apache.commons.net.ftp.FTPClient;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- /**
- *
- * @author Martin
- */
- public class FTPManager {
- private String server;
- private int port;
- private String user;
- private String pass;
- private FTPClient ftpClient;
- public FTPManager(String server, int port, String user, String password) {
- this.server = server;
- this.port = port;
- this.user = user;
- this.pass = password;
- this.ftpClient = new FTPClient();
- }
- public boolean connect() {
- try {
- ftpClient.connect(server, port);
- ftpClient.login(user, pass);
- ftpClient.enterLocalPassiveMode();
- ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
- return true;
- } catch (IOException ex) {
- System.out.println("Error: " + ex.getMessage());
- ex.printStackTrace();
- return false;
- }
- }
- public boolean disconnect() {
- try {
- if (ftpClient.isConnected()) {
- ftpClient.logout();
- ftpClient.disconnect();
- return true;
- }
- return false;
- } catch (IOException ex) {
- ex.printStackTrace();
- return false;
- }
- }
- public boolean transferFile(File file, String filePath) {
- boolean done = false;
- try {
- // APPROACH #1: uploads first file using an InputStream
- File firstLocalFile = file;
- String firstRemoteFile = filePath;
- InputStream inputStream = new FileInputStream(firstLocalFile);
- System.out.println("Start uploading first file");
- done = ftpClient.storeFile(firstRemoteFile, inputStream);
- inputStream.close();
- if (done) {
- System.out.println("The first file is uploaded successfully.");
- }
- } catch (IOException ex) {
- System.out.println("Error: " + ex.getMessage());
- ex.printStackTrace();
- }
- return done;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement