List of usage examples for org.apache.commons.net.ftp FTPClient isConnected
public boolean isConnected()
From source file:cn.zhuqi.mavenssh.web.util.FTPClientTemplate.java
/** * ftp// w w w .ja v a 2s.co m * * @throws Exception */ public void disconnect() throws Exception { try { FTPClient ftpClient = getFTPClient(); ftpClient.logout(); if (ftpClient.isConnected()) { ftpClient.disconnect(); ftpClient = null; } } catch (IOException e) { throw new Exception("Could not disconnect from server.", e); } }
From source file:com.zxy.commons.net.ftp.FtpUtils.java
/** * FTP handle/*from w ww.ja va2 s.com*/ * * @param <T> return object type * @param ftpConfig ftp config * @param callback ftp callback * @return value */ public static <T> T ftpHandle(FtpConfig ftpConfig, FtpCallback<T> callback) { FTPClient client = null; if (ftpConfig.isFtps() && ftpConfig.getSslContext() != null) { client = new FTPSClient(ftpConfig.getSslContext()); } else if (ftpConfig.isFtps()) { client = new FTPSClient(); } else { client = new FTPClient(); } client.configure(ftpConfig.getFtpClientConfig()); try { // client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); client.connect(ftpConfig.getHost(), ftpConfig.getPort()); client.setConnectTimeout(ftpConfig.getConnectTimeoutMs()); client.setControlKeepAliveTimeout(ftpConfig.getKeepAliveTimeoutSeconds()); if (!Strings.isNullOrEmpty(ftpConfig.getUsername())) { client.login(ftpConfig.getUsername(), ftpConfig.getPassword()); } LOGGER.trace("Connected to {}, reply: {}", ftpConfig.getHost(), client.getReplyString()); // After connection attempt, you should check the reply code to verify success. int reply = client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { client.disconnect(); throw new NetException("FTP server refused connection."); } return callback.process(client); } catch (Exception e) { throw new NetException(e); } finally { if (client.isConnected()) { try { client.logout(); } catch (IOException ioe) { LOGGER.warn(ioe.getMessage()); } try { client.disconnect(); } catch (IOException ioe) { LOGGER.warn(ioe.getMessage()); } } } }
From source file:eu.ggnet.dwoss.misc.op.listings.FtpTransfer.java
/** * Uploads a some files to a remove ftp host. * <p/>//from w ww . j a va 2 s . c o m * @param config the config for he connection. * @param uploads the upload commands * @param monitor an optional monitor. * @throws java.net.SocketException * @throws java.io.IOException */ public static void upload(ConnectionConfig config, IMonitor monitor, UploadCommand... uploads) throws SocketException, IOException { if (uploads == null || uploads.length == 0) return; SubMonitor m = SubMonitor.convert(monitor, "FTP Transfer", toWorkSize(uploads) + 4); m.message("verbinde"); m.start(); FTPClient ftp = new FTPClient(); ftp.addProtocolCommandListener(PROTOCOL_TO_LOGGER); try { ftp.connect(config.getHost(), config.getPort()); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) throw new IOException("FTPReply.isPositiveCompletion(ftp.getReplyCode()) = false"); if (!ftp.login(config.getUser(), config.getPass())) throw new IOException("Login with " + config.getUser() + " not successful"); L.info("Connected to {} idenfied by {}", config.getHost(), ftp.getSystemType()); ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.enterLocalPassiveMode(); for (UploadCommand upload : uploads) { m.worked(1, "uploading to " + upload.getPath()); ftp.changeWorkingDirectory(upload.getPath()); deleteFilesByType(ftp, upload.getDeleteFileTypes()); for (File file : upload.getFiles()) { m.worked(1, "uploading to " + upload.getPath() + " file " + file.getName()); try (InputStream input = new FileInputStream(file)) { if (!ftp.storeFile(file.getName(), input)) throw new IOException("Cannot store file " + file + " on server!"); } } } m.finish(); } finally { // just cleaning up try { ftp.logout(); } catch (IOException e) { } if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException f) { } } } }
From source file:facturacion.ftp.FtpServer.java
public static InputStream getFileInputStream(String remote_file_ruta) { FTPClient ftpClient = new FTPClient(); try {// ww w. j a v a 2s .c o m ftpClient.connect(Config.getInstance().getProperty(Config.ServerFtpToken), Integer.parseInt(Config.getInstance().getProperty(Config.PortFtpToken))); ftpClient.login(Config.getInstance().getProperty(Config.UserFtpToken), Config.getInstance().getProperty(Config.PassFtpToken)); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); //ftpClient.enterLocalPassiveMode(); // crear directorio //OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(file_ruta)); //System.out.println("File #1 has been downloaded successfully. 1"); //FileInputStream fis = new FileInputStream("C:\\Users\\aaguerra\\Desktop\\firmado2.p12"); //InputStream is = fis; System.out.println("File ruta=" + "1/" + remote_file_ruta); InputStream is = ftpClient.retrieveFileStream(remote_file_ruta); if (is == null) System.out.println("File #1 es null"); else System.out.println("File #1 no es null"); //return ftpClient.retrieveFileStream(remote_file_ruta); return is; } catch (IOException ex) { System.out.println("File #1 has been downloaded successfully. 222"); } finally { try { if (ftpClient.isConnected()) { ftpClient.logout(); ftpClient.disconnect(); } } catch (IOException ex) { System.out.println("File #1 has been downloaded successfully. 3"); return null; //ex.printStackTrace(); } } return null; }
From source file:com.webarch.common.net.ftp.FtpService.java
/** * //from w ww. j av a2 s . c o m * * @param fileName ?? * @param path ftp? * @param fileStream ? * @return true/false ? */ public boolean uploadFile(String fileName, String path, InputStream fileStream) { 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(path); ftpClient.storeFile(fileName, fileStream); fileStream.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.buzz.buzzdata.MongoBuzz.java
private InputStream getFTPInputStream(String ftp_location) { InputStream retval = null;// w ww. ja va 2 s .co m String server = "162.219.245.61"; int port = 21; String user = "jelastic-ftp"; String pass = "HeZCHxeefB"; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); ftpClient.login(user, pass); ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE); retval = ftpClient.retrieveFileStream(ftp_location); } catch (IOException ex) { Logger.getLogger(MongoBuzz.class.getName()).log(Level.SEVERE, null, ex); } finally { try { if (ftpClient.isConnected()) { ftpClient.disconnect(); } } catch (IOException ex) { Logger.getLogger(MongoBuzz.class.getName()).log(Level.SEVERE, null, ex); } } return retval; }
From source file:com.intellij.diagnostic.SubmitPerformanceReportAction.java
@Nullable private static String uploadFileToFTP(final File reportPath, @NonNls final String ftpSite, @NonNls final String directory, final ProgressIndicator indicator) { FTPClient ftp = new FTPClient(); ftp.setConnectTimeout(30 * 1000);//w ww. j av a 2s . c o m try { indicator.setText("Connecting to server..."); ftp.connect(ftpSite); indicator.setText("Connected to server"); if (!ftp.login("anonymous", "anonymous@jetbrains.com")) { return "Failed to login"; } indicator.setText("Logged in"); // After connection attempt, you should check the reply code to verify // success. int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return "FTP server refused connection: " + reply; } if (!ftp.changeWorkingDirectory(directory)) { return "Failed to change directory"; } // else won't work behind FW ftp.enterLocalPassiveMode(); if (!ftp.setFileType(FTPClient.BINARY_FILE_TYPE)) { return "Failed to switch to binary mode"; } indicator.setText("Transferring (" + StringUtil.formatFileSize(reportPath.length()) + ")"); FileInputStream readStream = new FileInputStream(reportPath); try { if (!ftp.storeFile(reportPath.getName(), readStream)) { return "Failed to upload file"; } } catch (IOException e) { return "Error during transfer: " + e.getMessage(); } finally { readStream.close(); } ftp.logout(); return null; } catch (IOException e) { return "Failed to upload: " + e.getMessage(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { // do nothing } } } }
From source file:br.com.tiagods.util.FTPDownload.java
public boolean downloadFile(String arquivo) { FTPConfig cf = FTPConfig.getInstance(); FTPClient ftp = new FTPClient(); try {// www . j a va 2 s .co 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:facturacion.ftp.FtpServer.java
public static int sendImgFile(String fileNameServer, String hostDirServer, InputStream localFile) { FTPClient ftpClient = new FTPClient(); boolean success = false; BufferedInputStream buffIn = null; try {/* w w w. j av a 2 s .com*/ ftpClient.connect(Config.getInstance().getProperty(Config.ServerFtpToken), Integer.parseInt(Config.getInstance().getProperty(Config.PortFtpToken))); ftpClient.login(Config.getInstance().getProperty(Config.UserFtpToken), Config.getInstance().getProperty(Config.PassFtpToken)); ftpClient.enterLocalPassiveMode(); /*ftpClient.connect("127.0.0.1", 21); ftpClient.login("erpftp", "Tribut@2014");*/ ftpClient.setFileType(FTP.BINARY_FILE_TYPE); int reply = ftpClient.getReplyCode(); System.out.println("Respuesta recibida de conexin FTP:" + reply); if (!FTPReply.isPositiveCompletion(reply)) { System.out.println("Imposible conectarse al servidor"); return -1; } buffIn = new BufferedInputStream(localFile);//Ruta del archivo para enviar ftpClient.enterLocalPassiveMode(); //crear directorio success = ftpClient.makeDirectory(hostDirServer); System.out.println("sucess 1 = " + success); success = ftpClient.makeDirectory(hostDirServer + "/img"); System.out.println("sucess 233 = " + success); success = ftpClient.storeFile(hostDirServer + "/img/" + fileNameServer, buffIn); System.out.println("sucess 3 = " + success); } catch (IOException ex) { } finally { try { if (ftpClient.isConnected()) { buffIn.close(); //Cerrar envio de arcivos al FTP ftpClient.logout(); ftpClient.disconnect(); } } catch (IOException ex) { return -1; //ex.printStackTrace(); } } return (success) ? 1 : 0; }
From source file:com.eryansky.common.utils.ftp.FtpFactory.java
/** * ?FTP?./*ww w . j a v a 2 s .c o m*/ * * @param path * FTP?? * @param filename * FTP??? * @param input * ? * @return ?true?false */ public boolean ftpUploadFile(String path, String filename, InputStream input) { boolean success = false; FTPClient ftp = new FTPClient(); ftp.setControlEncoding("UTF-8"); try { int reply; ftp.connect(url, port); ftp.login(username, password); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return success; } // ftp.changeWorkingDirectory(path); ftp.setBufferSize(1024); ftp.setFileType(FTPClient.ASCII_FILE_TYPE); // ftp.storeFile(filename, input); input.close(); ftp.logout(); success = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } return success; }