use of org.apache.commons.net.ftp.FTPClient in project hive by apache.
the class Ftp method openConnection.
/**
* Open and initialize FTP
*/
FTPClient openConnection(HplsqlParser.Copy_from_ftp_stmtContext ctx) {
FTPClient ftp = new FTPClient();
Timer timer = new Timer();
timer.start();
try {
ftp.connect(host);
ftp.enterLocalPassiveMode();
ftp.setFileType(FTP.BINARY_FILE_TYPE);
if (!ftp.login(user, pwd)) {
if (ftp.isConnected()) {
ftp.disconnect();
}
exec.signal(Signal.Type.SQLEXCEPTION, "Cannot login to FTP server: " + host);
return null;
}
timer.stop();
if (info) {
info(ctx, "Connected to ftp: " + host + " (" + timer.format() + ")");
}
} catch (IOException e) {
exec.signal(e);
}
return ftp;
}
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 camel by apache.
the class FtpInitialConnectTimeoutTest method mockedClient.
private FTPClient mockedClient() throws IOException {
FTPClient client = new FTPClient();
client.setSocketFactory(createSocketFactory());
return client;
}
use of org.apache.commons.net.ftp.FTPClient in project azure-tools-for-java by Microsoft.
the class WebAppUtils method deployArtifact.
public static void deployArtifact(String artifactName, String artifactPath, PublishingProfile pp, boolean toRoot, IProgressIndicator indicator) throws IOException {
FTPClient ftp = null;
InputStream input = null;
try {
if (indicator != null)
indicator.setText("Connecting to FTP server...");
ftp = getFtpConnection(pp);
if (indicator != null)
indicator.setText("Uploading the application...");
input = new FileInputStream(artifactPath);
if (toRoot) {
WebAppUtils.removeFtpDirectory(ftp, ftpWebAppsPath + "ROOT", indicator);
ftp.deleteFile(ftpWebAppsPath + "ROOT.war");
ftp.storeFile(ftpWebAppsPath + "ROOT.war", input);
} else {
WebAppUtils.removeFtpDirectory(ftp, ftpWebAppsPath + artifactName, indicator);
ftp.deleteFile(artifactName + ".war");
boolean success = ftp.storeFile(ftpWebAppsPath + artifactName + ".war", input);
if (!success) {
int rc = ftp.getReplyCode();
throw new IOException("FTP client can't store the artifact, reply code: " + rc);
}
}
if (indicator != null)
indicator.setText("Logging out of FTP server...");
ftp.logout();
} finally {
if (input != null)
input.close();
if (ftp != null && ftp.isConnected()) {
ftp.disconnect();
}
}
}
use of org.apache.commons.net.ftp.FTPClient in project opennms by OpenNMS.
the class FtpSystemReportFormatter method end.
@Override
public void end() {
m_zipFormatter.end();
IOUtils.closeQuietly(m_outputStream);
final FTPClient ftp = new FTPClient();
FileInputStream fis = null;
try {
if (m_url.getPort() == -1 || m_url.getPort() == 0 || m_url.getPort() == m_url.getDefaultPort()) {
ftp.connect(m_url.getHost());
} else {
ftp.connect(m_url.getHost(), m_url.getPort());
}
if (m_url.getUserInfo() != null && m_url.getUserInfo().length() > 0) {
final String[] userInfo = m_url.getUserInfo().split(":", 2);
ftp.login(userInfo[0], userInfo[1]);
} else {
ftp.login("anonymous", "opennmsftp@");
}
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
LOG.error("FTP server refused connection.");
return;
}
String path = m_url.getPath();
if (path.endsWith("/")) {
LOG.error("Your FTP URL must specify a filename.");
return;
}
File f = new File(path);
path = f.getParent();
if (!ftp.changeWorkingDirectory(path)) {
LOG.info("unable to change working directory to {}", path);
return;
}
LOG.info("uploading {} to {}", f.getName(), path);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
fis = new FileInputStream(m_zipFile);
if (!ftp.storeFile(f.getName(), fis)) {
LOG.info("unable to store file");
return;
}
LOG.info("finished uploading");
} catch (final Exception e) {
LOG.error("Unable to FTP file to {}", m_url, e);
} finally {
IOUtils.closeQuietly(fis);
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
// do nothing
}
}
}
}
Aggregations