use of org.apache.commons.net.ftp.FTPClient in project Orthanc_Tools by salimkanoun.
the class ExportFiles method export.
public void export() {
FileInputStream fis = null;
try {
File localFile = new File(this.filePath);
switch(chosenOption) {
case OPTION_FTP:
FTPClient client = new FTPClient();
client.connect(this.serverAdress, this.port);
client.login(this.login, this.pwd);
client.enterLocalPassiveMode();
client.setFileType(FTPClient.BINARY_FILE_TYPE);
String remoteFile = this.remotePath + this.remoteFileName;
fis = new FileInputStream(localFile);
client.storeFile(remoteFile, fis);
client.logout();
break;
case OPTION_SFTP:
StandardFileSystemManager manager = new StandardFileSystemManager();
manager.init();
FileObject localFileSFTP = manager.resolveFile(this.filePath);
FileObject remoteFileSFTP = manager.resolveFile("sftp://" + this.login + ":" + this.pwd + "@" + this.serverAdress + ":" + this.port + this.remotePath + this.remoteFileName);
// Copy local file to sftp server
remoteFileSFTP.copyFrom(localFileSFTP, Selectors.SELECT_SELF);
manager.close();
break;
case OPTION_WEBDAV:
fis = new FileInputStream(localFile);
Sardine sardine = SardineFactory.begin(this.login, this.pwd);
sardine.put(this.serverAdress + this.remotePath + this.remoteFileName, fis);
break;
default:
break;
}
} catch (IOException e) {
// Getting the exception message, that we will give to VueAnon with getResult
this.result = e.getMessage();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
use of org.apache.commons.net.ftp.FTPClient in project fess-crawler by codelibs.
the class FtpClient method getResponseData.
protected ResponseData getResponseData(final String uri, final boolean includeContent) {
final ResponseData responseData = new ResponseData();
FTPClient client = null;
try {
responseData.setMethod(Constants.GET_METHOD);
final FtpInfo ftpInfo = new FtpInfo(uri);
responseData.setUrl(ftpInfo.toUrl());
client = getClient(ftpInfo);
FTPFile file = null;
client.changeWorkingDirectory(ftpInfo.getParent());
validateRequest(client);
if (ftpInfo.getName() == null) {
// root directory
final Set<RequestData> requestDataSet = new HashSet<>();
if (includeContent) {
try {
final FTPFile[] files = client.listFiles(ftpInfo.getParent(), FTPFileFilters.NON_NULL);
validateRequest(client);
for (final FTPFile f : files) {
final String chileUri = ftpInfo.toChildUrl(f.getName());
requestDataSet.add(RequestDataBuilder.newRequestData().get().url(chileUri).build());
}
} catch (final IOException e) {
disconnectInternalClient(client);
throw new CrawlingAccessException("Could not access " + uri, e);
}
}
ftpClientQueue.offer(client);
throw new ChildUrlsException(requestDataSet, this.getClass().getName() + "#getResponseData");
}
final FTPFile[] files = client.listFiles(null, FTPFileFilters.NON_NULL);
validateRequest(client);
for (final FTPFile f : files) {
if (ftpInfo.getName().equals(f.getName())) {
file = f;
break;
}
}
updateResponseData(uri, includeContent, responseData, client, ftpInfo, file);
} catch (final CrawlerSystemException e) {
CloseableUtil.closeQuietly(responseData);
throw e;
} catch (final Exception e) {
CloseableUtil.closeQuietly(responseData);
throw new CrawlingAccessException("Could not access " + uri, e);
}
return responseData;
}
use of org.apache.commons.net.ftp.FTPClient in project fess-crawler by codelibs.
the class FtpClient method getClient.
protected FTPClient getClient(final FtpInfo info) throws IOException {
FTPClient ftpClient = ftpClientQueue.poll();
if (ftpClient != null) {
if (ftpClient.isAvailable()) {
return ftpClient;
}
try {
ftpClient.disconnect();
} catch (final Exception e) {
logger.debug("Failed to disconnect " + info.toUrl(), e);
}
}
try {
ftpClient = new FTPClient();
if (activeExternalHost != null) {
ftpClient.setActiveExternalIPAddress(activeExternalHost);
}
if (passiveLocalHost != null) {
ftpClient.setPassiveLocalIPAddress(passiveLocalHost);
}
if (reportActiveExternalHost != null) {
ftpClient.setReportActiveExternalIPAddress(reportActiveExternalHost);
}
if (activeMinPort != -1 && activeMaxPort != -1) {
ftpClient.setActivePortRange(activeMinPort, activeMaxPort);
}
ftpClient.setAutodetectUTF8(autodetectEncoding);
ftpClient.setConnectTimeout(connectTimeout);
ftpClient.setDataTimeout(dataTimeout);
ftpClient.setControlEncoding(controlEncoding);
ftpClient.setBufferSize(bufferSize);
if (passiveNatWorkaround) {
ftpClient.setPassiveNatWorkaroundStrategy(new NatServerResolverImpl(ftpClient));
}
ftpClient.setUseEPSVwithIPv4(useEPSVwithIPv4);
ftpClient.configure(ftpClientConfig);
ftpClient.connect(info.getHost(), info.getPort());
validateRequest(ftpClient);
final FtpAuthentication auth = ftpAuthenticationHolder.get(info.toUrl());
if (auth != null) {
if (!ftpClient.login(auth.getUsername(), auth.getPassword())) {
throw new CrawlerLoginFailureException("Login Failure: " + auth.getUsername() + " for " + info.toUrl());
}
}
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
return ftpClient;
} catch (final IOException e) {
if (ftpClient != null) {
try {
ftpClient.disconnect();
} catch (final Exception e1) {
logger.debug("Failed to disconnect " + info.toUrl(), e);
}
}
throw e;
}
}
use of org.apache.commons.net.ftp.FTPClient in project DataX by alibaba.
the class StandardFtpHelperImpl method loginFtpServer.
@Override
public void loginFtpServer(String host, String username, String password, int port, int timeout) {
this.ftpClient = new FTPClient();
try {
this.ftpClient.setControlEncoding("UTF-8");
// 不需要写死ftp server的OS TYPE,FTPClient getSystemType()方法会自动识别
// this.ftpClient.configure(new FTPClientConfig(FTPClientConfig.SYST_UNIX));
this.ftpClient.setDefaultTimeout(timeout);
this.ftpClient.setConnectTimeout(timeout);
this.ftpClient.setDataTimeout(timeout);
// 连接登录
this.ftpClient.connect(host, port);
this.ftpClient.login(username, password);
this.ftpClient.enterRemotePassiveMode();
this.ftpClient.enterLocalPassiveMode();
int reply = this.ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
this.ftpClient.disconnect();
String message = String.format("与ftp服务器建立连接失败,host:%s, port:%s, username:%s, replyCode:%s", host, port, username, reply);
LOG.error(message);
throw DataXException.asDataXException(FtpWriterErrorCode.FAIL_LOGIN, message);
}
} catch (UnknownHostException e) {
String message = String.format("请确认ftp服务器地址是否正确,无法连接到地址为: [%s] 的ftp服务器, errorMessage:%s", host, e.getMessage());
LOG.error(message);
throw DataXException.asDataXException(FtpWriterErrorCode.FAIL_LOGIN, message, e);
} catch (IllegalArgumentException e) {
String message = String.format("请确认连接ftp服务器端口是否正确,错误的端口: [%s], errorMessage:%s", port, e.getMessage());
LOG.error(message);
throw DataXException.asDataXException(FtpWriterErrorCode.FAIL_LOGIN, message, e);
} catch (Exception e) {
String message = String.format("与ftp服务器建立连接失败,host:%s, port:%s, username:%s, errorMessage:%s", host, port, username, e.getMessage());
LOG.error(message);
throw DataXException.asDataXException(FtpWriterErrorCode.FAIL_LOGIN, message, e);
}
}
use of org.apache.commons.net.ftp.FTPClient in project irontest by zheng-wang.
the class FTPTeststepRunner method put.
private void put(Endpoint endpoint, FtpPutRequest ftpPutRequest) throws IOException {
String username = StringUtils.trimToEmpty(endpoint.getUsername());
String remoteFilePath = StringUtils.trimToEmpty(ftpPutRequest.getRemoteFilePath());
byte[] fileBytes;
// validate arguments
if ("".equals(username)) {
throw new IllegalArgumentException("Username not specified in Endpoint.");
} else if ("".equals(remoteFilePath)) {
throw new IllegalArgumentException("Target File Path not specified.");
}
if (ftpPutRequest instanceof FtpPutRequestFileFromText) {
FtpPutRequestFileFromText ftpPutRequestFileFromText = (FtpPutRequestFileFromText) ftpPutRequest;
String fileContent = ftpPutRequestFileFromText.getFileContent();
// validate arguments
if ("".equals(StringUtils.trimToEmpty(fileContent))) {
throw new IllegalArgumentException("No file content.");
}
fileBytes = fileContent.getBytes();
} else {
FtpPutRequestFileFromFile ftpPutRequestFileFromFile = (FtpPutRequestFileFromFile) ftpPutRequest;
fileBytes = ftpPutRequestFileFromFile.getFileContent();
// validate arguments
if (fileBytes == null || fileBytes.length == 0) {
throw new IllegalArgumentException("No file content.");
}
}
FTPEndpointProperties endpointProperties = (FTPEndpointProperties) endpoint.getOtherProperties();
String password = getDecryptedEndpointPassword();
FTPClient ftpClient;
if (endpointProperties.isUseSSL()) {
ftpClient = new FTPSClient();
((FTPSClient) ftpClient).setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());
} else {
ftpClient = new FTPClient();
}
ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
ftpClient.addProtocolCommandListener(new ProtocolCommandListener() {
@Override
public void protocolCommandSent(ProtocolCommandEvent event) {
}
@Override
public void protocolReplyReceived(ProtocolCommandEvent event) {
if (FTPReply.isNegativePermanent(event.getReplyCode())) {
throw new RuntimeException("Failed to put the file. " + event.getMessage());
}
}
});
try {
ftpClient.connect(endpoint.getHost(), endpoint.getPort());
ftpClient.login(username, password);
ftpClient.enterLocalPassiveMode();
if (endpointProperties.isUseSSL()) {
((FTPSClient) ftpClient).execPROT("P");
}
ftpClient.storeFile(remoteFilePath, new ByteArrayInputStream(fileBytes));
} finally {
ftpClient.disconnect();
}
}
Aggregations