use of org.apache.commons.net.ftp.FTPClient in project ant by apache.
the class FTPTaskMirrorImpl method doFTP.
@Override
public void doFTP() throws BuildException {
FTPClient ftp = null;
try {
task.log("Opening FTP connection to " + task.getServer(), Project.MSG_VERBOSE);
ftp = new FTPClient();
if (task.isConfigurationSet()) {
ftp = FTPConfigurator.configure(ftp, task);
}
ftp.setRemoteVerificationEnabled(task.getEnableRemoteVerification());
ftp.connect(task.getServer(), task.getPort());
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
throw new BuildException("FTP connection failed: %s", ftp.getReplyString());
}
task.log("connected", Project.MSG_VERBOSE);
task.log("logging in to FTP server", Project.MSG_VERBOSE);
if ((task.getAccount() != null && !ftp.login(task.getUserid(), task.getPassword(), task.getAccount())) || (task.getAccount() == null && !ftp.login(task.getUserid(), task.getPassword()))) {
throw new BuildException("Could not login to FTP server");
}
task.log("login succeeded", Project.MSG_VERBOSE);
if (task.isBinary()) {
ftp.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
throw new BuildException("could not set transfer type: %s", ftp.getReplyString());
}
} else {
ftp.setFileType(org.apache.commons.net.ftp.FTP.ASCII_FILE_TYPE);
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
throw new BuildException("could not set transfer type: %s", ftp.getReplyString());
}
}
if (task.isPassive()) {
task.log("entering passive mode", Project.MSG_VERBOSE);
ftp.enterLocalPassiveMode();
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
throw new BuildException("could not enter into passive mode: %s", ftp.getReplyString());
}
}
// a legacy file system.
if (task.getInitialSiteCommand() != null) {
final FTPClient lftp = ftp;
executeRetryable(new RetryHandler(task.getRetriesAllowed(), task), () -> doSiteCommand(lftp, task.getInitialSiteCommand()), "initial site command: " + task.getInitialSiteCommand());
}
if (task.getUmask() != null) {
final FTPClient lftp = ftp;
executeRetryable(new RetryHandler(task.getRetriesAllowed(), task), () -> doSiteCommand(lftp, "umask " + task.getUmask()), "umask " + task.getUmask());
}
if (task.getAction() == FTPTask.MK_DIR) {
final FTPClient lftp = ftp;
executeRetryable(new RetryHandler(task.getRetriesAllowed(), task), () -> makeRemoteDir(lftp, task.getRemotedir()), task.getRemotedir());
} else if (task.getAction() == FTPTask.SITE_CMD) {
final FTPClient lftp = ftp;
executeRetryable(new RetryHandler(task.getRetriesAllowed(), task), () -> doSiteCommand(lftp, task.getSiteCommand()), "Site Command: " + task.getSiteCommand());
} else {
if (task.getRemotedir() != null) {
task.log("changing the remote directory", Project.MSG_VERBOSE);
ftp.changeWorkingDirectory(task.getRemotedir());
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
throw new BuildException("could not change remote directory: %s", ftp.getReplyString());
}
}
if (task.isNewer() && task.isTimeDiffAuto()) {
// in this case we want to find how much time span there is between local
// and remote
task.setTimeDiffMillis(getTimeDiff(ftp));
}
task.log(FTPTask.ACTION_STRS[task.getAction()] + " " + FTPTask.ACTION_TARGET_STRS[task.getAction()]);
transferFiles(ftp);
}
} catch (IOException ex) {
throw new BuildException("error during FTP transfer: " + ex, ex);
} finally {
if (ftp != null && ftp.isConnected()) {
try {
task.log("disconnecting", Project.MSG_VERBOSE);
ftp.logout();
ftp.disconnect();
} catch (IOException ex) {
// ignore it
}
}
}
}
use of org.apache.commons.net.ftp.FTPClient in project ant by apache.
the class FTPTest method setUp.
@Before
public void setUp() {
buildRule.configureProject("src/etc/testcases/taskdefs/optional/net/ftp.xml");
Project project = buildRule.getProject();
project.executeTarget("setup");
tmpDir = project.getProperty("tmp.dir");
ftp = new FTPClient();
ftpFileSep = project.getProperty("ftp.filesep");
myFTPTask.setSeparator(ftpFileSep);
myFTPTask.setProject(project);
remoteTmpDir = myFTPTask.resolveFile(tmpDir);
String remoteHost = project.getProperty("ftp.host");
int port = Integer.parseInt(project.getProperty("ftp.port"));
String remoteUser = project.getProperty("ftp.user");
String password = project.getProperty("ftp.password");
boolean connectionSucceeded = false;
try {
ftp.connect(remoteHost, port);
connectionSucceeded = true;
} catch (Exception ex) {
loginFailureMessage = "could not connect to host " + remoteHost + " on port " + port;
}
if (connectionSucceeded) {
try {
ftp.login(remoteUser, password);
loginSucceeded = true;
} catch (IOException ioe) {
loginFailureMessage = "could not log on to " + remoteHost + " as user " + remoteUser;
}
}
}
use of org.apache.commons.net.ftp.FTPClient in project DataX by alibaba.
the class StandardFtpHelper method loginFtpServer.
@Override
public void loginFtpServer(String host, String username, String password, int port, int timeout, String connectMode) {
ftpClient = new FTPClient();
try {
// 连接
ftpClient.connect(host, port);
// 登录
ftpClient.login(username, password);
// 不需要写死ftp server的OS TYPE,FTPClient getSystemType()方法会自动识别
// ftpClient.configure(new FTPClientConfig(FTPClientConfig.SYST_UNIX));
ftpClient.setConnectTimeout(timeout);
ftpClient.setDataTimeout(timeout);
if ("PASV".equals(connectMode)) {
ftpClient.enterRemotePassiveMode();
ftpClient.enterLocalPassiveMode();
} else if ("PORT".equals(connectMode)) {
ftpClient.enterLocalActiveMode();
// ftpClient.enterRemoteActiveMode(host, port);
}
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
String message = String.format("与ftp服务器建立连接失败,请检查用户名和密码是否正确: [%s]", "message:host =" + host + ",username = " + username + ",port =" + port);
LOG.error(message);
throw DataXException.asDataXException(FtpReaderErrorCode.FAIL_LOGIN, message);
}
// 设置命令传输编码
String fileEncoding = System.getProperty("file.encoding");
ftpClient.setControlEncoding(fileEncoding);
} catch (UnknownHostException e) {
String message = String.format("请确认ftp服务器地址是否正确,无法连接到地址为: [%s] 的ftp服务器", host);
LOG.error(message);
throw DataXException.asDataXException(FtpReaderErrorCode.FAIL_LOGIN, message, e);
} catch (IllegalArgumentException e) {
String message = String.format("请确认连接ftp服务器端口是否正确,错误的端口: [%s] ", port);
LOG.error(message);
throw DataXException.asDataXException(FtpReaderErrorCode.FAIL_LOGIN, message, e);
} catch (Exception e) {
String message = String.format("与ftp服务器建立连接失败 : [%s]", "message:host =" + host + ",username = " + username + ",port =" + port);
LOG.error(message);
throw DataXException.asDataXException(FtpReaderErrorCode.FAIL_LOGIN, message, e);
}
}
use of org.apache.commons.net.ftp.FTPClient in project jmeter by apache.
the class FTPSampler method sample.
@Override
public SampleResult sample(Entry e) {
SampleResult res = new SampleResult();
// Assume failure
res.setSuccessful(false);
String remote = getRemoteFilename();
String local = getLocalFilename();
boolean binaryTransfer = isBinaryMode();
res.setSampleLabel(getName());
final String label = getLabel();
res.setSamplerData(label);
try {
res.setURL(new URL(label));
} catch (MalformedURLException e1) {
log.warn("Cannot set URL: " + e1.getLocalizedMessage());
}
InputStream input = null;
FileInputStream fileIS = null;
res.sampleStart();
FTPClient ftp = new FTPClient();
try {
savedClient = ftp;
final int port = getPortAsInt();
if (port > 0) {
ftp.connect(getServer(), port);
} else {
ftp.connect(getServer());
}
res.latencyEnd();
int reply = ftp.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
if (ftp.login(getUsername(), getPassword())) {
if (binaryTransfer) {
ftp.setFileType(FTP.BINARY_FILE_TYPE);
}
// should probably come from the setup dialog
ftp.enterLocalPassiveMode();
boolean ftpOK = false;
if (isUpload()) {
String contents = getLocalFileContents();
if (contents.length() > 0) {
@SuppressWarnings("DefaultCharset") byte[] // TODO - charset?
bytes = contents.getBytes();
input = new ByteArrayInputStream(bytes);
res.setSentBytes((long) bytes.length);
} else {
File infile = new File(local);
res.setSentBytes(infile.length());
// NOSONAR False positive, fileIS is closed in finally and not overwritten
fileIS = new FileInputStream(infile);
input = new BufferedInputStream(fileIS);
}
ftpOK = ftp.storeFile(remote, input);
} else {
final boolean saveResponse = isSaveResponse();
// No need to close this
ByteArrayOutputStream baos = null;
OutputStream target = null;
OutputStream output = null;
try {
if (saveResponse) {
baos = new ByteArrayOutputStream();
target = baos;
}
if (local.length() > 0) {
// NOSONAR False positive, the output is closed in finally and not overwritten
output = new FileOutputStream(local);
if (target == null) {
target = output;
} else {
target = new TeeOutputStream(output, baos);
}
}
if (target == null) {
target = NullOutputStream.NULL_OUTPUT_STREAM;
}
input = ftp.retrieveFileStream(remote);
if (input == null) {
// Could not access file or other error
res.setResponseCode(Integer.toString(ftp.getReplyCode()));
res.setResponseMessage(ftp.getReplyString());
} else {
long bytes = IOUtils.copy(input, target);
ftpOK = bytes > 0;
if (saveResponse) {
saveResponse(res, binaryTransfer, baos);
} else {
res.setBytes(bytes);
}
}
} finally {
IOUtils.closeQuietly(target, null);
IOUtils.closeQuietly(output, null);
}
}
if (ftpOK) {
res.setResponseCodeOK();
res.setResponseMessageOK();
res.setSuccessful(true);
} else {
res.setResponseCode(Integer.toString(ftp.getReplyCode()));
res.setResponseMessage(ftp.getReplyString());
}
} else {
res.setResponseCode(Integer.toString(ftp.getReplyCode()));
res.setResponseMessage(ftp.getReplyString());
}
} else {
res.setResponseCode("501");
String replyString = ftp.getReplyString();
if (StringUtils.isEmpty(replyString)) {
res.setResponseMessage("Could not connect");
} else {
res.setResponseMessage(replyString);
}
}
} catch (IOException ex) {
res.setResponseCode("000");
res.setResponseMessage(ex.toString());
} finally {
savedClient = null;
if (ftp.isConnected()) {
try {
ftp.logout();
} catch (IOException ignored) {
// NOOP
}
try {
ftp.disconnect();
} catch (IOException ignored) {
// NOOP
}
}
IOUtils.closeQuietly(input, null);
IOUtils.closeQuietly(fileIS, null);
}
res.sampleEnd();
return res;
}
use of org.apache.commons.net.ftp.FTPClient in project syndesis-qe by syndesisio.
the class FtpClientManager method initClient.
private FTPClient initClient() {
FTPClient ftpClient = new FTPClient();
TestUtils.withRetry(() -> {
try {
ftpClient.connect(ftpServer, ftpLocalPort);
ftpClient.login(ftpUser, ftpPass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
ftpClient.setDataTimeout(500000);
return true;
} catch (IOException e) {
log.error("Unable to connect FTP client: " + e.getMessage());
return false;
}
}, 3, 30000L, "Unable to create FTP client after 3 retries");
return ftpClient;
}
Aggregations