com.connection.factory.FtpConnectionApacheLib.java Source code

Java tutorial

Introduction

Here is the source code for com.connection.factory.FtpConnectionApacheLib.java

Source

/*
 * 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 com.connection.factory;

import com.connection.exception.ConnectionException;
import com.connection.settings.FileInfoEnum;
import com.connection.settings.FileListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

/**
 *
 * @author EnesTerzi
 */
public class FtpConnectionApacheLib extends Connection {

    FTPClient _ftpObj = new FTPClient();

    protected FtpConnectionApacheLib(String _ipAdress, String _userName, String _passWord, Integer _port) {
        super(_ipAdress, _userName, _passWord, _port);
    }

    public void setFileType(int type) throws IOException {
        _ftpObj.setFileType(type);
    }

    public void setTransferMode(int mode) throws IOException {
        _ftpObj.setFileTransferMode(mode);
    }

    @Override
    public boolean getConnection() {
        boolean status;
        try {
            _ftpObj.connect(_ipAdress, _port);
            //  _ftpObj.setFileType(FTP.COMPRESSED_TRANSFER_MODE);
            _ftpObj.enterLocalPassiveMode();
            isConnected = true;
            status = true;
        } catch (IOException ex) {
            System.out.println(ex.toString());
            status = false;
        }
        if (status) {
            try {
                status = _ftpObj.login(_userName, _passWord);
                isLogin = true;
            } catch (IOException e) {
                System.out.println(e.toString());
                status = false;
            }
        }
        return status;
    }

    @Override
    public boolean downloadFile(String fullLocalPad, String fullRemotePath) {
        InputStream input = null;
        try {
            input = _ftpObj.retrieveFileStream(fullRemotePath);
            int returnCode = _ftpObj.getReplyCode();
            if (input == null || returnCode == 550) {
                return false;
            }
            try (FileOutputStream output = new FileOutputStream(fullLocalPad)) {
                byte[] bytesArray = new byte[CURRENT_FILE_BYTE_BUFFER];
                int bytesRead;
                while ((bytesRead = input.read(bytesArray)) != -1) {
                    output.write(bytesArray, 0, bytesRead);
                }
            }
            input.close();
            _ftpObj.completePendingCommand();
            return true;
        } catch (IOException ex) {
            return false;
        }

    }

    @Override
    public boolean closeConnection() {
        try {
            if (isLogin) {
                _ftpObj.logout();
                isLogin = false;
            }
            if (isConnected) {
                _ftpObj.disconnect();
                isConnected = false;
            }
            return true;
        } catch (IOException ex) {
            return false;
        }
    }

    @Override
    public boolean checkConnectionisAlive() {
        return isConnected && isLogin;
    }

    @Override
    public Object getConnectionObject() {
        return _ftpObj;
    }

    @Override
    public List<RemoteFileObject> readAllFilesWalkinPath(String remotePath) {
        List<RemoteFileObject> willReturnObject = new ArrayList<>();
        Queue<RemoteFileObject> directorylist = new LinkedBlockingQueue<>();
        RemoteFileObject object = null;
        object = new FtpApacheFileObject(FileInfoEnum.DIRECTORY);
        object.setDirectPath(remotePath);
        directorylist.add(object);
        try {
            while (!directorylist.isEmpty()) {
                object = directorylist.poll();
                FTPFile[] fileListTemp = _ftpObj.listFiles(object.getPath());
                for (FTPFile each : fileListTemp) {
                    RemoteFileObject objectTemp = null;
                    if (each.isDirectory()) {
                        objectTemp = new FtpApacheFileObject(FileInfoEnum.DIRECTORY);
                        objectTemp.setFileName(each.getName());
                        objectTemp.setAbsolutePath(object.getPath());
                        directorylist.add(objectTemp);
                    } else if (each.isFile()) {
                        objectTemp = new FtpApacheFileObject(FileInfoEnum.FILE);
                        objectTemp.setFileName(each.getName());
                        objectTemp.setAbsolutePath(object.getPath());
                        objectTemp.setFileSize(each.getSize());
                        objectTemp.setFileType();
                        objectTemp.setDate(each.getTimestamp().getTime());
                        willReturnObject.add(objectTemp);
                    }
                }
                object = null;
                fileListTemp = null;
            }

        } catch (IOException ex) {
            return null;
        } catch (ConnectionException ex) {

        }
        return willReturnObject;
    }

    @Override
    public void readAllFilesWalkingPathWithListener(FileListener listener, String remotePath) {
        // List<RemoteFileObject> willReturnObject = new ArrayList<>();
        Queue<RemoteFileObject> directorylist = new LinkedBlockingQueue<>();
        RemoteFileObject object = null;
        object = new FtpApacheFileObject(FileInfoEnum.DIRECTORY);
        object.setDirectPath(remotePath);
        directorylist.add(object);
        try {
            while (!directorylist.isEmpty()) {
                object = directorylist.poll();
                FTPFile[] fileListTemp = _ftpObj.listFiles(object.getPath());
                for (FTPFile each : fileListTemp) {
                    RemoteFileObject objectTemp = null;
                    if (each.isDirectory()) {
                        objectTemp = new FtpApacheFileObject(FileInfoEnum.DIRECTORY);
                        objectTemp.setFileName(each.getName());
                        objectTemp.setAbsolutePath(object.getPath());
                        directorylist.add(objectTemp);
                    } else if (each.isFile()) {
                        objectTemp = new FtpApacheFileObject(FileInfoEnum.FILE);
                        objectTemp.setFileName(each.getName());
                        objectTemp.setAbsolutePath(object.getPath());
                        objectTemp.setFileSize(each.getSize());
                        objectTemp.setFileType();
                        objectTemp.setDate(each.getTimestamp().getTime());
                        listener.handleRemoteFile(object);
                    }
                }
                object = null;
                fileListTemp = null;
            }

        } catch (IOException | ConnectionException ex) {
            //    return null;
        }
        //  return willReturnObject;

        //  return willReturnObject;
    }

    @Override
    public void readAllFilesInCurrentPathWithListener(FileListener listener, String remotePath) {

        try {
            FTPFile[] fileListTemp = _ftpObj.listFiles(remotePath);
            for (FTPFile each : fileListTemp) {
                RemoteFileObject objectTemp = null;
                if (each.isFile()) {
                    objectTemp = new FtpApacheFileObject(FileInfoEnum.FILE);
                    objectTemp.setFileName(each.getName());
                    objectTemp.setAbsolutePath(remotePath);
                    objectTemp.setFileSize(each.getSize());
                    objectTemp.setFileType();
                    objectTemp.setDate(each.getTimestamp().getTime());
                    listener.handleRemoteFile(objectTemp);
                }
            }
        } catch (IOException | ConnectionException ex) {

        }

    }

    @Override
    public List<RemoteFileObject> readAllFilesInCurrentPath(String remotePath) {
        List<RemoteFileObject> willReturnObject = new ArrayList<>();
        try {
            FTPFile[] fileListTemp = _ftpObj.listFiles(remotePath);
            for (FTPFile each : fileListTemp) {
                RemoteFileObject objectTemp = null;
                if (each.isFile()) {

                    objectTemp = new FtpApacheFileObject(FileInfoEnum.FILE);
                    //  System.out.println(each);
                    objectTemp.setFileName(each.getName());
                    objectTemp.setAbsolutePath(remotePath);
                    objectTemp.setFileSize(each.getSize());
                    objectTemp.setFileType();
                    objectTemp.setDate(each.getTimestamp().getTime());
                    willReturnObject.add(objectTemp);
                }
            }
        } catch (IOException | ConnectionException ex) {
            return null;
        }
        return willReturnObject.isEmpty() ? null : willReturnObject;
    }

    @Override
    public boolean uploadFile(InputStream input, String fileName, String remotePath) {
        try {
            OutputStream outStream = _ftpObj.storeFileStream(remotePath + fileName);
            byte[] block = new byte[CURRENT_FILE_BYTE_BUFFER];
            int len = 0;
            while ((len = input.read(block)) != -1) {
                outStream.write(block, 0, len);
                outStream.flush();
            }
            outStream.close();
            input.close();
            return true;
        } catch (IOException ex) {
            return false;
        }
    }

    @Override
    public boolean uploadFile(File input, String remotePath) {
        try {
            InputStream inputStream = new FileInputStream(input);
            OutputStream outStream = _ftpObj.storeFileStream(remotePath + input.getName());
            byte[] block = new byte[CURRENT_FILE_BYTE_BUFFER];
            int len = 0;
            while ((len = inputStream.read(block)) != -1) {
                outStream.write(block, 0, len);
                outStream.flush();
            }
            outStream.close();
            _ftpObj.completePendingCommand();
            inputStream.close();
            return true;
        } catch (IOException ex) {
            return false;
        }
    }

    @Override
    public boolean downloadFileWithTtgOutputStreamForXml(String fullRemotePath, ModifiedOutputStream output) {
        InputStream input = null;
        try {
            input = _ftpObj.retrieveFileStream(fullRemotePath);
            int returnCode = _ftpObj.getReplyCode();
            if (input == null || returnCode == 550) {
                return false;
            }

            byte[] bytesArray = new byte[CURRENT_FILE_BYTE_BUFFER];
            int bytesRead = 0;
            while ((bytesRead = input.read(bytesArray)) != -1) {
                output.checkXmlAndWrite(bytesArray, 0, bytesRead);
            }
            output.close();
            input.close();
            _ftpObj.completePendingCommand();
            return true;
        } catch (IOException ex) {
            try {
                output.close();

            } catch (IOException ex1) {

            }
            return false;
        }
    }

}