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.kcs.core.utilities.FtpUtil.java
public static boolean getFile(String hostname, int port, String username, String password, String remoteFileName, String localFileName) throws Exception { FTPClient ftp = null; boolean result = false; try {//w w w . j a v a 2 s .c o m System.out.println("File has been start downloaded."); ftp = FtpUtil.openFtpConnect(hostname, port, username, password); if (null != ftp && ftp.isConnected()) { File downloadFile = new File(localFileName); if (!FtpUtil.checkFileExists(remoteFileName, ftp)) { System.out.println("File not found in server."); throw new Exception("File not found in server."); } OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile)); result = ftp.retrieveFile(remoteFileName, outputStream); outputStream.close(); } System.out.println("File [" + remoteFileName + "] has been downloaded Complete."); } catch (Exception e) { e.printStackTrace(); throw new Exception("Error : " + e.getMessage()); } finally { FtpUtil.closeFtpServer(ftp); } return result; }
From source file:joshuatee.wx.UtilityFTP.java
public static void GetNids(Context c, String url, String path) { try {/* ww w. j av a 2 s . c om*/ FTPClient ftp = new FTPClient(); //String url = "tgftp.nws.noaa.gov"; //String user = "ftp"; //String pass = "anonymous"; ftp.connect(url); if (!ftp.login("ftp", "anonymous")) { ftp.logout(); } ftp.setFileType(FTP.BINARY_FILE_TYPE); int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); } ftp.enterLocalPassiveMode(); ftp.changeWorkingDirectory(path); //reply = ftp.getReplyCode(); //String fn = "sn.last"; FileOutputStream fos = c.openFileOutput("nids", Context.MODE_PRIVATE); ftp.retrieveFile("sn.last", fos); //reply = ftp.getReplyCode(); fos.close(); ftp.logout(); ftp.disconnect(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:moskitt4me.repositoryClient.core.util.RepositoryClientUtil.java
public static void downloadFragment(RepositoryLocation location, String fileName, String folder) throws IOException { FTPClient client = RepositoryClientUtil.connect(location, false); if (client != null) { //Download the file String destination = folder + "/" + fileName; FileOutputStream fos = new FileOutputStream(destination); client.retrieveFile(fileName, fos); fos.close();//from w w w. j a v a2 s . c om //Disconnect the client RepositoryClientUtil.disconnect(client); } }
From source file:cn.zhuqi.mavenssh.web.util.test.FtpTest.java
/** * Description: FTP?//w w w . ja v a 2 s .com * * @Version1.0 * @param url FTP?hostname * @param port FTP?? * @param username FTP? * @param password FTP? * @param remotePath FTP? * @param fileName ??? * @param localPath ?? * @return */ public static boolean downloadFile(String url, int port, String username, String password, String remotePath, String fileName, String localPath) { boolean success = false; FTPClient ftp = new FTPClient(); try { int reply; // FTP? if (port > -1) { ftp.connect(url, port); } else { ftp.connect(url); } ftp.login(username, password);// ftp.setFileType(FTPClient.BINARY_FILE_TYPE); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return success; } ftp.changeWorkingDirectory(remotePath);//FTP? FTPFile[] fs = ftp.listFiles(); for (FTPFile ff : fs) { if (ff.getName().equals(fileName)) { File localFile = new File(localPath + "/" + ff.getName()); OutputStream is = new FileOutputStream(localFile); ftp.retrieveFile(ff.getName(), is); is.close(); } } ftp.logout(); success = true; } catch (IOException e) { } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException e) { } } } return success; }
From source file:AIR.Common.Web.FileFtpHandler.java
/** * Makes FTP call to the provided URI and retrieves contents * /* w w w . j a v a 2 s . c o m*/ * @param uri * @return ByteArrayInputStream * @throws FtpResourceException */ public static byte[] getBytes(URI uri) throws FtpResourceException { try { FTPClient ftp = new FTPClient(); String[] credentialsAndHost = uri.getAuthority().split("@"); String host = credentialsAndHost[1]; String[] credentials = credentialsAndHost[0].split(":"); ftp.connect(host); ftp.enterLocalPassiveMode(); if (!ftp.login(credentials[0], credentials[1])) { ftp.logout(); ftp.disconnect(); throw new RuntimeException("FTP Authentication Failure"); } int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.logout(); ftp.disconnect(); throw new RuntimeException("FTP No reponse from server"); } ftp.setFileType(FTPClient.BINARY_FILE_TYPE); ByteArrayOutputStream output = new ByteArrayOutputStream(); ftp.retrieveFile(uri.getPath(), output); output.close(); ftp.logout(); ftp.disconnect(); return output.toByteArray(); } catch (IOException ex) { throw new FtpResourceException(ex); } }
From source file:edu.tum.cs.ias.knowrob.utils.ResourceRetriever.java
/** * Retrieve a file to a temporary path. This temporary path will be returned. Valid protocol * types: ftp, http, package or local file If a file has already be downloaded (the file is * existing in tmp directory) it will not be redownloaded again. Simply the path to this file * will be returned.//from w w w.ja va 2 s.co m * * @param url * URL to retrieve * @param checkAlreadyRetrieved * if false, always download the file and ignore, if it is already existing * @return NULL on error. On success the path to the file is returned. */ public static File retrieve(String url, boolean checkAlreadyRetrieved) { if (url.indexOf("://") <= 0) { // Is local file return new File(url); } int start = url.indexOf('/') + 2; int end = url.indexOf('/', start); String serverName = url.substring(start, end); if (url.startsWith("package://")) { String filePath = url.substring(end + 1); String pkgPath = findPackage(serverName); if (pkgPath == null) return null; return new File(pkgPath, filePath); } else if (url.startsWith("http://")) { OutputStream out = null; URLConnection conn = null; InputStream in = null; String filename = url.substring(url.lastIndexOf('/') + 1); File tmpPath = getTmpName(url, filename); if (checkAlreadyRetrieved && tmpPath.exists()) { return tmpPath; } try { // Get the URL URL urlClass = new URL(url); // Open an output stream to the destination file on our local filesystem out = new BufferedOutputStream(new FileOutputStream(tmpPath)); conn = urlClass.openConnection(); in = conn.getInputStream(); // Get the data byte[] buffer = new byte[1024]; int numRead; while ((numRead = in.read(buffer)) != -1) { out.write(buffer, 0, numRead); } // Done! Just clean up and get out } catch (Exception exception) { exception.printStackTrace(); } finally { try { if (in != null) { in.close(); } if (out != null) { out.close(); } } catch (IOException ioe) { // Ignore if stream not possible to close } } return tmpPath; } else if (url.startsWith("ftp://")) { FTPClient client = new FTPClient(); OutputStream outStream = null; String filename = url.substring(url.lastIndexOf('/') + 1); File tmpPath = getTmpName(url, filename); if (checkAlreadyRetrieved && tmpPath.exists()) { System.out.println("Already retrieved: " + url + " to " + tmpPath.getAbsolutePath()); return tmpPath; } try { // Connect to the FTP server as anonymous client.connect(serverName); client.login("anonymous", "knowrob@example.com"); String remoteFile = url.substring(end); // Write the contents of the remote file to a FileOutputStream outStream = new FileOutputStream(tmpPath); client.retrieveFile(remoteFile, outStream); } catch (IOException ioe) { System.out.println("ResourceRetriever: Error communicating with FTP server: " + serverName + "\n" + ioe.getMessage()); } finally { try { outStream.close(); } catch (IOException e1) { // Ignore if stream not possible to close } try { client.disconnect(); } catch (IOException e) { System.out.println("ResourceRetriever: Problem disconnecting from FTP server: " + serverName + "\n" + e.getMessage()); } } return tmpPath; } return null; }
From source file:com.ephesoft.dcma.util.FTPUtil.java
/** * API to download files from an arbitrary directory hierarchy on the remote ftp server * /*from www . j ava2 s. co m*/ * @param client {@link FTPClient} * @param ftpDirectory{{@link String} the directory tree only delimited with / chars. No file name! * @param destDirectoryPath {@link String} the path where file will be downloaded. * @throws Exception */ public static void retrieveFiles(final FTPClient client, final String ftpDirectory, final String destDirectoryPath) throws IOException { boolean dirExists = true; String[] directories = ftpDirectory.split(DIRECTORY_SEPARATOR); for (String dir : directories) { if (!dir.isEmpty()) { if (dirExists) { dirExists = client.changeWorkingDirectory(dir); } } } FTPFile[] fileList = client.listFiles(); if (fileList != null && fileList.length > 0) { String outputFilePath; FileOutputStream fileOutputStream; for (FTPFile file : fileList) { outputFilePath = EphesoftStringUtil.concatenate(destDirectoryPath, File.separator, file.getName()); fileOutputStream = new FileOutputStream(outputFilePath); client.retrieveFile(file.getName(), fileOutputStream); fileOutputStream.close(); } } }
From source file:de.l3s.dlg.ncbikraken.ftp.NcbiFTPClient.java
public static void getMedline(MedlineFileType type) { FileOutputStream out = null;/*from www . ja v a2s. c om*/ FTPClient ftp = new FTPClient(); try { // Connection String LOGGER.info("Connecting to FTP server " + SERVER_NAME); ftp.connect(SERVER_NAME); ftp.login("anonymous", ""); ftp.cwd(BASELINE_PATH); ftp.cwd(type.getServerPath()); try { ftp.pasv(); } catch (IOException e) { LOGGER.error( "Can not access the passive mode. Maybe a problem with your (Windows) firewall. Just try to run as administrator: \nnetsh advfirewall set global StatefulFTP disable"); return; } for (FTPFile file : ftp.listFiles()) { if (file.isFile()) { File meshF = new File(file.getName()); LOGGER.debug("Downloading file: " + SERVER_NAME + ":" + BASELINE_PATH + "/" + type.getServerPath() + "/" + meshF.getName()); out = new FileOutputStream(Configuration.INSTANCE.getMedlineDir() + File.separator + meshF); ftp.retrieveFile(meshF.getName(), out); out.flush(); out.close(); } } } catch (IOException ioe) { LOGGER.error(ioe.getMessage()); } finally { IOUtils.closeQuietly(out); try { ftp.disconnect(); } catch (IOException e) { LOGGER.error(e.getMessage()); } } }
From source file:moskitt4me.repositoryClient.core.util.RepositoryClientUtil.java
public static String downloadFragment(RepositoryLocation location, String fileName) throws IOException { FTPClient client = RepositoryClientUtil.connect(location, false); String tempDir = ""; if (client != null) { String eclipseInstallationDirectory = Platform.getInstallLocation().getURL().getPath(); tempDir = RepositoryClientUtil.createFolder(eclipseInstallationDirectory + "tmp", 0); //Download the file String destination = tempDir + "/" + fileName; FileOutputStream fos = new FileOutputStream(destination); client.retrieveFile(fileName, fos); fos.close();/*w ww . j a v a 2 s . com*/ //Disconnect the client RepositoryClientUtil.disconnect(client); } return tempDir; }
From source file:de.cismet.cids.custom.utils.formsolutions.FormSolutionFtpClient.java
/** * DOCUMENT ME!//w w w. j a va 2 s. com * * @param destinationPath DOCUMENT ME! * @param out DOCUMENT ME! * * @throws Exception DOCUMENT ME! */ public void download(final String destinationPath, final OutputStream out) throws Exception { final FTPClient connectedFtpClient = getConnectedFTPClient(); connectedFtpClient.enterLocalPassiveMode(); connectedFtpClient.setFileType(BINARY_FILE_TYPE); if (!connectedFtpClient.retrieveFile(destinationPath, out)) { throw new Exception("file " + destinationPath + " not found"); } }