Search in sources :

Example 1 with UploadCommand

use of eu.ggnet.dwoss.mandator.api.service.FtpConfiguration.UploadCommand in project dwoss by gg-net.

the class FtpTransfer method upload.

/**
 * Uploads a some files to a remove ftp host.
 * <p/>
 * @param config  the config for he connection.
 * @param uploads the upload commands
 * @param monitor an optional monitor.
 * @throws java.net.SocketException
 * @throws java.io.IOException
 */
public static void upload(ConnectionConfig config, IMonitor monitor, UploadCommand... uploads) throws SocketException, IOException {
    if (uploads == null || uploads.length == 0)
        return;
    SubMonitor m = SubMonitor.convert(monitor, "FTP Transfer", toWorkSize(uploads) + 4);
    m.message("verbinde");
    m.start();
    FTPClient ftp = new FTPClient();
    ftp.addProtocolCommandListener(PROTOCOL_TO_LOGGER);
    try {
        ftp.connect(config.getHost(), config.getPort());
        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode()))
            throw new IOException("FTPReply.isPositiveCompletion(ftp.getReplyCode()) = false");
        if (!ftp.login(config.getUser(), config.getPass()))
            throw new IOException("Login with " + config.getUser() + " not successful");
        L.info("Connected to {} idenfied by {}", config.getHost(), ftp.getSystemType());
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
        for (UploadCommand upload : uploads) {
            m.worked(1, "uploading to " + upload.getPath());
            ftp.changeWorkingDirectory(upload.getPath());
            deleteFilesByType(ftp, upload.getDeleteFileTypes());
            for (File file : upload.getFiles()) {
                m.worked(1, "uploading to " + upload.getPath() + " file " + file.getName());
                try (InputStream input = new FileInputStream(file)) {
                    if (!ftp.storeFile(file.getName(), input))
                        throw new IOException("Cannot store file " + file + " on server!");
                }
            }
        }
        m.finish();
    } finally {
        // just cleaning up
        try {
            ftp.logout();
        } catch (IOException e) {
        }
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
            }
        }
    }
}
Also used : UploadCommand(eu.ggnet.dwoss.mandator.api.service.FtpConfiguration.UploadCommand) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SubMonitor(eu.ggnet.dwoss.progress.SubMonitor) IOException(java.io.IOException) File(java.io.File) FTPFile(org.apache.commons.net.ftp.FTPFile) FTPClient(org.apache.commons.net.ftp.FTPClient) FileInputStream(java.io.FileInputStream)

Aggregations

UploadCommand (eu.ggnet.dwoss.mandator.api.service.FtpConfiguration.UploadCommand)1 SubMonitor (eu.ggnet.dwoss.progress.SubMonitor)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 FTPClient (org.apache.commons.net.ftp.FTPClient)1 FTPFile (org.apache.commons.net.ftp.FTPFile)1