List of usage examples for org.apache.commons.net.ftp FTPClient isConnected
public boolean isConnected()
From source file:ca.efendi.datafeeds.messaging.FtpSubscriptionMessageListener.java
public void fetch(final FtpSubscription ftpSubscription) { if (_log.isDebugEnabled()) { _log.debug("fetching " + ftpSubscription); }/*w w w. j a va 2s . c om*/ final FTPClient ftp = new FTPClient(); ftp.setControlKeepAliveTimeout(30); ftp.setControlKeepAliveReplyTimeout(30); ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true)); try { int reply; ftp.connect(ftpSubscription.getFtpHost()); _log.debug("Connected to " + ftpSubscription.getFtpHost() + " on " + ftp.getDefaultPort()); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); System.err.println("FTP server refused connection."); System.exit(1); } } catch (final IOException e) { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (final IOException f) { // do nothing } } System.err.println("Could not connect to server."); e.printStackTrace(); System.exit(1); } boolean error = false; __main: try { if (!ftp.login(ftpSubscription.getFtpUser(), ftpSubscription.getFtpPassword())) { ftp.logout(); error = true; break __main; } _log.info("Remote system is " + ftp.getSystemType()); ftp.setFileType(FTP.BINARY_FILE_TYPE); //ftp.enterLocalActiveMode(); ftp.enterLocalPassiveMode(); //final FTPClientConfig config = new FTPClientConfig(); ////config.setLenientFutureDates(true); //ftp.configure(config); if (!StringUtils.isBlank(ftpSubscription.getFtpFolder())) { ftp.changeWorkingDirectory(ftpSubscription.getFtpFolder()); } final InputStream is = ftp.retrieveFileStream(ftpSubscription.getFtpFile()); if (is == null) { _log.error("FIle not found: " + ftp.getSystemType()); } else { unzip(ftpSubscription, is); is.close(); } ftp.completePendingCommand(); } catch (final FTPConnectionClosedException e) { error = true; System.err.println("Server closed connection."); e.printStackTrace(); } catch (final IOException e) { error = true; e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (final IOException e) { _log.error(e); } } } }
From source file:com.microsoft.azuretools.utils.WebAppUtils.java
public static void deployArtifact(String artifactName, String artifactPath, PublishingProfile pp, boolean toRoot, IProgressIndicator indicator) throws IOException { FTPClient ftp = null; InputStream input = null;//www. jav a2 s . c om try { if (indicator != null) indicator.setText("Connecting to FTP server..."); ftp = getFtpConnection(pp); if (indicator != null) indicator.setText("Uploading the application..."); input = new FileInputStream(artifactPath); if (toRoot) { WebAppUtils.removeFtpDirectory(ftp, ftpWebAppsPath + "ROOT", indicator); ftp.deleteFile(ftpWebAppsPath + "ROOT.war"); ftp.storeFile(ftpWebAppsPath + "ROOT.war", input); } else { WebAppUtils.removeFtpDirectory(ftp, ftpWebAppsPath + artifactName, indicator); ftp.deleteFile(artifactName + ".war"); boolean success = ftp.storeFile(ftpWebAppsPath + artifactName + ".war", input); if (!success) { int rc = ftp.getReplyCode(); throw new IOException("FTP client can't store the artifact, reply code: " + rc); } } if (indicator != null) indicator.setText("Logging out of FTP server..."); ftp.logout(); } finally { if (input != null) input.close(); if (ftp != null && ftp.isConnected()) { ftp.disconnect(); } } }
From source file:model.ProfilModel.java
public void uploadFoto(String path) { String server = "localhost"; int port = 21; String user = "user1"; String pass = "itsme"; FTPClient ftpClient = new FTPClient(); try {// w ww . ja va2s . com ftpClient.connect(server, port); ftpClient.login(user, pass); ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // APPROACH #1: uploads first file using an InputStream File firstLocalFile = new File(path); String firstRemoteFile = this.user.getFoto(); InputStream inputStream = new FileInputStream(firstLocalFile); System.out.println("Start uploading first file"); boolean done = ftpClient.storeFile(firstRemoteFile, inputStream); inputStream.close(); if (done) { System.out.println("The first file is uploaded successfully."); } inputStream.close(); } catch (IOException ex) { System.out.println("Error: " + ex.getMessage()); ex.printStackTrace(); } finally { try { if (ftpClient.isConnected()) { ftpClient.logout(); ftpClient.disconnect(); } } catch (IOException ex) { ex.printStackTrace(); } } }
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 {/*from ww w. j av 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:com.webarch.common.net.ftp.FtpService.java
/** * /*www . j a va 2s . co 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:cn.zhuqi.mavenssh.web.util.FTPClientTemplate.java
/** * ftp?/*from w w w . ja v a 2 s .co m*/ * * @param ftpClient * @return ?true?false * @throws Exception */ private boolean connect(FTPClient ftpClient) throws Exception { try { ftpClient.connect(host, port); // ????? int reply = ftpClient.getReplyCode(); if (FTPReply.isPositiveCompletion(reply)) { //ftp? if (ftpClient.login(username, password)) { setFileType(ftpClient); return true; } } else { ftpClient.disconnect(); throw new Exception("FTP server refused connection."); } } catch (IOException e) { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); // } catch (IOException e1) { throw new Exception("Could not disconnect from server.", e1); } } throw new Exception("Could not connect to server.", e); } return false; }
From source file:fr.bmartel.speedtest.SpeedTestTask.java
/** * logout & disconnect FTP client.//from w w w .j a v a2 s.c o m * * @param ftpclient ftp client */ private void disconnectFtp(final FTPClient ftpclient) { try { if (ftpclient.isConnected()) { ftpclient.logout(); ftpclient.disconnect(); } } catch (IOException ex) { //ex.printStackTrace(); } }
From source file:it.zero11.acme.example.FTPChallengeListener.java
private boolean createChallengeFiles(String token, String challengeBody) { boolean success = false; FTPClient ftp = new FTPClient(); try {//from w ww. ja v a 2 s. c o m ftp.connect(host); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { ftp.disconnect(); return false; } ftp.login(username, password); ftp.changeWorkingDirectory(webroot); ftp.makeDirectory(".well-known"); ftp.changeWorkingDirectory(".well-known"); ftp.makeDirectory("acme-challenge"); ftp.changeWorkingDirectory("acme-challenge"); ftp.enterLocalPassiveMode(); ftp.setFileType(FTPClient.BINARY_FILE_TYPE, FTPClient.BINARY_FILE_TYPE); ftp.setFileTransferMode(FTPClient.BINARY_FILE_TYPE); success = ftp.storeFile(token, new ByteArrayInputStream(challengeBody.getBytes())); if (!success) System.err.println("FTP error uploading file: " + ftp.getReplyCode() + ": " + ftp.getReplyString()); ftp.logout(); } catch (IOException e) { throw new AcmeException(e); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return success; }
From source file:conf.FTPConf.java
public String subirArchivo(File archivo) { FTPClient ftpClient = new FTPClient(); try {/* w w w. j a v a 2s . c om*/ ftpClient.connect(server, port); ftpClient.login(user, pass); ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // APPROACH #2: uploads second file using an OutputStream String serverFile = dir + archivo.getName(); InputStream inputStream = new FileInputStream(archivo); OutputStream outputStream = ftpClient.storeFileStream(serverFile); byte[] bytesIn = new byte[4096]; int read = 0; while ((read = inputStream.read(bytesIn)) != -1) { outputStream.write(bytesIn, 0, read); } inputStream.close(); outputStream.close(); boolean completed = ftpClient.completePendingCommand(); if (completed) { String link = "ftp://" + user + "@" + server + "/" + serverFile; return link; } } catch (IOException ex) { System.out.println("Error: " + ex.getMessage()); ex.printStackTrace(); } finally { try { if (ftpClient.isConnected()) { ftpClient.logout(); ftpClient.disconnect(); } } catch (IOException ex) { ex.printStackTrace(); } } return null; }
From source file:facturacion.ftp.FtpServer.java
public static InputStream getTokenInputStream(String remote_file_ruta) { FTPClient ftpClient = new FTPClient(); try {//from w w w . j a v a 2 s. c om //ftpClient.connect("127.0.0.1", 21 ); //ftpClient.login("erpftp", "Tribut@2014"); 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); 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; } else { System.out.println("se conecto al servidor"); } //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 rutaqq=" + "1-/" + remote_file_ruta); InputStream is = ftpClient.retrieveFileStream(remote_file_ruta); if (is == null) System.out.println("File #1 es null token"); else System.out.println("File #1 no es null token"); //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; }