List of usage examples for org.apache.commons.net.ftp FTPClient retrieveFile
public boolean retrieveFile(String remote, OutputStream local) throws IOException
From source file:com.microsoft.azuretools.utils.WebAppUtils.java
public static void deployCustomJdk(WebApp webApp, String jdkDownloadUrl, WebContainer webContainer, IProgressIndicator indicator) throws IOException, InterruptedException, WebAppException { FTPClient ftp = null; String customJdkFolderName = null; try {//w w w.j a va 2s .c o m PublishingProfile pp = webApp.getPublishingProfile(); ftp = getFtpConnection(pp); // stop and restart web app // if (indicator != null) indicator.setText("Stopping the service..."); // webApp.stop(); if (indicator != null) indicator.setText("Deleting custom jdk artifacts, if any (takes a while)..."); removeCustomJdkArtifacts(ftp, indicator); if (indicator != null) indicator.setText("Uploading scripts..."); uploadJdkDownloadScript(ftp, jdkDownloadUrl); // if (indicator != null) indicator.setText("Starting the service..."); // webApp.start(); final String siteUrl = "https://" + webApp.defaultHostName(); // send get to activate the script sendGet(siteUrl); // Polling report.txt... if (indicator != null) indicator.setText("Checking the JDK gets downloaded and unpacked..."); //int step = 0; while (!doesRemoteFileExist(ftp, ftpRootPath, reportFilename)) { if (indicator != null && indicator.isCanceled()) throw new CancellationException("Canceled by user."); //if (step++ > 3) checkFreeSpaceAvailability(ftp); Thread.sleep(5000); sendGet(siteUrl); } if (indicator != null) indicator.setText("Checking status..."); OutputStream reportFileStream = new ByteArrayOutputStream(); ftp.retrieveFile("report.txt", reportFileStream); String reportFileString = reportFileStream.toString(); if (reportFileString.startsWith("FAIL")) { String err = reportFileString.substring(reportFileString.indexOf(":" + 1)); throw new WebAppException(err); } // get top level jdk folder name (under jdk folder) FTPFile[] ftpDirs = ftp.listDirectories(ftpJdkPath); if (ftpDirs.length != 1) { String err = "Bad JDK archive. Please make sure the JDK archive contains a single JDK folder. For example, 'my-jdk1.7.0_79.zip' archive should contain 'jdk1.7.0_79' folder only"; throw new WebAppException(err); } customJdkFolderName = ftpDirs[0].getName(); uploadWebConfigForCustomJdk(ftp, webApp, customJdkFolderName, webContainer, indicator); } catch (IOException | WebAppException | InterruptedException ex) { if (doesRemoteFolderExist(ftp, ftpRootPath, jdkFolderName)) { indicator.setText("Error happened. Cleaning up..."); removeFtpDirectory(ftp, ftpJdkPath, indicator); } throw ex; } finally { indicator.setText("Removing working data from server..."); cleanupWorkerData(ftp); if (ftp != null && ftp.isConnected()) { ftp.disconnect(); } } }
From source file:com.cisco.dvbu.ps.utils.net.FtpFile.java
public void ftpFile(String fileName) throws CustomProcedureException, SQLException { // new ftp client FTPClient ftp = new FTPClient(); OutputStream output = null;/*from w w w .j ava2 s.c om*/ success = false; try { //try to connect ftp.connect(hostIp); //login to server if (!ftp.login(userId, userPass)) { ftp.logout(); qenv.log(LOG_ERROR, "Ftp server refused connection user/password incorrect."); } int reply = ftp.getReplyCode(); //FTPReply stores a set of constants for FTP Reply codes if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); qenv.log(LOG_ERROR, "Ftp server refused connection."); } //enter passive mode ftp.setFileType(FTPClient.BINARY_FILE_TYPE, FTPClient.BINARY_FILE_TYPE); ftp.setFileTransferMode(FTPClient.BINARY_FILE_TYPE); ftp.enterLocalPassiveMode(); //get system name //System.out.println("Remote system is " + ftp.getSystemType()); //change current directory ftp.changeWorkingDirectory(ftpDirName); System.out.println("Current directory is " + ftp.printWorkingDirectory()); System.out.println("File is " + fileName); output = new FileOutputStream(dirName + "/" + fileName); //get the file from the remote system success = ftp.retrieveFile(fileName, output); //close output stream output.close(); } catch (IOException ex) { throw new CustomProcedureException("Error in CJP " + getName() + ": " + ex.toString()); } }
From source file:com.claim.controller.FileTransferController.java
public void readFilesFromServer(String targetDirectory) { FTPClient ftpClient = new FTPClient(); try {/*from www . j a v a 2 s . c o m*/ FtpProperties properties = new ResourcesProperties().loadFTPProperties(); ftpClient.connect(properties.getFtp_server(), properties.getFtp_port()); ftpClient.login(properties.getFtp_username(), properties.getFtp_password()); ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); //FTP_REMOTE_HOME = ftpClient.printWorkingDirectory(); FTPFile[] ftpFiles = ftpClient.listFiles(); if (ftpFiles != null && ftpFiles.length > 0) { //loop thru files for (FTPFile file : ftpFiles) { if (!file.isFile()) { continue; } System.out.println("File is " + file.getName()); //get output stream OutputStream output; //output = new FileOutputStream(FTP_REMOTE_HOME + "/" + file.getName()); output = new FileOutputStream(file.getName()); //get the file from the remote system ftpClient.retrieveFile(file.getName(), output); //close output stream output.close(); //delete the file //ftpClient.deleteFile(file.getName()); } } } catch (Exception e) { e.printStackTrace(); } finally { try { if (ftpClient != null) { ftpClient.logout(); ftpClient.disconnect(); } } catch (IOException ex) { Logger.getLogger(FileTransferController.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:fr.acxio.tools.agia.ftp.FTPDownloadTasklet.java
@Override public RepeatStatus execute(StepContribution sContribution, ChunkContext sChunkContext) throws Exception { FTPClient aClient = ftpClientFactory.getFtpClient(); RegexFilenameFilter aFilter = new RegexFilenameFilter(); aFilter.setRegex(regexFilename);//from w w w . j a v a 2s . co m try { URI aRemoteBaseURI = new URI(remoteBaseDir); URI aRemoteBasePath = new URI(aRemoteBaseURI.toASCIIString() + SEPARATOR); if (LOGGER.isInfoEnabled()) { LOGGER.info("Listing : [{}] {} ({})", aClient.getRemoteAddress().toString(), aRemoteBaseURI.toASCIIString(), regexFilename); } FTPFile[] aRemoteFiles = aClient.listFiles(aRemoteBaseURI.toASCIIString(), aFilter); if (LOGGER.isInfoEnabled()) { LOGGER.info(" {} file(s) found", aRemoteFiles.length); } for (FTPFile aRemoteFile : aRemoteFiles) { if (sContribution != null) { sContribution.incrementReadCount(); } File aLocalFile = new File(localBaseDir, aRemoteFile.getName()); URI aRemoteTFile = aRemoteBasePath.resolve(aRemoteFile.getName()); FileOutputStream aOutputStream = new FileOutputStream(aLocalFile); try { if (LOGGER.isInfoEnabled()) { LOGGER.info(" Downloading : {} => {}", aRemoteTFile.toASCIIString(), aLocalFile.getAbsolutePath()); } aClient.retrieveFile(aRemoteTFile.toASCIIString(), aOutputStream); if (removeRemote) { if (LOGGER.isInfoEnabled()) { LOGGER.info(" Deleting : {}", aRemoteTFile.toASCIIString()); } aClient.deleteFile(aRemoteTFile.toASCIIString()); } if (sContribution != null) { sContribution.incrementWriteCount(1); } } finally { aOutputStream.close(); } } } finally { aClient.logout(); aClient.disconnect(); } return RepeatStatus.FINISHED; }
From source file:edu.cmu.cs.in.hoop.hoops.load.HoopFTPReader.java
/** * /* w ww . j a v a 2 s . co m*/ */ private String retrieveFTP(String aURL) { debug("retrieveFTP (" + aURL + ")"); URL urlObject = null; try { urlObject = new URL(aURL); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } String downloadPath = this.projectToFullPath("<PROJECTPATH>/tmp/download/"); HoopLink.fManager.createDirectory(downloadPath); File translator = new File(urlObject.getFile()); String localFileName = "<PROJECTPATH>/tmp/download/" + translator.getName(); OutputStream fileStream = null; if (HoopLink.fManager.openStreamBinary(this.projectToFullPath(localFileName)) == false) { this.setErrorString("Error opening temporary output file"); return (null); } fileStream = HoopLink.fManager.getOutputStreamBinary(); if (fileStream == null) { this.setErrorString("Error opening temporary output file"); return (null); } debug("Starting FTP client ..."); FTPClient ftp = new FTPClient(); try { int reply; debug("Connecting ..."); ftp.connect(urlObject.getHost()); debug("Connected to " + urlObject.getHost() + "."); debug(ftp.getReplyString()); // After connection attempt, you should check the reply code to verify // success. reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); debug("FTP server refused connection."); return (null); } else { ftp.login("anonymous", "hoop-dev@gmail.com"); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); debug("Unable to login to FTP server"); return (null); } debug("Logged in"); boolean rep = true; String pathFixed = translator.getParent().replace("\\", "/"); rep = ftp.changeWorkingDirectory(pathFixed); if (rep == false) { debug("Unable to change working directory to: " + pathFixed); return (null); } else { debug("Current working directory: " + pathFixed); debug("Retrieving file " + urlObject.getFile() + " ..."); try { rep = ftp.retrieveFile(urlObject.getFile(), fileStream); } catch (FTPConnectionClosedException connEx) { debug("Caught: FTPConnectionClosedException"); connEx.printStackTrace(); return (null); } catch (CopyStreamException strEx) { debug("Caught: CopyStreamException"); strEx.printStackTrace(); return (null); } catch (IOException ioEx) { debug("Caught: IOException"); ioEx.printStackTrace(); return (null); } debug("File retrieved"); } ftp.logout(); } } catch (IOException e) { debug("Error retrieving FTP file"); e.printStackTrace(); return (null); } finally { if (ftp.isConnected()) { debug("Closing ftp connection ..."); try { ftp.disconnect(); } catch (IOException ioe) { debug("Exception closing ftp connection"); } } } debug("Closing local file stream ..."); try { fileStream.close(); } catch (IOException e) { e.printStackTrace(); } String result = HoopLink.fManager.loadContents(this.projectToFullPath(localFileName)); return (result); }
From source file:it.baywaylabs.jumpersumo.twitter.TwitterListener.java
/** * @param host FTP Host name./*from w ww . j av a2s . c o m*/ * @param port FTP port. * @param user FTP User. * @param pswd FTP Password. * @param c Context * @return Downloaded name file or blank list if something was going wrong. */ private String FTPDownloadFile(String host, Integer port, String user, String pswd, Context c) { String result = ""; FTPClient mFTPClient = null; try { mFTPClient = new FTPClient(); // connecting to the host mFTPClient.connect(host, port); // Now check the reply code, if positive mean connection success if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) { // Login using username & password boolean status = mFTPClient.login(user, pswd); mFTPClient.setFileType(FTP.BINARY_FILE_TYPE); mFTPClient.enterLocalPassiveMode(); mFTPClient.changeWorkingDirectory(Constants.DIR_ROBOT_MEDIA); FTPFile[] fileList = mFTPClient.listFiles(); long timestamp = 0l; String nameFile = ""; for (int i = 0; i < fileList.length; i++) { if (fileList[i].isFile() && fileList[i].getTimestamp().getTimeInMillis() > timestamp) { timestamp = fileList[i].getTimestamp().getTimeInMillis(); nameFile = fileList[i].getName(); } } Log.d(TAG, "File da scaricare: " + nameFile); mFTPClient.enterLocalActiveMode(); File folder = new File(Constants.DIR_ROBOT_IMG); OutputStream outputStream = null; boolean success = true; if (!folder.exists()) { success = folder.mkdir(); } try { outputStream = new FileOutputStream(folder.getAbsolutePath() + "/" + nameFile); success = mFTPClient.retrieveFile(nameFile, outputStream); } catch (Exception e) { return e.getMessage(); } finally { if (outputStream != null) { outputStream.close(); } } if (success) { result = nameFile; mFTPClient.deleteFile(nameFile); } } } catch (Exception e) { Log.e(TAG, e.getMessage()); } finally { if (mFTPClient != null) { try { mFTPClient.logout(); mFTPClient.disconnect(); } catch (IOException e) { Log.e(TAG, e.getMessage()); } } } return result; }
From source file:com.maxl.java.aips2sqlite.AllDown.java
public void downIBSA() { String fl = ""; String fp = ""; String fs = ""; try {/*w ww . j a va 2s . c om*/ FileInputStream glnCodesCsv = new FileInputStream(Constants.DIR_IBSA + "/access.ami.csv"); BufferedReader br = new BufferedReader(new InputStreamReader(glnCodesCsv, "UTF-8")); String line; while ((line = br.readLine()) != null) { // Semicolon is used as a separator String[] gln = line.split(";"); if (gln.length > 2) { if (gln[0].equals("IbsaAmiko")) { fl = gln[0]; fp = gln[1]; fs = gln[2]; } } } br.close(); } catch (Exception e) { e.printStackTrace(); } FTPClient ftp_client = new FTPClient(); try { ftp_client.connect(fs, 21); ftp_client.login(fl, fp); ftp_client.enterLocalPassiveMode(); ftp_client.changeWorkingDirectory("data"); ftp_client.setFileType(FTP.BINARY_FILE_TYPE); int reply = ftp_client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp_client.disconnect(); System.err.println("FTP server refused connection."); return; } System.out.println("- Connected to server " + fs + "..."); //get list of filenames FTPFile[] ftpFiles = ftp_client.listFiles(); List<String> list_remote_files = Arrays.asList("Konditionen.csv", "Targeting_diff.csv", "Address.csv"); List<String> list_local_files = Arrays.asList(Constants.FILE_CUST_IBSA, Constants.FILE_TARG_IBSA, Constants.FILE_MOOS_ADDR); if (ftpFiles != null && ftpFiles.length > 0) { int index = 0; for (String remote_file : list_remote_files) { OutputStream os = new FileOutputStream(Constants.DIR_IBSA + "/" + list_local_files.get(index)); System.out.print("- Downloading " + remote_file + " from server " + fs + "... "); boolean done = ftp_client.retrieveFile(remote_file, os); if (done) System.out.println("file downloaded successfully."); else System.out.println("error."); os.close(); index++; } } } catch (IOException ex) { System.out.println("Error: " + ex.getMessage()); ex.printStackTrace(); } finally { try { if (ftp_client.isConnected()) { ftp_client.logout(); ftp_client.disconnect(); } } catch (IOException ex) { ex.printStackTrace(); } } }
From source file:com.cws.esolutions.core.utils.NetworkUtils.java
/** * Creates a connection to a target host and then executes an FTP * request to send or receive a file to or from the target. This is fully * key-based, as a result, a keyfile is required for the connection to * successfully authenticate./*from w w w.j av a 2s .co m*/ * * @param sourceFile - The full path to the source file to transfer * @param targetFile - The full path (including file name) of the desired target file * @param targetHost - The target server to perform the transfer to * @param isUpload - <code>true</code> is the transfer is an upload, <code>false</code> if it * is a download * @throws UtilityException {@link com.cws.esolutions.core.utils.exception.UtilityException} if an error occurs processing */ public static final synchronized void executeFtpConnection(final String sourceFile, final String targetFile, final String targetHost, final boolean isUpload) throws UtilityException { final String methodName = NetworkUtils.CNAME + "#executeFtpConnection(final String sourceFile, final String targetFile, final String targetHost, final boolean isUpload) throws UtilityException"; if (DEBUG) { DEBUGGER.debug(methodName); DEBUGGER.debug("Value: {}", sourceFile); DEBUGGER.debug("Value: {}", targetFile); DEBUGGER.debug("Value: {}", targetHost); DEBUGGER.debug("Value: {}", isUpload); } final FTPClient client = new FTPClient(); final FTPConfig ftpConfig = appBean.getConfigData().getFtpConfig(); if (DEBUG) { DEBUGGER.debug("FTPClient: {}", client); DEBUGGER.debug("FTPConfig: {}", ftpConfig); } try { client.connect(targetHost); if (DEBUG) { DEBUGGER.debug("FTPClient: {}", client); } if (!(client.isConnected())) { throw new IOException("Failed to authenticate to remote host with the provided information"); } boolean isAuthenticated = false; if (StringUtils.isNotBlank(ftpConfig.getFtpAccount())) { isAuthenticated = client.login( (StringUtils.isNotEmpty(ftpConfig.getFtpAccount())) ? ftpConfig.getFtpAccount() : System.getProperty("user.name"), PasswordUtils.decryptText(ftpConfig.getFtpPassword(), ftpConfig.getFtpSalt(), secBean.getConfigData().getSecurityConfig().getEncryptionAlgorithm(), secBean.getConfigData().getSecurityConfig().getIterations(), secBean.getConfigData().getSecurityConfig().getKeyBits(), secBean.getConfigData().getSecurityConfig().getEncryptionAlgorithm(), secBean.getConfigData().getSecurityConfig().getEncryptionInstance(), appBean.getConfigData().getSystemConfig().getEncoding())); } else { isAuthenticated = client.login(ftpConfig.getFtpAccount(), null); } if (DEBUG) { DEBUGGER.debug("isAuthenticated: {}", isAuthenticated); } if (!(isAuthenticated)) { throw new IOException("Failed to connect to FTP server: " + targetHost); } client.enterLocalPassiveMode(); if (!(FileUtils.getFile(sourceFile).exists())) { throw new IOException("File " + sourceFile + " does not exist. Skipping"); } if (isUpload) { client.storeFile(targetFile, new FileInputStream(FileUtils.getFile(sourceFile))); } else { client.retrieveFile(sourceFile, new FileOutputStream(targetFile)); } if (DEBUG) { DEBUGGER.debug("Reply: {}", client.getReplyCode()); DEBUGGER.debug("Reply: {}", client.getReplyString()); } } catch (IOException iox) { throw new UtilityException(iox.getMessage(), iox); } finally { try { if (client.isConnected()) { client.completePendingCommand(); client.disconnect(); } } catch (IOException iox) { ERROR_RECORDER.error(iox.getMessage(), iox); } } }
From source file:com.maxl.java.aips2sqlite.AllDown.java
public void downDesitin() { String fl = ""; String fp = ""; String fs = ""; try {/* ww w. j a v a 2 s. c o m*/ FileInputStream access = new FileInputStream(Constants.DIR_DESITIN + "/access.ami.csv"); BufferedReader br = new BufferedReader(new InputStreamReader(access, "UTF-8")); String line; while ((line = br.readLine()) != null) { // Semicolon is used as a separator String[] gln = line.split(";"); if (gln.length > 2) { if (gln[0].equals("ftp_amiko")) { fl = gln[0]; fp = gln[1]; fs = gln[2]; } } } br.close(); } catch (Exception e) { e.printStackTrace(); } FTPClient ftp_client = new FTPClient(); try { ftp_client.connect(fs, 21); ftp_client.login(fl, fp); ftp_client.enterLocalPassiveMode(); ftp_client.setFileType(FTP.BINARY_FILE_TYPE); System.out.println("- Connected to server " + fs + "..."); // Set working directory String working_dir = "ywesee_in"; ftp_client.changeWorkingDirectory(working_dir); int reply = ftp_client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp_client.disconnect(); System.err.println("FTP server refused connection."); return; } // Get list of filenames FTPFile[] ftpFiles = ftp_client.listFiles(); if (ftpFiles != null && ftpFiles.length > 0) { // ... then download all csv files for (FTPFile f : ftpFiles) { String remote_file = f.getName(); if (remote_file.endsWith("csv")) { String local_file = remote_file; if (remote_file.startsWith("Kunden")) local_file = Constants.FILE_CUST_DESITIN; if (remote_file.startsWith("Artikel")) local_file = Constants.FILE_ARTICLES_DESITIN; OutputStream os = new FileOutputStream(Constants.DIR_DESITIN + "/" + local_file); System.out.print("- Downloading " + remote_file + " from server " + fs + "... "); boolean done = ftp_client.retrieveFile(remote_file, os); if (done) System.out.println("success."); else System.out.println("error."); os.close(); } } } } catch (IOException ex) { System.out.println("Error: " + ex.getMessage()); ex.printStackTrace(); } finally { try { if (ftp_client.isConnected()) { ftp_client.logout(); ftp_client.disconnect(); } } catch (IOException ex) { ex.printStackTrace(); } } }
From source file:com.maxl.java.aips2sqlite.AllDown.java
public void downZurRose() { String fl = ""; String fp = ""; String fs = ""; try {//from w ww . j a v a2 s. co m FileInputStream access = new FileInputStream(Constants.DIR_ZURROSE + "/access.ami.csv"); BufferedReader br = new BufferedReader(new InputStreamReader(access, "UTF-8")); String line; while ((line = br.readLine()) != null) { // Semicolon is used as a separator String[] gln = line.split(";"); if (gln.length > 2) { if (gln[0].equals("P_ywesee")) { fl = gln[0]; fp = gln[1]; fs = gln[2]; } } } br.close(); } catch (Exception e) { e.printStackTrace(); } FTPClient ftp_client = new FTPClient(); try { ftp_client.connect(fs, 21); ftp_client.login(fl, fp); ftp_client.enterLocalPassiveMode(); ftp_client.setFileType(FTP.BINARY_FILE_TYPE); System.out.println("- Connected to server " + fs + "..."); String[] working_dir = { "ywesee out", "../ywesee in" }; for (int i = 0; i < working_dir.length; ++i) { // Set working directory ftp_client.changeWorkingDirectory(working_dir[i]); int reply = ftp_client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp_client.disconnect(); System.err.println("FTP server refused connection."); return; } // Get list of filenames FTPFile[] ftpFiles = ftp_client.listFiles(); if (ftpFiles != null && ftpFiles.length > 0) { // ... then download all csv files for (FTPFile f : ftpFiles) { String remote_file = f.getName(); if (remote_file.endsWith("csv")) { String local_file = remote_file; if (remote_file.startsWith("Artikelstamm")) local_file = Constants.CSV_FILE_DISPO_ZR; OutputStream os = new FileOutputStream(Constants.DIR_ZURROSE + "/" + local_file); System.out.print("- Downloading " + remote_file + " from server " + fs + "... "); boolean done = ftp_client.retrieveFile(remote_file, os); if (done) System.out.println("success."); else System.out.println("error."); os.close(); } } } } } catch (IOException ex) { System.out.println("Error: " + ex.getMessage()); ex.printStackTrace(); } finally { try { if (ftp_client.isConnected()) { ftp_client.logout(); ftp_client.disconnect(); } } catch (IOException ex) { ex.printStackTrace(); } } }