List of usage examples for org.apache.commons.net.ftp FTPReply isPositiveCompletion
public static boolean isPositiveCompletion(int reply)
From source file:EscribirCorreo.java
private void SubirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SubirActionPerformed //Codigo para subir archivo String localfile = subir1Field.getText(); String server = "51.254.137.26"; String username = "proyecto"; String password = "proyecto"; String destinationfile = nombre; try {/* w ww . j a va2 s. co m*/ FTPClient ftp = new FTPClient(); ftp.connect(server); if (!ftp.login(username, password)) { ftp.logout(); } int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); } InputStream in = new FileInputStream(localfile); ftp.setFileType(ftp.BINARY_FILE_TYPE); ftp.storeFile(destinationfile, in); JOptionPane.showMessageDialog(null, "Archivo subido correctamente", "Subido!", JOptionPane.INFORMATION_MESSAGE); if (archivos == null) { archivos = "http://51.254.137.26/proyecto/" + destinationfile; } else { archivos = archivos + "#http://51.254.137.26/proyecto/" + destinationfile; } in.close(); ftp.logout(); ftp.disconnect(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:com.cloudhopper.commons.rfs.provider.FtpRemoteFileSystem.java
public boolean exists(String filename) throws FileSystemException { // we have to be connected if (ftp == null) { throw new FileSystemException("Not yet connected to FTP server"); }/* ww w .j ava 2 s.co m*/ try { // check if the file already exists FTPFile[] files = ftp.listFiles(filename); // did this command succeed? if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new FileSystemException( "FTP server failed to get file listing (reply=" + ftp.getReplyString() + ")"); } if (files != null && files.length > 0) { // this file already exists return true; } else { return false; } } catch (IOException e) { throw new FileSystemException("Underlying IO exception with FTP server while checking if file exists", e); } }
From source file:edu.stanford.epad.common.util.FTPUtil.java
public boolean sendFile(String filePath, boolean delete) throws Exception { String fileName = filePath;/*from w ww .j av a2 s . c om*/ int slash = fileName.lastIndexOf("/"); if (slash != -1) fileName = fileName.substring(slash + 1); slash = fileName.lastIndexOf("\\"); if (slash != -1) fileName = fileName.substring(slash + 1); boolean success = true; FTPClient ftp = new FTPClient(); try { ftp.connect(ftpHost, ftpPort); int reply = ftp.getReplyCode(); if (FTPReply.isPositiveCompletion(reply)) { if (ftp.login(ftpUser, ftpPassword)) { if (ftpFolder != null && ftpFolder.trim().length() > 0) { ftp.cwd(ftpFolder); System.out.print("ftp cd: " + ftp.getReplyString()); } ftp.setFileType(FTP.ASCII_FILE_TYPE); FileInputStream in = new FileInputStream(filePath); success = ftp.storeFile(fileName, in); in.close(); if (delete) { File file = new File(filePath); file.delete(); } } else success = false; } } finally { ftp.disconnect(); } return success; }
From source file:com.naryx.tagfusion.cfm.tag.net.ftp.cfFTPData.java
public String cmd(String cmd) { if (!ftpclient.isConnected()) { errorText = "not connected"; succeeded = false;/*from w w w .j a va 2s .co m*/ return ""; } String params = ""; if (cmd.indexOf(" ") != -1) { params = cmd.substring(cmd.indexOf(" ") + 1); cmd = cmd.substring(0, cmd.indexOf(" ")); } String bResult = ""; try { ftpclient.doCommand(cmd, params); bResult = ftpclient.getReplyString(); errorCode = ftpclient.getReplyCode(); succeeded = FTPReply.isPositiveCompletion(errorCode); } catch (Exception e) { errorCode = ftpclient.getReplyCode(); errorText = e.getMessage(); } finally { setStatusData(); } return bResult; }
From source file:ch.sdi.core.impl.ftp.FTPClientExample.java
/** * *///ww w .j a v a 2 s.c o m private void run() throws Throwable { if (myProtocol == null) { if (myProxyHost != null) { myLog.debug("Using HTTP proxy server: " + myProxyHost); myFtp = new FTPHTTPClient(myProxyHost, myProxyPort, myProxyUser, myProxyPassword); } else { myFtp = new FTPClient(); } } else { FTPSClient ftps; if (myProtocol.equals("true")) { ftps = new FTPSClient(true); } else if (myProtocol.equals("false")) { ftps = new FTPSClient(false); } else { String prot[] = myProtocol.split(","); if (prot.length == 1) { // Just protocol ftps = new FTPSClient(myProtocol); } else { // protocol,true|false ftps = new FTPSClient(prot[0], Boolean.parseBoolean(prot[1])); } } myFtp = ftps; if ("all".equals(myTrustmgr)) { ftps.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager()); } else if ("valid".equals(myTrustmgr)) { ftps.setTrustManager(TrustManagerUtils.getValidateServerCertificateTrustManager()); } else if ("none".equals(myTrustmgr)) { ftps.setTrustManager(null); } } if (myPrintHash) { myFtp.setCopyStreamListener(createListener()); } if (myKeepAliveTimeout >= 0) { myFtp.setControlKeepAliveTimeout(myKeepAliveTimeout); } if (myControlKeepAliveReplyTimeout >= 0) { myFtp.setControlKeepAliveReplyTimeout(myControlKeepAliveReplyTimeout); } myFtp.setListHiddenFiles(myHidden); // intercept commands and write it to our logger myPrintCommandToLoggerListener = new PrintCommandToLoggerListener(myLog); PrintWriter writer = myPrintCommandToLoggerListener.getPrintWriter(); myFtp.addProtocolCommandListener(new PrintCommandListener(writer, true, '\n', true)); // myFtp.addProtocolCommandListener( new PrintCommandListener( new PrintWriter( System.out ), true ) ); try { int reply; if (myPort > 0) { myFtp.connect(myServer, myPort); } else { myFtp.connect(myServer); } myLog.debug("Connected to " + myServer + " on " + (myPort > 0 ? myPort : myFtp.getDefaultPort())); // After connection attempt, you should check the reply code to verify // success. reply = myFtp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { myFtp.disconnect(); throw new Exception("FTP server refused connection."); } } catch (IOException e) { if (myFtp.isConnected()) { try { myFtp.disconnect(); } catch (IOException f) { // do nothing } } throw new Exception("Could not connect to server.", e); } try { if (!myFtp.login(myUsername, myPassword)) { myFtp.logout(); throw createFtpException("Problems on login"); } myLog.debug("Remote system is " + myFtp.getSystemType()); if (myBinaryTransfer) { myFtp.setFileType(FTP.BINARY_FILE_TYPE); } else { // in theory this should not be necessary as servers should default to ASCII // but they don't all do so - see NET-500 myFtp.setFileType(FTP.ASCII_FILE_TYPE); } // Use passive mode as default because most of us are // behind firewalls these days. if (myLocalActive) { myFtp.enterLocalActiveMode(); } else { myFtp.enterLocalPassiveMode(); } myFtp.setUseEPSVwithIPv4(myUseEpsvWithIPv4); if (myStoreFile) { InputStream input; input = new FileInputStream(myLocal); myFtp.storeFile(myRemote, input); input.close(); } else if (myListFiles) { if (myLenient) { FTPClientConfig config = new FTPClientConfig(); config.setLenientFutureDates(true); myFtp.configure(config); } for (FTPFile f : myFtp.listFiles(myRemote)) { myLog.debug(f.getRawListing()); myLog.debug(f.toFormattedString()); } } else if (myMlsd) { for (FTPFile f : myFtp.mlistDir(myRemote)) { myLog.debug(f.getRawListing()); myLog.debug(f.toFormattedString()); } } else if (myMlst) { FTPFile f = myFtp.mlistFile(myRemote); if (f != null) { myLog.debug(f.toFormattedString()); } } else if (myListNames) { for (String s : myFtp.listNames(myRemote)) { myLog.debug(s); } } else if (myFeat) { // boolean feature check if (myRemote != null) { // See if the command is present if (myFtp.hasFeature(myRemote)) { myLog.debug("Has feature: " + myRemote); } else { if (FTPReply.isPositiveCompletion(myFtp.getReplyCode())) { myLog.debug("FEAT " + myRemote + " was not detected"); } else { throw createFtpException("Command failed"); } } // Strings feature check String[] features = myFtp.featureValues(myRemote); if (features != null) { for (String f : features) { myLog.debug("FEAT " + myRemote + "=" + f + "."); } } else { if (FTPReply.isPositiveCompletion(myFtp.getReplyCode())) { myLog.warn("FEAT " + myRemote + " is not present"); } else { throw createFtpException("Command failed"); } } } else { if (myFtp.features()) { // Command listener has already printed the output } else { throw createFtpException("Command failed"); } } } else if (myDoCommand != null) { if (myFtp.doCommand(myDoCommand, myRemote)) { // Command listener has already printed the output } else { throw createFtpException("Command failed"); } } else { OutputStream output; output = new FileOutputStream(myLocal); myFtp.retrieveFile(myRemote, output); output.close(); } myFtp.noop(); // check that control connection is working OK myFtp.logout(); } catch (FTPConnectionClosedException e) { throw createFtpException("Server closed connection."); } catch (IOException e) { throw createFtpException("IOException caught"); } finally { myPrintCommandToLoggerListener.flushRest(); if (myFtp.isConnected()) { try { myFtp.disconnect(); } catch (IOException f) { // do nothing } } } }
From source file:ch.cyberduck.core.ftp.FTPClient.java
/** * http://drftpd.org/index.php/PRET_Specifications * * @param command Command to execute/* w w w . j av a2s.c o m*/ * @param file Remote file * @throws IOException I/O failure */ protected void pret(final FTPCmd command, final String file) throws IOException { if (this.hasFeature("PRET")) { if (!FTPReply.isPositiveCompletion( this.sendCommand("PRET", String.format("%s %s", command.getCommand(), file)))) { throw new FTPException(this.getReplyCode(), this.getReplyString()); } } }
From source file:it.greenvulcano.util.remotefs.ftp.FTPManager.java
/** * @see it.greenvulcano.util.remotefs.RemoteManager#get(String, String, * outputDataStream, java.util.Map) *///from ww w . jav a 2 s .c o m @Override public boolean get(String remoteDirectory, String remoteFile, OutputStream outputStream, Map<String, String> optProperties) throws RemoteManagerException { checkConnected(); boolean result = false; try { logger.debug("Downloading remote file " + remoteFile + " from " + (remoteDirectory != null ? " remote directory " + remoteDirectory : " current remote working directory") + "..."); if (remoteDirectory != null) { changeWorkingDirectory(remoteDirectory); } ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.retrieveFile(remoteFile, outputStream); int reply = ftpClient.getReplyCode(); if (FTPReply.isPositiveCompletion(reply)) { logger.debug("Remote file " + remoteFile + " saved to OutputStream"); result = true; } else { logger.warn("FTP Server NEGATIVE response: "); logServerReply(Level.WARN); } return result; } catch (IOException exc) { throw new RemoteManagerException("I/O error", exc); } catch (Exception exc) { throw new RemoteManagerException("Generic error", exc); } finally { if (isAutoconnect()) { disconnect(); } } }
From source file:ch.sdi.core.impl.ftp.FtpExecutor.java
/** * @throws SdiException/*from w w w . java 2 s. co m*/ * @throws IOException */ public void connectAndLogin() throws SdiException, IOException { if (!myInitialized) { initBySpringContext(); } // if !initialized try { if (myProtocol == null) { if (myProxyHost != null) { myLog.debug("Using HTTP proxy server: " + myProxyHost); myFtp = new FTPHTTPClient(myProxyHost, myProxyPort, myProxyUser, myProxyPassword); } else { myLog.debug("Using simple FTPClient"); myFtp = new FTPClient(); } } else { FTPSClient ftps; if (myProtocol.equals("true")) { myLog.debug("Using FTPSClient with implicite SSL"); ftps = new FTPSClient(true); } else if (myProtocol.equals("false")) { myLog.debug("Using FTPSClient with no implicite SSL"); ftps = new FTPSClient(false); } else { String prot[] = myProtocol.split(","); if (prot.length == 1) { // Just protocol myLog.debug("Using FTPSClient with protocol " + myProtocol); ftps = new FTPSClient(myProtocol); } else { // protocol,true|false myLog.debug("Using FTPSClient with " + prot[0] + " and " + prot[1]); ftps = new FTPSClient(prot[0], Boolean.parseBoolean(prot[1])); } } myFtp = ftps; if ("all".equals(myTrustmgr)) { myLog.debug("Using AcceptAllTrustManager"); ftps.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager()); } else if ("valid".equals(myTrustmgr)) { myLog.debug("Using ValidateServerCertificateTrustManager"); ftps.setTrustManager(TrustManagerUtils.getValidateServerCertificateTrustManager()); } else if ("none".equals(myTrustmgr)) { myLog.debug("Setting TrustManager to null"); ftps.setTrustManager(null); } else { myLog.debug("Using no TrustManager at all"); } } if (myKeepAliveTimeout >= 0) { myLog.debug("Setting KeepAliveTimeout to " + myKeepAliveTimeout); myFtp.setControlKeepAliveTimeout(myKeepAliveTimeout); } if (myControlKeepAliveReplyTimeout >= 0) { myLog.debug("Setting ControlKeepAliveReplyTimeout to " + myControlKeepAliveReplyTimeout); myFtp.setControlKeepAliveReplyTimeout(myControlKeepAliveReplyTimeout); } // intercept commands and write it to our logger myPrintCommandToLoggerListener = new PrintCommandToLoggerListener(myLog); PrintWriter writer = myPrintCommandToLoggerListener.getPrintWriter(); myFtp.addProtocolCommandListener(new PrintCommandListener(writer, true, '\n', true)); // myFtp.addProtocolCommandListener( new PrintCommandListener( new PrintWriter( System.out ), true ) ); try { int reply; if (myPort > 0) { myLog.debug("Connecting to " + myServer + ":" + myPort); myFtp.connect(myServer, myPort); } else { myLog.debug("Connecting to " + myServer + " (default port)"); myFtp.connect(myServer); } myLog.debug("Connected to " + myServer + " on " + (myPort > 0 ? myPort : myFtp.getDefaultPort())); // After connection attempt, you should check the reply code to verify success. reply = myFtp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { myFtp.disconnect(); throw createFtpException("FTP server refused connection."); } } catch (IOException e) { throw createFtpException("Could not connect to server.", e); } if (!myFtp.login(myUsername, myPassword)) { myFtp.logout(); throw createFtpException("Problems on login"); } myLog.debug("Remote system is " + myFtp.getSystemType()); myFtp.setFileType(FTP.BINARY_FILE_TYPE); // Use passive mode as default because most of us are behind firewalls these days. if (myLocalActive) { myFtp.enterLocalActiveMode(); } else { myFtp.enterLocalPassiveMode(); } myFtp.setUseEPSVwithIPv4(myUseEpsvWithIPv4); } finally { myPrintCommandToLoggerListener.flushRest(); } }
From source file:de.climbingguide.erzgebirsgrenzgebiet.downloader.DownloaderThread.java
public boolean ftpConnect(String host, String username, String password, int port) { try {/*from w ww. j a va 2 s. c o m*/ mFTPClient = new FTPClient(); // connecting to the host mFTPClient.connect(host, port); // now check the reply code, if positive mean connection success if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) { // login using username & password boolean status = mFTPClient.login(username, password); /* Set File Transfer Mode * * To avoid corruption issue you must specified a correct * transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE, * EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE * for transferring text, image, and compressed files. */ mFTPClient.setFileType(FTP.BINARY_FILE_TYPE); mFTPClient.enterLocalPassiveMode(); return status; } } catch (Exception e) { String errMsg = parentActivity.getString(R.string.error_message_general); Message msg = Message.obtain(activityHandler, KleFuEntry.MESSAGE_ENCOUNTERED_ERROR_FTP_CONNECT, 0, 0, errMsg); activityHandler.sendMessage(msg); } return false; }
From source file:com.aliasi.lingmed.medline.DownloadMedline.java
private void checkFtpCompletion(String description, boolean exceptionIfNotComplete) throws IOException { int replyCode = mFTPClient.getReplyCode(); // mLogger.debug("Ftp completion code: "+replyCode); if (FTPReply.isPositiveCompletion(replyCode)) return;/* w w w .ja v a 2 s.c o m*/ if (exceptionIfNotComplete) { raiseIOException(description); } }