List of usage examples for org.apache.commons.net.ftp FTPClient isConnected
public boolean isConnected()
From source file:com.microsoft.tooling.msservices.helpers.azure.AzureManagerImpl.java
@Override public void publishWebArchiveArtifact(@NotNull String subscriptionId, @NotNull String webSpaceName, @NotNull String webSiteName, @NotNull String artifactPath, @NotNull boolean isDeployRoot, @NotNull String artifactName) throws AzureCmdException { WebSitePublishSettings webSitePublishSettings = getWebSitePublishSettings(subscriptionId, webSpaceName, webSiteName);/*from w w w . ja v a 2 s. co m*/ WebSitePublishSettings.FTPPublishProfile publishProfile = null; for (PublishProfile pp : webSitePublishSettings.getPublishProfileList()) { if (pp instanceof FTPPublishProfile) { publishProfile = (FTPPublishProfile) pp; break; } } if (publishProfile == null) { throw new AzureCmdException("Unable to retrieve FTP credentials to publish web site"); } URI uri; try { uri = new URI(publishProfile.getPublishUrl()); } catch (URISyntaxException e) { throw new AzureCmdException("Unable to parse FTP Publish Url information", e); } final FTPClient ftp = new FTPClient(); try { ftp.connect(uri.getHost()); final int replyCode = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) { ftp.disconnect(); throw new AzureCmdException("Unable to connect to FTP server"); } if (!ftp.login(publishProfile.getUserName(), publishProfile.getPassword())) { ftp.logout(); throw new AzureCmdException("Unable to login to FTP server"); } ftp.setFileType(FTP.BINARY_FILE_TYPE); if (publishProfile.isFtpPassiveMode()) { ftp.enterLocalPassiveMode(); } String targetDir = getAbsolutePath(uri.getPath()); targetDir += "/webapps"; InputStream input = new FileInputStream(artifactPath); if (isDeployRoot) { ftp.storeFile(targetDir + "/ROOT.war", input); } else { ftp.storeFile(targetDir + "/" + artifactName + ".war", input); } input.close(); ftp.logout(); } catch (IOException e) { throw new AzureCmdException("Unable to connect to the FTP server", e); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ignored) { } } } }
From source file:com.jaeksoft.searchlib.scheduler.task.TaskFtpXmlFeed.java
@Override public void execute(Client client, TaskProperties properties, Variables variables, TaskLog taskLog) throws SearchLibException { String server = properties.getValue(propServer); String path = properties.getValue(propPath); String login = properties.getValue(propLogin); String password = properties.getValue(propPassword); String fileNamePattern = properties.getValue(propFileNamePattern); boolean deleteAfterLoad = Boolean.TRUE.toString().equals(properties.getValue(propDeleteAfterLoad)); boolean truncateWhenFilesFound = Boolean.TRUE.toString() .equals(properties.getValue(propTruncateIndexWhenFilesFound)); Pattern pattern = null;//from ww w .j av a2 s.com if (fileNamePattern != null && fileNamePattern.length() > 0) pattern = Pattern.compile(fileNamePattern); String p = properties.getValue(propBuffersize); String xsl = properties.getValue(propXsl); File xmlTempResult = null; int bufferSize = 50; if (p != null && p.length() > 0) bufferSize = Integer.parseInt(p); HttpDownloader httpDownloader = client.getWebCrawlMaster().getNewHttpDownloader(true); FTPClient ftp = null; InputStream inputStream = null; try { // FTP Connection ftp = new FTPClient(); checkConnect(ftp, server, login, password); FTPFile[] files = ftp.listFiles(path, new FtpFileInstance.FtpInstanceFileFilter(true, false, null)); if (files == null) return; // Sort by ascendant filename String[] fileNames = new String[files.length]; int i = 0; for (FTPFile file : files) fileNames[i++] = file.getName(); Arrays.sort(fileNames); int ignored = 0; int loaded = 0; boolean bAlreadyTruncated = false; for (String fileName : fileNames) { String filePathName = FilenameUtils.concat(path, fileName); if (pattern != null) if (!pattern.matcher(fileName).find()) { ignored++; continue; } if (truncateWhenFilesFound && !bAlreadyTruncated) { client.deleteAll(); bAlreadyTruncated = true; } taskLog.setInfo("Working on: " + filePathName); inputStream = ftp.retrieveFileStream(filePathName); Node xmlDoc = null; if (xsl != null && xsl.length() > 0) { xmlTempResult = File.createTempFile("ossftpfeed", ".xml"); DomUtils.xslt(new StreamSource(inputStream), xsl, xmlTempResult); xmlDoc = DomUtils.readXml(new StreamSource(xmlTempResult), false); } else xmlDoc = DomUtils.readXml(new StreamSource(inputStream), false); client.updateXmlDocuments(xmlDoc, bufferSize, null, httpDownloader, taskLog); client.deleteXmlDocuments(xmlDoc, bufferSize, taskLog); inputStream.close(); inputStream = null; if (!ftp.completePendingCommand()) throw new SearchLibException("FTP Error"); if (xmlTempResult != null) { xmlTempResult.delete(); xmlTempResult = null; } checkConnect(ftp, server, login, password); if (deleteAfterLoad) ftp.deleteFile(filePathName); loaded++; } taskLog.setInfo(loaded + " file(s) loaded - " + ignored + " file(s) ignored"); } catch (XPathExpressionException e) { throw new SearchLibException(e); } catch (NoSuchAlgorithmException e) { throw new SearchLibException(e); } catch (ParserConfigurationException e) { throw new SearchLibException(e); } catch (SAXException e) { throw new SearchLibException(e); } catch (IOException e) { throw new SearchLibException(e); } catch (URISyntaxException e) { throw new SearchLibException(e); } catch (InstantiationException e) { throw new SearchLibException(e); } catch (IllegalAccessException e) { throw new SearchLibException(e); } catch (ClassNotFoundException e) { throw new SearchLibException(e); } catch (TransformerException e) { throw new SearchLibException(e); } finally { if (xmlTempResult != null) xmlTempResult.delete(); IOUtils.close(inputStream); try { if (ftp != null) if (ftp.isConnected()) ftp.disconnect(); } catch (IOException e) { Logging.warn(e); } if (httpDownloader != null) httpDownloader.release(); } }
From source file:com.wheelermarine.android.publicAccesses.Updater.java
@Override protected Integer doInBackground(URL... urls) { try {//from www .ja v a2 s.c o m final DatabaseHelper db = new DatabaseHelper(context); SQLiteDatabase database = db.getWritableDatabase(); if (database == null) throw new IllegalStateException("Unable to open database!"); database.beginTransaction(); try { // Clear out the old data. database.delete(DatabaseHelper.PublicAccessEntry.TABLE_NAME, null, null); // Connect to the web server and locate the FTP download link. Log.v(TAG, "Finding update: " + urls[0]); activity.runOnUiThread(new Runnable() { @Override public void run() { progress.setMessage("Locating update..."); progress.setIndeterminate(true); } }); Document doc = Jsoup.connect(urls[0].toString()).timeout(timeout * 1000).userAgent(userAgent).get(); URL dataURL = null; for (Element element : doc.select("a")) { if (element.hasAttr("href") && element.attr("href").startsWith("ftp://ftp.dnr.state.mn.us")) { dataURL = new URL(element.attr("href")); } } // Make sure the download URL was fund. if (dataURL == null) throw new FileNotFoundException("Unable to locate data URL."); // Connect to the FTP server and download the update. Log.v(TAG, "Downloading update: " + dataURL); activity.runOnUiThread(new Runnable() { @Override public void run() { progress.setMessage("Downloading update..."); progress.setIndeterminate(true); } }); FTPClient ftp = new FTPClient(); try { ftp.setConnectTimeout(timeout * 1000); ftp.setDefaultTimeout(timeout * 1000); ftp.connect(dataURL.getHost()); ftp.enterLocalPassiveMode(); // After connection attempt, you should check the reply code // to verify success. if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { ftp.disconnect(); throw new IOException("FTP server refused connection: " + ftp.getReplyString()); } // Login using the standard anonymous credentials. if (!ftp.login("anonymous", "anonymous")) { ftp.disconnect(); throw new IOException("FTP Error: " + ftp.getReplyString()); } Map<Integer, Location> locations = null; // Download the ZIP archive. Log.v(TAG, "Downloading: " + dataURL.getFile()); ftp.setFileType(FTP.BINARY_FILE_TYPE); InputStream in = ftp.retrieveFileStream(dataURL.getFile()); if (in == null) throw new FileNotFoundException(dataURL.getFile() + " was not found!"); try { ZipInputStream zin = new ZipInputStream(in); try { // Locate the .dbf entry in the ZIP archive. ZipEntry entry; while ((entry = zin.getNextEntry()) != null) { if (entry.getName().endsWith(entryName)) { readDBaseFile(zin, database); } else if (entry.getName().endsWith(shapeEntryName)) { locations = readShapeFile(zin); } } } finally { try { zin.close(); } catch (Exception e) { // Ignore this error. } } } finally { in.close(); } if (locations != null) { final int recordCount = locations.size(); activity.runOnUiThread(new Runnable() { @Override public void run() { progress.setIndeterminate(false); progress.setMessage("Updating locations..."); progress.setMax(recordCount); } }); int progress = 0; for (int recordNumber : locations.keySet()) { PublicAccess access = db.getPublicAccessByRecordNumber(recordNumber); Location loc = locations.get(recordNumber); access.setLatitude(loc.getLatitude()); access.setLongitude(loc.getLongitude()); db.updatePublicAccess(access); publishProgress(++progress); } } } finally { if (ftp.isConnected()) ftp.disconnect(); } database.setTransactionSuccessful(); return db.getPublicAccessesCount(); } finally { database.endTransaction(); } } catch (Exception e) { error = e; Log.e(TAG, "Error loading data: " + e.getLocalizedMessage(), e); return -1; } }
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 w w.jav a 2 s .co 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:com.claim.controller.FileTransferController.java
public boolean uploadMutiFilesWithFTP(ObjFileTransfer ftpObj) throws Exception { FTPClient ftpClient = new FTPClient(); int replyCode; boolean completed = false; try {//w w w . j a v a2s .c om FtpProperties properties = new ResourcesProperties().loadFTPProperties(); try { ftpClient.connect(properties.getFtp_server(), properties.getFtp_port()); FtpUtil.showServerReply(ftpClient); } catch (ConnectException ex) { FtpUtil.showServerReply(ftpClient); Console.LOG(ex.getMessage(), 0); System.out.println("ConnectException: " + ex.getMessage()); ex.printStackTrace(); } catch (SocketException ex) { FtpUtil.showServerReply(ftpClient); Console.LOG(ex.getMessage(), 0); System.out.println("SocketException: " + ex.getMessage()); ex.printStackTrace(); } catch (UnknownHostException ex) { FtpUtil.showServerReply(ftpClient); Console.LOG(ex.getMessage(), 0); System.out.println("UnknownHostException: " + ex.getMessage()); ex.printStackTrace(); } replyCode = ftpClient.getReplyCode(); FtpUtil.showServerReply(ftpClient); if (!FTPReply.isPositiveCompletion(replyCode)) { FtpUtil.showServerReply(ftpClient); ftpClient.disconnect(); Console.LOG("Exception in connecting to FTP Serve ", 0); throw new Exception("Exception in connecting to FTP Server"); } else { FtpUtil.showServerReply(ftpClient); Console.LOG("Success in connecting to FTP Serve ", 1); } try { boolean success = ftpClient.login(properties.getFtp_username(), properties.getFtp_password()); FtpUtil.showServerReply(ftpClient); if (!success) { throw new Exception("Could not login to the FTP server."); } else { Console.LOG("login to the FTP server. Successfully ", 1); } //ftpClient.enterLocalPassiveMode(); } catch (FTPConnectionClosedException ex) { FtpUtil.showServerReply(ftpClient); Console.LOG(ex.getMessage(), 0); System.out.println("Error: " + ex.getMessage()); ex.printStackTrace(); } ftpClient.setFileType(FTP.BINARY_FILE_TYPE); //FTP_REMOTE_HOME = ftpClient.printWorkingDirectory(); // APPROACH #2: uploads second file using an OutputStream File files = new File(ftpObj.getFtp_directory_path()); String workingDirectoryReportType = properties.getFtp_remote_directory() + File.separator + ftpObj.getFtp_report_type(); FtpUtil.ftpCreateDirectoryTree(ftpClient, workingDirectoryReportType); FtpUtil.showServerReply(ftpClient); String workingDirectoryStmp = workingDirectoryReportType + File.separator + ftpObj.getFtp_stmp(); FtpUtil.ftpCreateDirectoryTree(ftpClient, workingDirectoryStmp); FtpUtil.showServerReply(ftpClient); for (File file : files.listFiles()) { if (file.isFile()) { System.out.println("file ::" + file.getName()); InputStream in = new FileInputStream(file); ftpClient.changeWorkingDirectory(workingDirectoryStmp); completed = ftpClient.storeFile(file.getName(), in); in.close(); Console.LOG( " " + file.getName() + " ", 1); FtpUtil.showServerReply(ftpClient); } } Console.LOG(" ?... ", 1); //completed = ftpClient.completePendingCommand(); FtpUtil.showServerReply(ftpClient); completed = true; ftpClient.disconnect(); } catch (IOException ex) { Console.LOG(ex.getMessage(), 0); FtpUtil.showServerReply(ftpClient); System.out.println("Error: " + ex.getMessage()); ex.printStackTrace(); } finally { try { if (ftpClient.isConnected()) { ftpClient.logout(); ftpClient.disconnect(); } } catch (IOException ex) { FtpUtil.showServerReply(ftpClient); Console.LOG(ex.getMessage(), 0); ex.printStackTrace(); } } return completed; }
From source file:com.ibm.cics.ca1y.Emit.java
/** * Get the contents of a file from an FTP server. * //from w ww. j a v a 2s .c o m * @param filename * - * @param server * - * @param useraname * - * @param userpassword * - * @param transfer * - * @param mode * - * @param epsv * - * @param protocol * - * @param trustmgr * - * @param datatimeout * - * @param proxyserver * - * @param proxyusername * - * @param proxypassword * - * @param anonymouspassword * - * @return byte[] representing the retrieved file, null otherwise. */ private static byte[] getFileUsingFTP(String filename, String server, String username, String userpassword, String transfer, String mode, String epsv, String protocol, String trustmgr, String datatimeout, String proxyserver, String proxyusername, String proxypassword, String anonymouspassword) { FTPClient ftp; if (filename == null || server == null) return null; int port = 0; if (server != null) { String parts[] = server.split(":"); if (parts.length == 2) { server = parts[0]; try { port = Integer.parseInt(parts[1]); } catch (Exception _ex) { } } } int proxyport = 0; if (proxyserver != null) { String parts[] = proxyserver.split(":"); if (parts.length == 2) { proxyserver = parts[0]; try { proxyport = Integer.parseInt(parts[1]); } catch (Exception _ex) { } } } if (username == null) { username = "anonymous"; if (userpassword == null) userpassword = anonymouspassword; } if (protocol == null) { if (proxyserver != null) ftp = new FTPHTTPClient(proxyserver, proxyport, proxyusername, proxypassword); else ftp = new FTPClient(); } else { FTPSClient ftps = null; if ("true".equalsIgnoreCase(protocol)) { ftps = new FTPSClient(true); } else if ("false".equalsIgnoreCase(protocol)) { ftps = new FTPSClient(false); } else if (protocol != null) { String parts[] = protocol.split(","); if (parts.length == 1) ftps = new FTPSClient(protocol); else ftps = new FTPSClient(parts[0], Boolean.parseBoolean(parts[1])); } ftp = ftps; if ("all".equalsIgnoreCase(trustmgr)) { ftps.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager()); } else if ("valid".equalsIgnoreCase(trustmgr)) { ftps.setTrustManager(TrustManagerUtils.getValidateServerCertificateTrustManager()); } else if ("none".equalsIgnoreCase(trustmgr)) { ftps.setTrustManager(null); } } if (datatimeout != null) { try { ftp.setDataTimeout(Integer.parseInt(datatimeout)); } catch (Exception _ex) { ftp.setDataTimeout(-1); } } if (port <= 0) { port = ftp.getDefaultPort(); } if (logger.isLoggable(Level.FINE)) { StringBuilder sb = new StringBuilder(); sb.append(Emit.messages.getString("FTPAboutToGetFileFromServer")).append(" - ").append("file name:") .append(filename).append(",server:").append(server).append(":").append(port) .append(",username:").append(username).append(",userpassword:") .append(userpassword != null && !"anonymous".equals(username) ? "<obscured>" : userpassword) .append(",transfer:").append(transfer).append(",mode:").append(mode).append(",epsv:") .append(epsv).append(",protocol:").append(protocol).append(",trustmgr:").append(trustmgr) .append(",datatimeout:").append(datatimeout).append(",proxyserver:").append(proxyserver) .append(":").append(proxyport).append(",proxyusername:").append(proxyusername) .append(",proxypassword:").append(proxypassword != null ? "<obscured>" : null); logger.fine(sb.toString()); } try { if (port > 0) { ftp.connect(server, port); } else { ftp.connect(server); } int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); logger.warning(Emit.messages.getString("FTPCouldNotConnectToServer")); return null; } } catch (IOException _ex) { logger.warning(Emit.messages.getString("FTPCouldNotConnectToServer")); if (ftp.isConnected()) try { ftp.disconnect(); } catch (IOException _ex2) { } return null; } ByteArrayOutputStream output; try { if (!ftp.login(username, userpassword)) { ftp.logout(); logger.warning(Emit.messages.getString("FTPServerRefusedLoginCredentials")); return null; } if ("binary".equalsIgnoreCase(transfer)) { ftp.setFileType(2); } else { ftp.setFileType(0); } if ("localactive".equalsIgnoreCase(mode)) { ftp.enterLocalActiveMode(); } else { ftp.enterLocalPassiveMode(); } ftp.setUseEPSVwithIPv4("true".equalsIgnoreCase(epsv)); output = new ByteArrayOutputStream(); ftp.retrieveFile(filename, output); output.close(); ftp.noop(); ftp.logout(); } catch (IOException _ex) { logger.warning(Emit.messages.getString("FTPFailedToTransferFile")); if (ftp.isConnected()) try { ftp.disconnect(); } catch (IOException _ex2) { } return null; } return output.toByteArray(); }
From source file:com.droid.app.fotobot.FotoBot.java
public boolean files_to_ftp(List<String> FTP_files) { String server;// w w w . j av a 2 s .c o m int port; String user; String pass; String FTP_folder = ""; if (FTP_server.contains("/")) { FTP_folder = FTP_server.substring(FTP_server.indexOf("/", 1)); FTP_folder = FTP_folder.substring(1); server = FTP_server.substring(0, FTP_server.indexOf("/", 1)); } else { server = FTP_server; } port = Integer.parseInt(FTP_port); user = FTP_username; pass = FTP_password; SendMessage("FTP user: " + "<br>" + user, MSG_PASS); SendMessage("FTP folder: " + "<br>" + FTP_folder, MSG_PASS); SendMessage("FTP server: " + "<br>" + server, MSG_PASS); FTPClient ftpClient = new FTPClient(); try { int reply; ftpClient.connect(server, port); System.out.println("Connected to " + server + "."); System.out.print(ftpClient.getReplyString()); reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftpClient.disconnect(); System.err.println("FTP server refused connection."); SendMessage("FTP ?", MSG_FAIL); return false; } } catch (Exception e) { } try { ftpClient.login(user, pass); ftpClient.enterLocalPassiveMode(); if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } SendMessage( " ? FTP ?, FTP ?.", MSG_FAIL); return false; } } catch (Exception e) { } // chdir if (FTP_folder.length() > 1) { try { ftpClient.changeWorkingDirectory(FTP_folder); if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { ftpClient.disconnect(); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } System.err.println("FTP chdir error"); SendMessage( "FTP ? <br>" + FTP_folder, MSG_FAIL); return false; } SendMessage("FTP <br>" + FTP_folder, MSG_PASS); System.out.println("Successfully changed working directory."); } catch (Exception e) { try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e1) { e1.printStackTrace(); } SendMessage("FTP ? <br>" + FTP_folder, MSG_FAIL); System.out.println("Failed to change working directory."); return false; } } try { ftpClient.setFileType(FTP.BINARY_FILE_TYPE); } catch (Exception e) { SendMessage("FTP BINARY_FILE_TYPE", MSG_FAIL); System.out.println("Failed to change to BINARY_FILE_TYPE."); return false; } // APPROACH #1: uploads first file using an InputStream for (String str : FTP_files) { File firstLocalFile = new File(str); String firstRemoteFile = firstLocalFile.getName(); try { InputStream inputStream = new FileInputStream(firstLocalFile); SendMessage("? ", MSG_PASS); boolean done = ftpClient.storeFile(firstRemoteFile, inputStream); inputStream.close(); if (done) { SendMessage(" " + "<br>" + str + "<br>" + " ", MSG_PASS); } } catch (IOException ex) { try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } SendMessage(" ? " + "<br>" + str + "<br>" + ftpClient.getReplyCode() + "\n" + ftpClient.getReplyString() + "\n" + ex.getMessage() + "<br>" + " FTP ?.", MSG_FAIL); ex.printStackTrace(); } } try { if (ftpClient.isConnected()) { ftpClient.logout(); ftpClient.disconnect(); SendMessage("FTP ???? ", MSG_PASS); fbpause(h, 3); return true; } } catch (IOException ex) { ex.printStackTrace(); SendMessage("FTP ???? ", MSG_FAIL); } return false; }
From source file:com.seajas.search.contender.service.modifier.ArchiveModifierService.java
/** * Store the not-already-cached files from the given URL and return their locations. * /*from www . j a v a 2 s. c o m*/ * @param archive * @return Map<File, String> */ private Map<File, String> storeAndDecompressFiles(final Archive archive) { Map<File, String> result = new HashMap<File, String>(); // Create the FTP client FTPClient ftpClient = retrieveFtpClient(archive.getUri()); try { // Retrieve the directory listing List<ArchiveFile> files = retrieveFiles(archive.getUri().getPath(), ftpClient, archive.getExclusionExpression()); Integer archiveNumber = -1, archiveTotal = files.size(); logger.info("Archive with name '" + archive.getName() + "' produced " + archiveTotal + " files"); // An empty archive typically indicates failure if (archiveTotal == 0) logger.warn("The given archive produced no entries - something probably went wrong"); // Handle all archive files for (ArchiveFile archiveFile : files) { archiveNumber++; // Check whether the file already exists within the cache String baseUrl = (StringUtils.hasText(archive.getUri().getScheme()) ? archive.getUri().getScheme() + "://" : "") + archive.getUri().getHost() + (archive.getUri().getPort() != -1 ? ":" + archive.getUri().getPort() : ""); if (!cacheService.isArchived(baseUrl + archiveFile.getFullPath())) { logger.info("Started decompressing archive " + archiveNumber + "/" + archiveTotal + " with name " + archiveFile.getFullPath()); // Write out the archive to disk so we can determine the MIME type File archiveFileFolder = new File(archiveFile .getTranslatedPath(packagesLocation.replace("%1", String.valueOf(archive.getId())))); if (!archiveFileFolder.exists()) archiveFileFolder.mkdirs(); File archiveFileLocation = new File(archiveFileFolder, archiveFile.getFile().getName()); InputStream archiveInputStream = ftpClient.retrieveFileStream(archiveFile.getFullPath()); OutputStream archiveOutputStream = new FileOutputStream(archiveFileLocation); IOUtils.copy(archiveInputStream, archiveOutputStream); archiveInputStream.close(); ftpClient.completePendingCommand(); archiveOutputStream.flush(); archiveOutputStream.close(); // Now unpack the archive and transform each file InputStream compressedArchiveInputStream = new FileInputStream(archiveFileLocation); // Now determine the content type and create a reader in case of structured content MediaType archiveMediaType = autoDetectParser.getDetector() .detect(new BufferedInputStream(compressedArchiveInputStream), new Metadata()); if (!(archiveMediaType.getType().equals("application") && archiveMediaType.getSubtype().equals("zip"))) { logger.warn("Archive file " + archiveFile.getFullPath() + " contains " + archiveMediaType + " data, which is not yet supported"); compressedArchiveInputStream.close(); continue; } else compressedArchiveInputStream.close(); // Create a new ZIP file from the given archive and decompress it ZipFile zipFile = new ZipFile(archiveFileLocation); File resultsLocationFolder = new File(archiveFile .getTranslatedPath(resultsLocation.replace("%1", String.valueOf(archive.getId())))); if (!resultsLocationFolder.exists()) resultsLocationFolder.mkdirs(); File resultsLocation = new File(resultsLocationFolder, stripExtension(archiveFile.getFile().getName())); if (!resultsLocation.exists()) resultsLocation.mkdirs(); logger.info("Started processing archive with name " + archiveFile.getFullPath()); Enumeration<? extends ZipEntry> zipEnumerator = zipFile.entries(); while (zipEnumerator.hasMoreElements()) { ZipEntry entry = zipEnumerator.nextElement(); // Store it locally first File entryLocation = new File(resultsLocation, entry.getName()); try { InputStream entryInputStream = zipFile.getInputStream(entry); OutputStream entryOutputStream = new FileOutputStream(entryLocation); IOUtils.copy(entryInputStream, entryOutputStream); entryInputStream.close(); entryOutputStream.close(); } catch (IOException e) { logger.error("Could not store the compressed archive entry on disk", e); continue; } } zipFile.close(); // Add it to the results result.put(resultsLocation, baseUrl + archiveFile.getFullPath()); logger.info("Finished processing archive with name " + archiveFile.getFullPath()); } else if (logger.isDebugEnabled()) logger.debug("Skipping previously processed archive with name " + archiveFile.getFullPath()); } } catch (IOException e) { logger.error("Could not close input stream during archive processing", e); } finally { try { if (ftpClient.isConnected()) ftpClient.disconnect(); } catch (IOException e) { logger.error("Could not disconnect the FTP client", e); } } return result; }
From source file:com.github.goldin.org.apache.tools.ant.taskdefs.optional.net.FTP.java
/** * Runs the task.//from w w w .ja v a 2 s. com * * @throws BuildException if the task fails or is not configured * correctly. */ public void execute() throws BuildException { checkAttributes(); FTPClient ftp = null; try { log("Opening FTP connection to " + server, Project.MSG_VERBOSE); /** * "verbose" version of <code>FTPClient</code> prints progress indicator * See http://evgeny-goldin.com/blog/2010/08/18/ant-ftp-task-progress-indicator-timeout/ */ ftp = (verbose ? new com.github.goldin.org.apache.tools.ant.taskdefs.optional.net.FTPClient(getProject()) : new org.apache.commons.net.ftp.FTPClient()); ftp.setDataTimeout(5 * 60 * 1000); // 5 minutes if (this.isConfigurationSet) { ftp = FTPConfigurator.configure(ftp, this); } ftp.setRemoteVerificationEnabled(enableRemoteVerification); ftp.connect(server, port); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("FTP connection failed: " + ftp.getReplyString()); } log("connected", Project.MSG_VERBOSE); log("logging in to FTP server", Project.MSG_VERBOSE); if ((this.account != null && !ftp.login(userid, password, account)) || (this.account == null && !ftp.login(userid, password))) { throw new BuildException("Could not login to FTP server"); } log("login succeeded", Project.MSG_VERBOSE); if (binary) { ftp.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("could not set transfer type: " + 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: " + ftp.getReplyString()); } } if (passive) { log("entering passive mode", Project.MSG_VERBOSE); ftp.enterLocalPassiveMode(); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("could not enter into passive " + "mode: " + ftp.getReplyString()); } } // If an initial command was configured then send it. // Some FTP servers offer different modes of operation, // E.G. switching between a UNIX file system mode and // a legacy file system. if (this.initialSiteCommand != null) { RetryHandler h = new RetryHandler(this.retriesAllowed, this); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { doSiteCommand(lftp, FTP.this.initialSiteCommand); } }, "initial site command: " + this.initialSiteCommand); } // For a unix ftp server you can set the default mask for all files // created. if (umask != null) { RetryHandler h = new RetryHandler(this.retriesAllowed, this); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { doSiteCommand(lftp, "umask " + umask); } }, "umask " + umask); } // If the action is MK_DIR, then the specified remote // directory is the directory to create. if (action == MK_DIR) { RetryHandler h = new RetryHandler(this.retriesAllowed, this); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { makeRemoteDir(lftp, remotedir); } }, remotedir); } else if (action == SITE_CMD) { RetryHandler h = new RetryHandler(this.retriesAllowed, this); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { doSiteCommand(lftp, FTP.this.siteCommand); } }, "Site Command: " + this.siteCommand); } else { if (remotedir != null) { log("changing the remote directory to " + remotedir, Project.MSG_VERBOSE); ftp.changeWorkingDirectory(remotedir); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("could not change remote " + "directory: " + ftp.getReplyString()); } } if (newerOnly && timeDiffAuto) { // in this case we want to find how much time span there is between local // and remote timeDiffMillis = getTimeDiff(ftp); } log(ACTION_STRS[action] + " " + ACTION_TARGET_STRS[action]); transferFiles(ftp); } } catch (IOException ex) { throw new BuildException("error during FTP transfer: " + ex, ex); } finally { if (ftp != null && ftp.isConnected()) { try { log("disconnecting", Project.MSG_VERBOSE); ftp.logout(); ftp.disconnect(); } catch (IOException ex) { // ignore it } } } }
From source file:com.github.carlosrubio.org.apache.tools.ant.taskdefs.optional.net.FTP.java
/** * Runs the task.// w w w.ja v a 2 s . co m * * @throws BuildException if the task fails or is not configured * correctly. */ public void execute() throws BuildException { checkAttributes(); FTPClient ftp = null; try { log("Opening FTP connection to " + server, Project.MSG_VERBOSE); /** * "verbose" version of <code>FTPClient</code> prints progress indicator * See http://evgeny-goldin.com/blog/2010/08/18/ant-ftp-task-progress-indicator-timeout/ */ ftp = (verbose ? new com.github.carlosrubio.org.apache.tools.ant.taskdefs.optional.net.FTPClient(getProject()) : new org.apache.commons.net.ftp.FTPClient()); ftp.setDataTimeout(5 * 60 * 1000); // 5 minutes if (this.isConfigurationSet) { ftp = FTPConfigurator.configure(ftp, this); } ftp.setRemoteVerificationEnabled(enableRemoteVerification); ftp.connect(server, port); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("FTP connection failed: " + ftp.getReplyString()); } log("connected", Project.MSG_VERBOSE); log("logging in to FTP server", Project.MSG_VERBOSE); if ((this.account != null && !ftp.login(userid, password, account)) || (this.account == null && !ftp.login(userid, password))) { throw new BuildException("Could not login to FTP server"); } log("login succeeded", Project.MSG_VERBOSE); if (binary) { ftp.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("could not set transfer type: " + 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: " + ftp.getReplyString()); } } if (passive) { log("entering passive mode", Project.MSG_VERBOSE); ftp.enterLocalPassiveMode(); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("could not enter into passive " + "mode: " + ftp.getReplyString()); } } // If an initial command was configured then send it. // Some FTP servers offer different modes of operation, // E.G. switching between a UNIX file system mode and // a legacy file system. if (this.initialSiteCommand != null) { RetryHandler h = new RetryHandler(this.retriesAllowed, this); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { doSiteCommand(lftp, FTP.this.initialSiteCommand); } }, "initial site command: " + this.initialSiteCommand); } // For a unix ftp server you can set the default mask for all files // created. if (umask != null) { RetryHandler h = new RetryHandler(this.retriesAllowed, this); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { doSiteCommand(lftp, "umask " + umask); } }, "umask " + umask); } // If the action is MK_DIR, then the specified remote // directory is the directory to create. if (action == MK_DIR) { RetryHandler h = new RetryHandler(this.retriesAllowed, this); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { makeRemoteDir(lftp, remotedir); } }, remotedir); } else if (action == SITE_CMD) { RetryHandler h = new RetryHandler(this.retriesAllowed, this); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { doSiteCommand(lftp, FTP.this.siteCommand); } }, "Site Command: " + this.siteCommand); } else { if (remotedir != null) { log("changing the remote directory to " + remotedir, Project.MSG_VERBOSE); ftp.changeWorkingDirectory(remotedir); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("could not change remote " + "directory: " + ftp.getReplyString()); } } if (newerOnly && timeDiffAuto) { // in this case we want to find how much time span there is between local // and remote timeDiffMillis = getTimeDiff(ftp); } log(ACTION_STRS[action] + " " + ACTION_TARGET_STRS[action]); transferFiles(ftp); } } catch (IOException ex) { throw new BuildException("error during FTP transfer: " + ex, ex); } finally { if (ftp != null && ftp.isConnected()) { try { log("disconnecting", Project.MSG_VERBOSE); ftp.logout(); ftp.disconnect(); } catch (IOException ex) { // ignore it } } } }