List of usage examples for org.apache.commons.net.ftp FTPClient connect
public void connect(InetAddress host) throws SocketException, IOException
From source file:net.audumla.climate.bom.BOMDataLoader.java
private synchronized FTPClient getFTPClient(String host) { FTPClient ftp = ftpClients.get(host); if (ftp == null || !ftp.isAvailable() || !ftp.isConnected()) { ftp = new FTPClient(); FTPClientConfig config = new FTPClientConfig(); ftp.configure(config);// w w w . ja v a2s .c o m try { ftp.setControlKeepAliveTimeout(30); ftp.setControlKeepAliveReplyTimeout(5); ftp.setDataTimeout(3000); ftp.setDefaultTimeout(1000); int reply; ftp.connect(host); LOG.debug("Connected to " + host); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); LOG.error("FTP server '" + host + "' refused connection."); } else { if (!ftp.login("anonymous", "guest")) { LOG.error("Unable to login to server " + host); } ftp.setSoTimeout(60000); ftp.enterLocalPassiveMode(); ftp.setFileType(FTPClient.BINARY_FILE_TYPE); } } catch (IOException e) { LOG.error("Unable to connect to " + host, e); } ftpClients.put(host, ftp); } if (!ftp.isConnected() || !ftp.isAvailable()) { throw new UnsupportedOperationException("Cannot connect to " + host); } return ftp; }
From source file:edu.wisc.ssec.mcidasv.data.cyclone.AtcfStormDataSource.java
/** * _more_//w ww . j a v a2 s .c o m * * @param file * _more_ * @param ignoreErrors * _more_ * * @return _more_ * * @throws Exception * _more_ */ private byte[] readFile(String file, boolean ignoreErrors) throws Exception { if (new File(file).exists()) { return IOUtil.readBytes(IOUtil.getInputStream(file, getClass())); } if (!file.startsWith("ftp:")) { if (ignoreErrors) { return null; } throw new FileNotFoundException("Could not read file: " + file); } URL url = new URL(file); FTPClient ftp = new FTPClient(); try { ftp.connect(url.getHost()); ftp.login("anonymous", "password"); ftp.setFileType(FTP.IMAGE_FILE_TYPE); ftp.enterLocalPassiveMode(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); if (ftp.retrieveFile(url.getPath(), bos)) { return bos.toByteArray(); } else { throw new IOException("Unable to retrieve file:" + url); } } catch (org.apache.commons.net.ftp.FTPConnectionClosedException fcce) { System.err.println("ftp error:" + fcce); System.err.println(ftp.getReplyString()); if (!ignoreErrors) { throw fcce; } return null; } catch (Exception exc) { if (!ignoreErrors) { throw exc; } return null; } finally { try { ftp.logout(); } catch (Exception exc) { } try { ftp.disconnect(); } catch (Exception exc) { } } }
From source file:com.limegroup.gnutella.archive.ArchiveContribution.java
/** * /*from w ww. j a va 2 s . co m*/ * @throws UnknownHostException * If the hostname cannot be resolved. * * @throws SocketException * If the socket timeout could not be set. * * @throws FTPConnectionClosedException * If the connection is closed by the server. * * @throws LoginFailedException * If the login fails. * * @throws DirectoryChangeFailedException * If changing to the directory provided by the internet * archive fails. * * @throws CopyStreamException * If an I/O error occurs while in the middle of * transferring a file. * * @throws IOException * If an I/O error occurs while sending a command or * receiving a reply from the server * * @throws IllegalStateException * If the contribution object is not ready to upload * (no username, password, server, etc. set) * or if java's xml parser is configured badly */ public void upload() throws UnknownHostException, SocketException, FTPConnectionClosedException, LoginFailedException, DirectoryChangeFailedException, CopyStreamException, RefusedConnectionException, IOException { final int NUM_XML_FILES = 2; final String META_XML_SUFFIX = "_meta.xml"; final String FILES_XML_SUFFIX = "_files.xml"; final String username = getUsername(); final String password = getPassword(); if (getFtpServer() == null) { throw new IllegalStateException("ftp server not set"); } if (getFtpPath() == null) { throw new IllegalStateException("ftp path not set"); } if (username == null) { throw new IllegalStateException("username not set"); } if (password == null) { throw new IllegalStateException("password not set"); } // calculate total number of files and bytes final String metaXmlString = serializeDocument(getMetaDocument()); final String filesXmlString = serializeDocument(getFilesDocument()); final byte[] metaXmlBytes = metaXmlString.getBytes(); final byte[] filesXmlBytes = filesXmlString.getBytes(); final int metaXmlLength = metaXmlBytes.length; final int filesXmlLength = filesXmlBytes.length; final Collection files = getFiles(); final int totalFiles = NUM_XML_FILES + files.size(); final String[] fileNames = new String[totalFiles]; final long[] fileSizes = new long[totalFiles]; final String metaXmlName = getIdentifier() + META_XML_SUFFIX; fileNames[0] = metaXmlName; fileSizes[0] = metaXmlLength; final String filesXmlName = getIdentifier() + FILES_XML_SUFFIX; fileNames[1] = filesXmlName; fileSizes[1] = filesXmlLength; int j = 2; for (Iterator i = files.iterator(); i.hasNext();) { final File f = (File) i.next(); fileNames[j] = f.getRemoteFileName(); fileSizes[j] = f.getFileSize(); j++; } // init the progress mapping for (int i = 0; i < fileSizes.length; i++) { _fileNames2Progress.put(fileNames[i], new UploadFileProgress(fileSizes[i])); _totalUploadSize += fileSizes[i]; } FTPClient ftp = new FTPClient(); try { // first connect if (isCancelled()) { return; } ftp.enterLocalPassiveMode(); if (isCancelled()) { return; } ftp.connect(getFtpServer()); final int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { throw new RefusedConnectionException(getFtpServer() + "refused FTP connection"); } // now login if (isCancelled()) { return; } if (!ftp.login(username, password)) { throw new LoginFailedException(); } try { // try to change the directory if (!ftp.changeWorkingDirectory(getFtpPath())) { // if changing fails, make the directory if (!isFtpDirPreMade() && !ftp.makeDirectory(getFtpPath())) { throw new DirectoryChangeFailedException(); } // now change directory, if it fails again bail if (isCancelled()) { return; } if (!ftp.changeWorkingDirectory(getFtpPath())) { throw new DirectoryChangeFailedException(); } } if (isCancelled()) { return; } connected(); // upload xml files uploadFile(metaXmlName, new ByteArrayInputStream(metaXmlBytes), ftp); uploadFile(filesXmlName, new ByteArrayInputStream(filesXmlBytes), ftp); // now switch to binary mode if (isCancelled()) { return; } ftp.setFileType(FTP.BINARY_FILE_TYPE); // upload contributed files for (final Iterator i = files.iterator(); i.hasNext();) { final File f = (File) i.next(); uploadFile(f.getRemoteFileName(), new FileInputStream(f.getIOFile()), ftp); } } catch (InterruptedIOException ioe) { // we've been requested to cancel return; } finally { ftp.logout(); // we don't care if logging out fails } } finally { try { ftp.disconnect(); } catch (IOException e) { } // don't care if disconnecting fails } // now tell the Internet Archive that we're done if (isCancelled()) { return; } checkinStarted(); if (isCancelled()) { return; } checkin(); if (isCancelled()) { return; } checkinCompleted(); }
From source file:edu.lternet.pasta.dml.download.DownloadHandler.java
/** * Gets content from given source and writes it to DataStorageInterface * to store them. This method will be called by run() * //from ww w .j av a 2 s . c om * @param resourceName the URL to the source data to be retrieved */ protected boolean getContentFromSource(String resourceName) { boolean successFlag = false; QualityCheck onlineURLsQualityCheck = null; boolean onlineURLsException = false; // used to determine status of onlineURLs quality check if (resourceName != null) { resourceName = resourceName.trim(); } if (resourceName != null && (resourceName.startsWith("http://") || resourceName.startsWith("https://") || resourceName.startsWith("file://") || resourceName.startsWith("ftp://"))) { // get the data from a URL int responseCode = 0; String responseMessage = null; try { URL url = new URL(resourceName); boolean isFTP = false; if (entity != null) { String contentType = null; // Find the right MIME type and set it as content type if (resourceName.startsWith("http")) { HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("HEAD"); httpURLConnection.connect(); contentType = httpURLConnection.getContentType(); responseCode = httpURLConnection.getResponseCode(); responseMessage = httpURLConnection.getResponseMessage(); } else if (resourceName.startsWith("file")) { URLConnection urlConnection = url.openConnection(); urlConnection.connect(); contentType = urlConnection.getContentType(); } else { // FTP isFTP = true; contentType = "application/octet-stream"; } entity.setUrlContentType(contentType); } if (!isFTP) { // HTTP(S) or FILE InputStream filestream = url.openStream(); try { successFlag = this.writeRemoteInputStreamIntoDataStorage(filestream); } catch (IOException e) { exception = e; String errorMessage = e.getMessage(); if (errorMessage.startsWith(ONLINE_URLS_EXCEPTION_MESSAGE)) { onlineURLsException = true; } } finally { filestream.close(); } } else { // FTP String[] urlParts = resourceName.split("/"); String address = urlParts[2]; String dir = "/"; for (int i = 3; i < urlParts.length - 1; i++) { dir += urlParts[i] + "/"; } String fileName = urlParts[urlParts.length - 1]; FTPClient ftpClient = new FTPClient(); ftpClient.connect(address); ftpClient.login(ANONYMOUS, anonymousFtpPasswd); ftpClient.changeWorkingDirectory(dir); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); // necessary to avoid firewall blocking InputStream filestream = ftpClient.retrieveFileStream(fileName); try { successFlag = this.writeRemoteInputStreamIntoDataStorage(filestream); } catch (IOException e) { exception = e; String errorMessage = e.getMessage(); if (errorMessage.startsWith(ONLINE_URLS_EXCEPTION_MESSAGE)) { onlineURLsException = true; } } finally { try { filestream.close(); } catch (IOException e) { exception = new DataSourceNotFoundException(String .format("Error closing local file '%s': %s", resourceName, e.getMessage())); onlineURLsException = true; } } // logout and disconnect if FTP session if (resourceName.startsWith("ftp") && ftpClient != null) { try { ftpClient.enterLocalActiveMode(); ftpClient.logout(); ftpClient.disconnect(); } catch (IOException e) { exception = new DataSourceNotFoundException( String.format("Error disconnecting from FTP with resource '%s': %s", resourceName, e.getMessage())); onlineURLsException = true; } } } } catch (MalformedURLException e) { String eClassName = e.getClass().getName(); String eMessage = String.format("%s: %s", eClassName, e.getMessage()); onlineURLsException = true; exception = new DataSourceNotFoundException( String.format("The URL '%s' is a malformed URL: %s", resourceName, eMessage)); } catch (IOException e) { String eClassName = e.getClass().getName(); String eMessage = String.format("%s: %s", eClassName, e.getMessage()); if (responseCode > 0) { eMessage = String.format("Response Code: %d %s; %s", responseCode, responseMessage, eMessage); } onlineURLsException = true; exception = new DataSourceNotFoundException( String.format("The URL '%s' is not reachable: %s", resourceName, eMessage)); } // Initialize the "Online URLs are live" quality check String qualityCheckIdentifier = "onlineURLs"; QualityCheck qualityCheckTemplate = QualityReport.getQualityCheckTemplate(qualityCheckIdentifier); onlineURLsQualityCheck = new QualityCheck(qualityCheckIdentifier, qualityCheckTemplate); if (QualityCheck.shouldRunQualityCheck(entity, onlineURLsQualityCheck)) { String resourceNameEscaped = embedInCDATA(resourceName); if (!onlineURLsException) { onlineURLsQualityCheck.setStatus(Status.valid); onlineURLsQualityCheck.setFound("true"); onlineURLsQualityCheck.setExplanation("Succeeded in accessing URL: " + resourceNameEscaped); } else { onlineURLsQualityCheck.setFailedStatus(); onlineURLsQualityCheck.setFound("false"); String explanation = "Failed to access URL: " + resourceNameEscaped; explanation = explanation + "; " + embedInCDATA(exception.getMessage()); onlineURLsQualityCheck.setExplanation(explanation); } entity.addQualityCheck(onlineURLsQualityCheck); } return successFlag; } else if (resourceName != null && resourceName.startsWith("ecogrid://")) { // get the docid from url int start = resourceName.indexOf("/", 11) + 1; //log.debug("start: " + start); int end = resourceName.indexOf("/", start); if (end == -1) { end = resourceName.length(); } //log.debug("end: " + end); String ecogridIdentifier = resourceName.substring(start, end); // pass this docid and get data item //System.out.println("the endpoint is "+ECOGRIDENDPOINT); //System.out.println("The identifier is "+ecogridIdentifier); //return false; return getContentFromEcoGridSource(ecogridEndPoint, ecogridIdentifier); } else if (resourceName != null && resourceName.startsWith("srb://")) { // get srb docid from the url String srbIdentifier = transformSRBurlToDocid(resourceName); // reset endpoint for srb (This is hack we need to figure ou // elegent way to do this //mEndPoint = Config.getValue("//ecogridService/srb/endPoint"); // pass this docid and get data item //log.debug("before get srb data"); return getContentFromEcoGridSource(SRBENDPOINT, srbIdentifier); } else { successFlag = false; return successFlag; } }
From source file:com.hackengine_er.muslumyusuf.DBOperations.java
private boolean uploadToFTP(String username, String fileName, InputStream inputStream) { FTPClient client = new FTPClient(); try {//from w w w.j a v a 2 s . c o m client.connect(Configuration.FTPClient()); if (client.login(Configuration.FTPUsername(), Configuration.getPASSWORD())) { client.setDefaultTimeout(10000); client.setFileType(FTPClient.BINARY_FILE_TYPE); if (client.storeFile(Tags.SITE + username + "-" + fileName, inputStream)) { return true; } } } catch (IOException e) { Logger.getLogger(DBOperations.class.getName()).log(Level.SEVERE, null, e); } finally { if (client.isConnected()) { try { client.logout(); client.disconnect(); } catch (IOException ex) { Logger.getLogger(DBOperations.class.getName()).log(Level.SEVERE, null, ex); } } } return false; }
From source file:com.microsoft.intellij.forms.CreateWebSiteForm.java
private void copyWebConfigForCustom(WebSiteConfiguration config) throws AzureCmdException { if (config != null) { AzureManager manager = AzureManagerImpl.getManager(project); WebSitePublishSettings webSitePublishSettings = manager.getWebSitePublishSettings( config.getSubscriptionId(), config.getWebSpaceName(), config.getWebSiteName()); // retrieve ftp publish profile WebSitePublishSettings.FTPPublishProfile ftpProfile = null; for (WebSitePublishSettings.PublishProfile pp : webSitePublishSettings.getPublishProfileList()) { if (pp instanceof WebSitePublishSettings.FTPPublishProfile) { ftpProfile = (WebSitePublishSettings.FTPPublishProfile) pp; break; }/*from w w w . j ava 2 s. c om*/ } if (ftpProfile != null) { FTPClient ftp = new FTPClient(); try { URI uri = null; uri = new URI(ftpProfile.getPublishUrl()); ftp.connect(uri.getHost()); final int replyCode = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) { ftp.disconnect(); } if (!ftp.login(ftpProfile.getUserName(), ftpProfile.getPassword())) { ftp.logout(); } ftp.setFileType(FTP.BINARY_FILE_TYPE); if (ftpProfile.isFtpPassiveMode()) { ftp.enterLocalPassiveMode(); } ftp.deleteFile(ftpPath + message("configName")); String tmpPath = String.format("%s%s%s", System.getProperty("java.io.tmpdir"), File.separator, message("configName")); File file = new File(tmpPath); if (file.exists()) { file.delete(); } WAEclipseHelperMethods.copyFile(WAHelper.getCustomJdkFile(message("configName")), tmpPath); String jdkFolderName = ""; if (customJDKUser.isSelected()) { String url = customUrl.getText(); jdkFolderName = url.substring(url.lastIndexOf("/") + 1, url.length()); jdkFolderName = jdkFolderName.substring(0, jdkFolderName.indexOf(".zip")); } else { String cloudVal = WindowsAzureProjectManager .getCloudValue((String) jdkNames.getSelectedItem(), AzurePlugin.cmpntFile); jdkFolderName = cloudVal.substring(cloudVal.indexOf("\\") + 1, cloudVal.length()); } String jdkPath = "%HOME%\\site\\wwwroot\\jdk\\" + jdkFolderName; String serverPath = "%programfiles(x86)%\\" + WAHelper .generateServerFolderName(config.getJavaContainer(), config.getJavaContainerVersion()); WebAppConfigOperations.prepareWebConfigForCustomJDKServer(tmpPath, jdkPath, serverPath); InputStream input = new FileInputStream(tmpPath); ftp.storeFile(ftpPath + message("configName"), input); cleanup(ftp); ftp.logout(); } catch (Exception e) { AzurePlugin.log(e.getMessage(), e); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ignored) { } } } } } }
From source file:com.muslumyusuf.DBOperations.java
private boolean uploadToFTP(String username, String fileName, InputStream inputStream) { FTPClient client = new FTPClient(); try {/*from ww w .j av a2 s.c om*/ client.connect(Initialize.FTPClient()); if (client.login(Initialize.FTPUsername(), Initialize.getPASSWORD())) { client.setDefaultTimeout(10000); client.setFileType(FTPClient.BINARY_FILE_TYPE); if (client.storeFile(Tags.SITE + username + "/" + fileName, inputStream)) { return true; } } } catch (IOException e) { Logger.getLogger(DBOperations.class.getName()).log(Level.SEVERE, null, e); } finally { if (client.isConnected()) { try { client.logout(); client.disconnect(); } catch (IOException ex) { Logger.getLogger(DBOperations.class.getName()).log(Level.SEVERE, null, ex); } } } return false; }
From source file:convcao.com.caoAgent.convcaoNeptusInteraction.java
private void Upload(String ftpServer, String pathDirectory, String SourcePathDirectory, String userName, String password, String filename) { FTPClient client = new FTPClient(); FileInputStream fis = null;/* w ww . j a v a 2 s.c o m*/ try { client.connect(ftpServer); client.login(userName, password); client.enterLocalPassiveMode(); client.setFileType(FTP.BINARY_FILE_TYPE); fis = new FileInputStream(SourcePathDirectory + filename); client.changeWorkingDirectory("/" + pathDirectory); client.storeFile(filename, fis); System.out.println( "The file " + SourcePathDirectory + " was stored to " + "/" + pathDirectory + "/" + filename); client.logout(); //Report = "File: " + filename + " Uploaded Successfully "; } catch (Exception exp) { exp.printStackTrace(); Report = "Server Error"; jLabel6.setText(Report); } finally { try { if (fis != null) { fis.close(); } client.disconnect(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:com.netpace.aims.controller.application.WapApplicationHelper.java
public static boolean wapFTPZipFile(File transferFile) throws AimsException { log.debug("wapFTPZipFile FTP Start. FileName: " + transferFile.getName()); boolean loginStatus = false; boolean dirChanged = false; FileInputStream transferStream = null; FTPClient ftpClient = new FTPClient(); ConfigEnvProperties envProperties = ConfigEnvProperties.getInstance(); //ftp server config String ftpServerAddress = envProperties.getProperty("wap.images.ftp.server.address"); String ftpUser = envProperties.getProperty("wap.images.ftp.user.name"); String ftpPassword = envProperties.getProperty("wap.images.ftp.user.password"); String ftpWorkingDirectory = envProperties.getProperty("wap.images.ftp.working.dir"); //general exception for ftp transfer AimsException aimsException = new AimsException("Error"); aimsException.addException(new AimsException("error.wap.app.ftp.transfer")); String errorMessage = ""; boolean transfered = false; try {/*from w ww .j a v a 2 s. c o m*/ ftpClient.connect(ftpServerAddress); loginStatus = ftpClient.login(ftpUser, ftpPassword); log.debug("Connection to server " + ftpServerAddress + " " + (loginStatus ? "success" : "failure")); if (loginStatus) { dirChanged = ftpClient.changeWorkingDirectory(ftpWorkingDirectory); log.debug("change remote directory to " + ftpWorkingDirectory + ": " + dirChanged); if (dirChanged) { transferStream = new FileInputStream(transferFile); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); transfered = ftpClient.storeFile(transferFile.getName(), transferStream); log.debug(transferFile.getName() + " transfered: " + transfered + " on server: " + ftpServerAddress); if (!transfered) { errorMessage = "File: " + transferFile.getName() + " not transfered to " + (ftpServerAddress + "/" + ftpServerAddress); System.out.println("Error in FTP Transfer: " + errorMessage); sendFTPErrorMail(errorMessage); throw aimsException; } } else { errorMessage = "Directory: " + ftpWorkingDirectory + " not found in " + ftpServerAddress; System.out.println("Error in FTP Transfer: " + errorMessage); sendFTPErrorMail(errorMessage); throw aimsException; } } //end loginstatus else { errorMessage = "FTP Authentication failed. \n\nServer: " + ftpServerAddress + "\nUserID: " + ftpUser + "\nPassword: " + ftpPassword; System.out.println("Error in FTP Transfer: " + errorMessage); sendFTPErrorMail(errorMessage); throw aimsException; } } //end try catch (FileNotFoundException e) { System.out.println("Exception: " + transferFile.getName() + " not found in temp directory"); e.printStackTrace();//zip file not found throw aimsException; } catch (IOException ioe) { System.out.println("Exception: Error in connection " + ftpServerAddress); ioe.printStackTrace(); sendFTPErrorMail("Error in connection to : " + ftpServerAddress + "\n\n" + MiscUtils.getExceptionStackTraceInfo(ioe)); throw aimsException; } finally { try { //close stream if (transferStream != null) { transferStream.close(); } } catch (IOException ioe) { ioe.printStackTrace(); } finally { if (ftpClient.isConnected()) { try { //logout. Issues QUIT command ftpClient.logout(); } catch (IOException ioex) { ioex.printStackTrace(); } finally { try { //disconnect ftp session ftpClient.disconnect(); } catch (IOException ioexc) { ioexc.printStackTrace(); } } } } } log.debug("wapFTPZipFile FTP end. FileName: " + transferFile.getName() + "\t transfered: " + transfered); return transfered; }
From source file:convcao.com.agent.ConvcaoNeptusInteraction.java
private void upload(String ftpServer, String pathDirectory, String sourcePathDirectory, String userName, String password, String filename) { FTPClient client = new FTPClient(); FileInputStream fis = null;//from w w w . j a v a 2 s . co m try { client.connect(ftpServer); client.login(userName, password); client.enterLocalPassiveMode(); client.setFileType(FTP.BINARY_FILE_TYPE); fis = new FileInputStream(sourcePathDirectory + filename); client.changeWorkingDirectory("/" + pathDirectory); client.storeFile(filename, fis); System.out.println( "The file " + sourcePathDirectory + " was stored to " + "/" + pathDirectory + "/" + filename); client.logout(); //Report = "File: " + filename + " Uploaded Successfully "; } catch (Exception exp) { exp.printStackTrace(); report = "Server Error"; jLabel6.setText(report); } finally { try { if (fis != null) { fis.close(); } client.disconnect(); } catch (IOException e) { e.printStackTrace(); } } }