List of usage examples for org.apache.commons.net.ftp FTPClient enterLocalPassiveMode
public void enterLocalPassiveMode()
PASSIVE_LOCAL_DATA_CONNECTION_MODE
. From source file:gtu._work.ui.ObnfCheckPDFErrorUI.java
private void checkBtnActionPerformed() { try {//from w ww . j a va 2 s .c o m for (FtpSite ftpSite : FtpSite.values()) { FtpUtil ftpUtil = new FtpUtil(); ftpUtil.connect(ftpSite.ip, ftpSite.port, ftpSite.userId, ftpSite.password, false); FTPClient ftp = ftpUtil.getFtp(); logArea.append("?" + ftpSite.label + "\n"); ftp.enterLocalPassiveMode(); FTPFile[] ftpFiles = ftp.listFiles(ftpSite.path1); logArea.append("-->" + ftpSite.path1 + "\n"); for (int i = 0; i < ((ftpFiles == null) ? 0 : ftpFiles.length); i++) { FTPFile ftpFile = ftpFiles[i]; if (ftpFile.isDirectory()) { String p = ftpSite.path1 + "/" + ftpFile.getName() + "/" + ftpSite.path2 + "/"; logArea.append("-->" + p + "\n"); List<FtpFileInfo> fileList = new ArrayList<FtpFileInfo>(); ftpUtil.scanFindFile(p, ".*", fileList, ftp); for (FtpFileInfo f : fileList) { logArea.append("-->" + f.getAbsolutePath() + "--" + f.getSize() + "\n"); if (!f.isDirectory() && f.getSize() == 0) { logArea.append("##" + ftpSite.label + "\t" + f.getAbsolutePath() + "\n"); } } } } ftpUtil.disconnect(); logArea.append("?:" + ftpSite.label); } JCommonUtil._jOptionPane_showMessageDialog_info("!"); } catch (Exception ex) { JCommonUtil.handleException(ex); } }
From source file:br.com.tiagods.util.FTPDownload.java
public boolean downloadFile(String arquivo) { FTPConfig cf = FTPConfig.getInstance(); FTPClient ftp = new FTPClient(); try {//from w w w. j a v a 2 s . c o m ftp.connect(cf.getValue("host"), Integer.parseInt(cf.getValue("port"))); ftp.login(cf.getValue("user"), cf.getValue("pass")); ftp.enterLocalPassiveMode(); ftp.setFileType(FTP.BINARY_FILE_TYPE); String remoteFile1 = cf.getValue("dirFTP") + "/" + arquivo; novoArquivo = new File(System.getProperty("java.io.tmpdir") + "/" + arquivo); try (OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(novoArquivo))) { boolean success = ftp.retrieveFile(remoteFile1, outputStream1); if (success) { return true; } } return false; } catch (IOException e) { System.out.println("Erro = " + e.getMessage()); return false; } finally { try { if (ftp.isConnected()) { ftp.logout(); ftp.disconnect(); } } catch (IOException ex) { ex.printStackTrace(); } } }
From source file:com.unicomer.opos.inhouse.gface.ejb.impl.GfaceGuatefacturasControlFtpEjbLocalImpl.java
public List<String> getFilesExists(List<String> fileNames) { List<String> ret = new ArrayList<String>(); try {/* ww w. j a v a2 s . co m*/ HashMap<String, String> params = getFtpParams(); FTPClient ftpClient = getFtpClient(params); BufferedInputStream buffIn; ftpClient.enterLocalPassiveMode(); for (String nombre : fileNames) { buffIn = new BufferedInputStream(ftpClient .retrieveFileStream(params.get("remoteRetreiveFiles") + REMOTE_SEPARATOR + nombre));//Ruta completa de alojamiento en el FTP if (ftpClient.getReplyCode() == 150) { System.out.println("Archivo obtenido exitosamente"); buffIn.close(); ret.add(nombre); } else { System.out.println( "No se pudo obtener el archivo: " + nombre + ", codigo:" + ftpClient.getReplyCode()); } } ftpClient.logout(); //Cerrar sesin ftpClient.disconnect();//Desconectarse del servidor } catch (Exception e) { System.out.println("Error: " + e.getMessage()); } return ret; }
From source file:com.unicomer.opos.inhouse.gface.ejb.impl.GfaceGuatefacturasControlFtpEjbLocalImpl.java
public void receive(List<String> fileNames) { try {/* w w w . j a va2s.c o m*/ HashMap<String, String> params = getFtpParams(); FTPClient ftpClient = getFtpClient(params); BufferedInputStream buffIn; ftpClient.enterLocalPassiveMode(); for (String nombre : fileNames) { buffIn = new BufferedInputStream(ftpClient .retrieveFileStream(params.get("remoteRetreiveFiles") + REMOTE_SEPARATOR + nombre));//Ruta completa de alojamiento en el FTP if (ftpClient.getReplyCode() == 150) { System.out.println("Archivo obtenido exitosamente"); // write the inputStream to a FileOutputStream OutputStream outputStream = new FileOutputStream( new File(params.get("localStoreFiles") + File.separator + nombre)); int read = 0; byte[] bytes = new byte[1024]; while ((read = buffIn.read(bytes)) != -1) { outputStream.write(bytes, 0, read); } buffIn.close(); //Cerrar envio de arcivos al FTP outputStream.close(); } else { System.out.println( "No se pudo obtener el archivo: " + nombre + ", codigo:" + ftpClient.getReplyCode()); } } ftpClient.logout(); //Cerrar sesin ftpClient.disconnect();//Desconectarse del servidor } catch (Exception e) { System.out.println("Error: " + e.getMessage()); } }
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 a2 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:main.TestManager.java
/** * Deletes all files on the server and uploads all the * tests from the local directory.//from ww w. j a va2 s. c om */ public static void syncServerWithTest() { 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 tests on the server to be replaced with // actualized version from local directory for (FTPFile f : files) { if (f.getName() == "." || f.getName() == "..") { continue; } if (f.isFile()) { ftp.deleteFile(f.getName()); } } // Copy the files from local folder to server File localFolder = new File("src/tests/"); File[] localFiles = localFolder.listFiles(); int failed = 0; for (File f : localFiles) { if (f.getName() == "." || f.getName() == "..") { continue; } if (f.isFile()) { Debugger.println(f.getName()); File file = new File("src/tests/" + f.getName()); InputStream input = new FileInputStream(file); if (!ftp.storeFile(f.getName(), input)) failed++; input.close(); } } // If we failed to upload 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(); return; } } Alert alert = new Alert(AlertType.INFORMATION); alert.setTitle("Upload hotov"); alert.setHeaderText(null); alert.setContentText("spene sa podarilo skoprova vetky testy na server!"); alert.showAndWait(); alert.close(); }
From source file:br.com.tiagods.util.FTPUpload.java
public boolean uploadFile(File arquivo, String novoNome) { FTPConfig cf = FTPConfig.getInstance(); FTPClient ftp = new FTPClient(); try {//w w w . ja v a2 s . c o m ftp.connect(cf.getValue("host"), Integer.parseInt(cf.getValue("port"))); ftp.login(cf.getValue("user"), cf.getValue("pass")); //ftp.connect(server,port); //ftp.login(user,pass); ftp.enterLocalPassiveMode(); ftp.setFileType(FTP.BINARY_FILE_TYPE); boolean done; try (InputStream stream = new FileInputStream(arquivo)) { String remoteFile = novoNome; System.out.println("Start uploading first file"); done = ftp.storeFile(cf.getValue("dirFTP") + "/" + remoteFile, stream); stream.close(); } if (done) { JOptionPane.showMessageDialog(null, "Arquivo enviado com sucesso!"); return true; } return false; } catch (IOException e) { System.out.println("Erro = " + e.getMessage()); return false; } finally { try { if (ftp.isConnected()) { ftp.logout(); ftp.disconnect(); } } catch (IOException ex) { ex.printStackTrace(); } } }
From source file:edu.stanford.epad.common.util.FTPUtil.java
public void getFile(String remoteFile, File localFile) throws Exception { FTPClient ftpClient = new FTPClient(); OutputStream outputStream = null; try {/*w w w. jav a 2 s . c o m*/ ftpClient.connect(ftpHost, ftpPort); ftpClient.enterLocalPassiveMode(); if (ftpUser != null && ftpUser.length() > 0) ftpClient.login(ftpUser, ftpPassword); else ftpClient.login("anonymous", ""); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); outputStream = new BufferedOutputStream(new FileOutputStream(localFile)); InputStream inputStream = ftpClient.retrieveFileStream(remoteFile); IOUtils.copy(inputStream, outputStream); outputStream.flush(); IOUtils.closeQuietly(outputStream); IOUtils.closeQuietly(inputStream); boolean commandOK = ftpClient.completePendingCommand(); //boolean success = ftpClient.retrieveFile(remoteFile, outputStream); //if (!success) { // throw new Exception("Error retrieving remote file: " + remoteFile); //} } finally { outputStream.close(); ftpClient.disconnect(); } }
From source file:jenkins.plugins.publish_over_ftp.BapFtpHostConfiguration.java
private void setDataTransferMode(final FTPClient ftpClient) { if (useActiveData) { ftpClient.enterLocalActiveMode(); } else {/*from www . j av a2s . c o m*/ ftpClient.enterLocalPassiveMode(); } }
From source file:com.nostra13.universalimageloader.core.download.BaseImageDownloader.java
protected InputStream getStreamFromFTPNetwork(String imageUri, Object extra) throws IOException { int indexUrl = imageUri.indexOf("//") + 1; int indexUrlFinal = imageUri.lastIndexOf(":"); int slash = imageUri.lastIndexOf("/"); String ip = imageUri.substring(indexUrl + 1, indexUrlFinal); FTPClient client = new FTPClient(); client.connect(ip, 20000);/* w w w .j ava 2 s .co m*/ client.login("anonymous", ""); client.setFileType(FTP.BINARY_FILE_TYPE); client.enterLocalPassiveMode(); InputStream imageStream = null; FTPFile[] directories = client.listFiles(); long fileLength = (int) getFile(directories, imageUri.substring(slash + 1)).getSize(); try { imageStream = client.retrieveFileStream(imageUri.substring(slash + 1)); } catch (IOException e) { // Read all data to allow reuse connection (http://bit.ly/1ad35PY) IoUtils.readAndCloseStream(imageStream); throw e; } return new ContentLengthInputStream(new BufferedInputStream(imageStream, BUFFER_SIZE), fileLength); }