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 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
}
}
}
}
use of org.apache.commons.net.ftp.FTPClient in project azure-sdk-for-java by Azure.
the class Utils method uploadFileToFtp.
/**
* Uploads a file to an Azure web app.
* @param profile the publishing profile for the web app.
* @param fileName the name of the file on server
* @param file the local file
*/
public static void uploadFileToFtp(PublishingProfile profile, String fileName, InputStream file) {
FTPClient ftpClient = new FTPClient();
String[] ftpUrlSegments = profile.ftpUrl().split("/", 2);
String server = ftpUrlSegments[0];
String path = "./site/wwwroot/webapps";
if (fileName.contains("/")) {
int lastslash = fileName.lastIndexOf('/');
path = path + "/" + fileName.substring(0, lastslash);
fileName = fileName.substring(lastslash);
}
try {
ftpClient.connect(server);
ftpClient.login(profile.ftpUsername(), profile.ftpPassword());
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.changeWorkingDirectory(path);
ftpClient.storeFile(fileName, file);
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
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 adempiere by adempiere.
the class MMediaServer method deploy.
// MMediaServer
/**
* (Re-)Deploy all media
* @param media array of media to deploy
* @return true if deployed
*/
public boolean deploy(MMedia[] media) {
// Check whether the host is our example localhost, we will not deploy locally, but this is no error
if (this.getIP_Address().equals("127.0.0.1") || this.getName().equals("localhost")) {
log.warning("You have not defined your own server, we will not really deploy to localhost!");
return true;
}
FTPClient ftp = new FTPClient();
try {
ftp.connect(getIP_Address());
if (ftp.login(getUserName(), getPassword()))
log.info("Connected to " + getIP_Address() + " as " + getUserName());
else {
log.warning("Could NOT connect to " + getIP_Address() + " as " + getUserName());
return false;
}
} catch (Exception e) {
log.log(Level.WARNING, "Could NOT connect to " + getIP_Address() + " as " + getUserName(), e);
return false;
}
boolean success = true;
String cmd = null;
// List the files in the directory
try {
cmd = "cwd";
ftp.changeWorkingDirectory(getFolder());
//
cmd = "list";
String[] fileNames = ftp.listNames();
log.log(Level.FINE, "Number of files in " + getFolder() + ": " + fileNames.length);
/*
FTPFile[] files = ftp.listFiles();
log.config("Number of files in " + getFolder() + ": " + files.length);
for (int i = 0; i < files.length; i++)
log.fine(files[i].getTimestamp() + " \t" + files[i].getName());*/
//
cmd = "bin";
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
//
for (int i = 0; i < media.length; i++) {
if (!media[i].isSummary()) {
log.log(Level.INFO, " Deploying Media Item:" + media[i].get_ID() + media[i].getExtension());
MImage thisImage = media[i].getImage();
// Open the file and output streams
byte[] buffer = thisImage.getData();
ByteArrayInputStream is = new ByteArrayInputStream(buffer);
String fileName = media[i].get_ID() + media[i].getExtension();
cmd = "put " + fileName;
ftp.storeFile(fileName, is);
is.close();
}
}
} catch (Exception e) {
log.log(Level.WARNING, cmd, e);
success = false;
}
// Logout from the FTP Server and disconnect
try {
cmd = "logout";
ftp.logout();
cmd = "disconnect";
ftp.disconnect();
} catch (Exception e) {
log.log(Level.WARNING, cmd, e);
}
ftp = null;
return success;
}
Aggregations