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.webarch.common.net.ftp.FtpService.java
/** * // ww w. j a va 2 s .c o m * * @param remotePath ftp * @param fileName ??? * @param localPath ??? * @return true/false ? */ public boolean downloadFile(String remotePath, String fileName, String localPath) { boolean success = false; FTPClient ftpClient = new FTPClient(); try { int replyCode; ftpClient.connect(url, port); ftpClient.login(userName, password); replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) { return false; } ftpClient.changeWorkingDirectory(remotePath); FTPFile[] files = ftpClient.listFiles(); for (FTPFile file : files) { if (file.getName().equals(fileName)) { File localFile = new File(localPath + File.separator + file.getName()); OutputStream outputStream = new FileOutputStream(localFile); ftpClient.retrieveFile(file.getName(), outputStream); outputStream.close(); } } ftpClient.logout(); success = true; } catch (IOException e) { logger.error("ftp?", e); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException e) { logger.error("ftp?", e); } } } return success; }
From source file:com.haha01haha01.harail.DatabaseDownloader.java
private Boolean downloadFile(String server, int portNumber, String user, String password, String filename, File localFile) throws IOException { FTPClient ftp = null; try {//from ww w .ja v a2 s .co m ftp = new FTPClient(); ftp.setBufferSize(1024 * 1024); ftp.connect(server, portNumber); Log.d(NAME, "Connected. Reply: " + ftp.getReplyString()); if (!ftp.login(user, password)) { return false; } Log.d(NAME, "Logged in"); if (!ftp.setFileType(FTP.BINARY_FILE_TYPE)) { return false; } Log.d(NAME, "Downloading"); ftp.enterLocalPassiveMode(); OutputStream outputStream = null; boolean success = false; try { outputStream = new BufferedOutputStream(new FileOutputStream(localFile)); success = ftp.retrieveFile(filename, outputStream); } finally { if (outputStream != null) { outputStream.close(); } } return success; } finally { if (ftp != null) { ftp.logout(); ftp.disconnect(); } } }
From source file:joshuatee.wx.UtilityFTP.java
public static String[] GetNidsArr(Context c, String url, String path, String frame_cnt_str) { int frame_cnt = Integer.parseInt(frame_cnt_str); String[] nids_arr = new String[frame_cnt]; try {/*w ww .j a v a 2s . c om*/ FTPClient ftp = new FTPClient(); //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(); FTPFile[] ftpFiles = ftp.listFiles(); //get newest .xml file name from ftp server java.util.Date lastMod = ftpFiles[0].getTimestamp().getTime(); FTPFile choice = ftpFiles[0]; for (FTPFile file : ftpFiles) { if (file.getTimestamp().getTime().after(lastMod) && !file.getName().equals("sn.last")) { choice = file; lastMod = file.getTimestamp().getTime(); } } int seq = Integer.parseInt(choice.getName().replace("sn.", "")); // was ALl int j = 0; int k = seq - frame_cnt + 1; for (j = 0; j < frame_cnt; j++) { // files range from 0000 to 0250, if num is negative add 251 int tmp_k = k; if (tmp_k < 0) tmp_k = tmp_k + 251; nids_arr[j] = "sn." + String.format("%4s", Integer.toString(tmp_k)).replace(' ', '0'); k++; } FileOutputStream fos; for (j = 0; j < frame_cnt; j++) { fos = c.openFileOutput(nids_arr[j], Context.MODE_PRIVATE); ftp.retrieveFile(nids_arr[j], fos); fos.close(); } ftp.logout(); ftp.disconnect(); } catch (Exception ex) { ex.printStackTrace(); } return nids_arr; }
From source file:edu.wisc.ssec.mcidasv.data.cyclone.AtcfStormDataSource.java
/** * _more_//from w w w .j a v a 2 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:edu.mda.bioinfo.ids.DownloadFiles.java
private void downloadFromFtpToFile(String theServer, String theServerFile, String theLocalFile) throws IOException, Exception { FTPClient ftp = new FTPClient(); try {//from www .ja v a 2 s.co m int reply = 0; boolean replyB = false; ftp.connect(theServer); System.out.print(ftp.getReplyString()); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); System.err.println("FTP server refused connection."); } else { ftp.login("anonymous", "anonymous"); replyB = ftp.setFileType(FTP.BINARY_FILE_TYPE); System.out.print(ftp.getReplyString()); System.out.println("replyB= " + replyB); if (false == replyB) { throw new Exception("Unable to login to " + theServer); } OutputStream output = new FileOutputStream(theLocalFile); replyB = ftp.retrieveFile(theServerFile, output); System.out.print(ftp.getReplyString()); System.out.println("replyB= " + replyB); if (false == replyB) { throw new Exception("Unable to retrieve " + theServerFile); } } ftp.logout(); } catch (IOException rethrownExp) { System.err.println("exception " + rethrownExp.getMessage()); throw rethrownExp; } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ignore) { // do nothing } } } }
From source file:com.shigeodayo.ardrone.utils.ARDroneInfo.java
private boolean connectToDroneThroughFtp() { FTPClient client = new FTPClient(); BufferedOutputStream bos = null; try {/*from w w w . ja v a 2s. c om*/ client.connect(ARDroneConstants.IP_ADDRESS, ARDroneConstants.FTP_PORT); if (!client.login("anonymous", "")) { ARDrone.error("Login failed", this); return false; } client.setFileType(FTP.BINARY_FILE_TYPE); bos = new BufferedOutputStream(new OutputStream() { @Override public void write(int arg0) throws IOException { //System.out.println("aa:" + (char)arg0); switch (count) { case 0: major = arg0 - '0'; break; case 2: minor = arg0 - '0'; break; case 4: revision = arg0 - '0'; break; default: break; } count++; } }); if (!client.retrieveFile("/" + VERSION_FILE_NAME, bos)) { ARDrone.error("Cannot find \"" + VERSION_FILE_NAME + "\"", this); return false; } bos.flush(); //System.out.print("major:" + major); //System.out.print(" minor:" + minor); //System.out.println(" revision:" + revision); //System.out.println("done"); } catch (IOException e) { e.printStackTrace(); return false; } finally { try { if (bos != null) { bos.flush(); bos.close(); } client.disconnect(); } catch (IOException e) { e.printStackTrace(); return false; } } return true; }
From source file:com.claim.support.FtpUtil.java
public void uploadMutiFileWithFTP(String targetDirectory, FileTransferController fileTransferController) { try {// w w w . j a v a 2 s .c om FtpProperties properties = new ResourcesProperties().loadFTPProperties(); FTPClient ftp = new FTPClient(); ftp.connect(properties.getFtp_server()); if (!ftp.login(properties.getFtp_username(), properties.getFtp_password())) { ftp.logout(); } int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); } ftp.enterLocalPassiveMode(); System.out.println("Remote system is " + ftp.getSystemName()); ftp.changeWorkingDirectory(targetDirectory); System.out.println("Current directory is " + ftp.printWorkingDirectory()); FTPFile[] ftpFiles = ftp.listFiles(); if (ftpFiles != null && ftpFiles.length > 0) { for (FTPFile file : ftpFiles) { if (!file.isFile()) { continue; } System.out.println("File is " + file.getName()); OutputStream output; output = new FileOutputStream( properties.getFtp_remote_directory() + File.separator + file.getName()); ftp.retrieveFile(file.getName(), output); output.close(); } } ftp.logout(); ftp.disconnect(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:com.eryansky.common.utils.ftp.FtpFactory.java
/** * FTP?.//from www . j a v a2 s .c o m * * @param remotePath * FTP? * @param fileName * ??? * @param localPath * ?? * @return */ public boolean ftpDownFile(String remotePath, String fileName, String localPath) { // ? boolean success = false; // FTPClient FTPClient ftp = new FTPClient(); ftp.setControlEncoding("GBK"); try { int reply; // FTP? // ??ftp.connect(url)?FTP? ftp.connect(url, port); // ftp ftp.login(username, password); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return success; } // ftp.changeWorkingDirectory(remotePath); // 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(); success = true; } } ftp.logout(); // ? } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return success; }
From source file:main.TestManager.java
/** * Deletes all local tests, downloads tests from the server * and saves all downloaded test in the local directory. *///from w w w. j a v a 2 s. c o m public static void syncTestsWithServer() { FTPClient ftp = new FTPClient(); boolean error = false; try { int reply; String server = "zajicek.endora.cz"; ftp.connect(server, 21); // After connection attempt, we check the reply code to verify // success. reply = ftp.getReplyCode(); //Not connected successfully - inform the user if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); Debugger.println("FTP server refused connection."); ErrorInformer.failedSyncing(); return; } // LOGIN boolean success = ftp.login("plakato", "L13nK4"); Debugger.println("Login successful: " + success); if (success == false) { ftp.disconnect(); ErrorInformer.failedSyncing(); return; } ftp.enterLocalPassiveMode(); // Get all files from the server FTPFile[] files = ftp.listFiles(); System.out.println("Got files! Count: " + files.length); // Delete all current test to be replaced with // actulized version File[] locals = new File("src/tests/").listFiles(); for (File f : locals) { if (f.getName() == "." || f.getName() == "..") { continue; } f.delete(); } // Copy the files from server to local folder int failed = 0; for (FTPFile f : files) { if (f.isFile()) { Debugger.println(f.getName()); if (f.getName() == "." || f.getName() == "..") { continue; } File file = new File("src/tests/" + f.getName()); file.createNewFile(); OutputStream output = new FileOutputStream(file); if (!ftp.retrieveFile(f.getName(), output)) failed++; output.close(); } } // If we failed to download some file, inform the user if (failed != 0) { ftp.disconnect(); ErrorInformer.failedSyncing(); return; } // Disconnect ftp client or resolve the potential error ftp.logout(); } catch (IOException e) { error = true; e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { // do nothing } } } if (error) { ErrorInformer.failedSyncing(); } Alert alert = new Alert(AlertType.INFORMATION); alert.setTitle("Download hotov"); alert.setHeaderText(null); alert.setContentText("Vetky testy boli zo servru spene stiahnut."); alert.showAndWait(); alert.close(); }
From source file:lucee.runtime.tag.Ftp.java
/** * gets a file from server and copy it local * @return FTPCLient//from w w w. j av a2s . c om * @throws PageException * @throws IOException */ private FTPClient actionGetFile() throws PageException, IOException { required("remotefile", remotefile); required("localfile", localfile); FTPClient client = getClient(); Resource local = ResourceUtil.toResourceExistingParent(pageContext, localfile);//new File(localfile); pageContext.getConfig().getSecurityManager().checkFileLocation(local); if (failifexists && local.exists()) throw new ApplicationException("File [" + local + "] already exist, if you want to overwrite, set attribute failIfExists to false"); OutputStream fos = null; client.setFileType(getType(local)); try { fos = IOUtil.toBufferedOutputStream(local.getOutputStream()); client.retrieveFile(remotefile, fos); } finally { IOUtil.closeEL(fos); } writeCfftp(client); return client; }