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) {
}
}
}
}
Aggregations