All Downloads are FREE. Search and download functionalities are using the official Maven repository.

es.iti.wakamiti.fileuploader.FTPClientTransmitter Maven / Gradle / Ivy

There is a newer version: 2.8.0
Show newest version
package es.iti.wakamiti.fileuploader;

import es.iti.wakamiti.api.WakamitiAPI;
import es.iti.wakamiti.api.WakamitiException;
import es.iti.wakamiti.api.util.ResourceLoader;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPSClient;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

public class FTPClientTransmitter implements FTPTransmitter {

    private final FTPClient ftpClient;

    public FTPClientTransmitter(boolean secure) {
        this.ftpClient = (secure ? new FTPSClient() : new FTPClient());
    }


    @Override
    public boolean isConnected() {
        return ftpClient.isConnected();
    }


    @Override
    public void connect(String username, String host, Integer port, String password, String identity) throws IOException {
        if (port != null) {
            ftpClient.connect(host, port);
        } else {
            ftpClient.connect(host);
        }
        if (password == null) {
            throw new WakamitiException("Password is required");
        }
        ftpClient.login(username,password);
    }




    @Override
    public void disconnect() throws IOException {
        ftpClient.disconnect();
    }


    @Override
    public void transferFile(Path localFile, Path destinationFolder) throws IOException {
        createDestinationDirectory(destinationFolder);
        ftpClient.changeWorkingDirectory(destinationFolder.toString());
        String fileName = localFile.toFile().getName();
        ResourceLoader resourceLoader = WakamitiAPI.instance().resourceLoader();
        try (InputStream inputStream = Files.newInputStream(resourceLoader.absolutePath(localFile))) {
            ftpClient.storeFile(fileName, inputStream);
        }
    }



    private void createDestinationDirectory(Path dirPath) throws IOException {
        if (dirPath.getParent() != null) {
            createDestinationDirectory(dirPath.getParent());
        }
        if (!ftpClient.changeWorkingDirectory(dirPath.toString())) {
            ftpClient.makeDirectory(dirPath.toString());
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy