List of usage examples for org.apache.commons.net.ftp FTPReply isPositiveCompletion
public static boolean isPositiveCompletion(int reply)
From source file:net.sf.jfilesync.plugins.net.items.TCommonsFTP_plugin.java
public void connect(TConnectionData connectData) throws PluginConnectException { this.conData = connectData; String username = conData.getUser(); String hostname = conData.getHost(); String passwd = conData.getPassword(); int port = conData.getPort(); LOGGER.info("trying to connect to :" + hostname); try {// w w w . j a v a 2 s. com if (connectData.getEncoding() != null) { LOGGER.info("set ftp control encoding : " + connectData.getEncoding()); ftpClient.setControlEncoding(connectData.getEncoding()); } if (port != -1 && port != getDefaultPort()) { ftpClient.connect(hostname, port); } else { ftpClient.connect(hostname); } ftpClient.enterLocalPassiveMode(); } catch (IOException ioex) { ioex.printStackTrace(); throw new PluginConnectException(TErrorHandling.ERROR_CONNECTION_FAILURE, ioex.getMessage()); } int reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { try { if (ftpClient.isConnected()) { ftpClient.disconnect(); } } catch (IOException e) { throw new PluginConnectException(TErrorHandling.ERROR_CONNECTION_FAILURE, e.getMessage()); } } else { boolean authOK = false; try { authOK = ftpClient.login(username, passwd); if (!authOK) { ftpClient.disconnect(); throw new PluginConnectException(TErrorHandling.ERROR_CONNECTION_AUTHENTICATION, LanguageBundle.getInstance().getMessage("plugin.connect.auth_failed")); } } catch (IOException ioex) { throw new PluginConnectException(TErrorHandling.ERROR_CONNECTION_AUTHENTICATION, ioex.getMessage()); } // connected = true; LOGGER.info("ftp connect done"); } }
From source file:cn.zhuqi.mavenssh.web.util.FTPClientTemplate.java
/** * ftp?//from w ww. jav a2s.c om * * @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:com.usefullc.platform.common.utils.FtpUtils.java
/** * /*w w w.ja v a2s.c om*/ * * @param oppositePath * @param downFileName ?? * @return */ public static InputStream delete(String oppositePath, String fileName) { InputStream is = null; try { // // ftpClient.connect(host, port); // ftpClient.login(user, password); // ?? int reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { log.error("FTP server refused connection,host=" + host + ",user=" + user + "," + password); return is; } if (fileName != null) { String sep = "/"; String deleteFileName = StrUtils.joinEmpty(remoteDir, sep, oppositePath, sep, fileName); ftpClient.deleteFile(deleteFileName); } ftpClient.logout(); } catch (Exception e) { e.printStackTrace(); } finally { try { ftpClient.disconnect(); } catch (IOException ioe) { ioe.printStackTrace(); } } return is; }
From source file:de.thischwa.pmcms.tool.connection.ftp.FtpConnectionManager.java
private void checkReply() { int replyCode = ftpClient.getReplyCode(); // close connection, if dropped, so that isConnected() return false if (replyCode == FTPReply.SERVICE_NOT_AVAILABLE) close();//w w w . java 2 s .co m logger.debug("[FTP] " + ftpClient.getReplyString()); // throw exceptions depending on the replyCode, if is not positiv if (!FTPReply.isPositiveCompletion(replyCode)) { if (replyCode == FTPReply.CODE_503 || replyCode == FTPReply.NEED_PASSWORD || replyCode == FTPReply.NOT_LOGGED_IN) throw new ConnectionAuthentificationException(ftpClient.getReplyString()); else throw new ConnectionRunningException(ftpClient.getReplyString()); } }
From source file:com.cisco.dvbu.ps.utils.net.FtpFile.java
public void ftpFile(String fileName) throws CustomProcedureException, SQLException { // new ftp client FTPClient ftp = new FTPClient(); OutputStream output = null;/*from www . j a va 2 s . co m*/ success = false; try { //try to connect ftp.connect(hostIp); //login to server if (!ftp.login(userId, userPass)) { ftp.logout(); qenv.log(LOG_ERROR, "Ftp server refused connection user/password incorrect."); } int reply = ftp.getReplyCode(); //FTPReply stores a set of constants for FTP Reply codes if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); qenv.log(LOG_ERROR, "Ftp server refused connection."); } //enter passive mode ftp.setFileType(FTPClient.BINARY_FILE_TYPE, FTPClient.BINARY_FILE_TYPE); ftp.setFileTransferMode(FTPClient.BINARY_FILE_TYPE); ftp.enterLocalPassiveMode(); //get system name //System.out.println("Remote system is " + ftp.getSystemType()); //change current directory ftp.changeWorkingDirectory(ftpDirName); System.out.println("Current directory is " + ftp.printWorkingDirectory()); System.out.println("File is " + fileName); output = new FileOutputStream(dirName + "/" + fileName); //get the file from the remote system success = ftp.retrieveFile(fileName, output); //close output stream output.close(); } catch (IOException ex) { throw new CustomProcedureException("Error in CJP " + getName() + ": " + ex.toString()); } }
From source file:fr.chaffottem.bonita.connector.ftp.FTPClientConnector.java
@Override protected void executeBusinessLogic() throws ConnectorException { try {// ww w .j a v a 2 s .com boolean login = true; final String userName = (String) getInputParameter(USER_NAME); if (userName != null) { final String password = (String) getInputParameter(PASSWORD); login = ftpClient.login(userName, password); } if (login) { final int reply = ftpClient.getReplyCode(); if (FTPReply.isPositiveCompletion(reply)) { configureClient(); executeFTPTask(); } } else { throw new ConnectorException("Login fails due to a wrong user name/password"); } } catch (final IOException ioe) { throw new ConnectorException(ioe); } }
From source file:com.mirth.connect.connectors.file.filesystems.FtpConnection.java
@Override public List<FileInfo> listFiles(String fromDir, String filenamePattern, boolean isRegex, boolean ignoreDot) throws Exception { FilenameFilter filenameFilter; if (isRegex) { filenameFilter = new RegexFilenameFilter(filenamePattern); } else {//www .jav a2 s . co m filenameFilter = new WildcardFileFilter(filenamePattern.trim().split("\\s*,\\s*")); } if (!cwd(fromDir)) { logger.error( "listFiles.changeWorkingDirectory: " + client.getReplyCode() + "-" + client.getReplyString()); throw new IOException("Ftp error: " + client.getReplyCode()); } FTPFile[] files = client.listFiles(); if (!FTPReply.isPositiveCompletion(client.getReplyCode())) { logger.error("listFiles.listFiles: " + client.getReplyCode() + "-" + client.getReplyString()); throw new IOException("Ftp error: " + client.getReplyCode()); } if (files == null || files.length == 0) { return new ArrayList<FileInfo>(); } List<FileInfo> v = new ArrayList<FileInfo>(files.length); for (int i = 0; i < files.length; i++) { if ((files[i] != null) && files[i].isFile()) { if ((filenameFilter == null || filenameFilter.accept(null, files[i].getName())) && !(ignoreDot && files[i].getName().startsWith("."))) { v.add(new FtpFileInfo(fromDir, files[i])); } } } return v; }
From source file:net.sf.ufsc.ftp.FTPSClient.java
@SuppressWarnings("nls") public int ccc() throws IOException { int reply = this.sendCommand("CCC"); if (FTPReply.isPositiveCompletion(reply)) { this._controlInput_ = new BufferedReader( new InputStreamReader(this.clearSocket.getInputStream(), this.getControlEncoding())); this._controlOutput_ = new BufferedWriter( new OutputStreamWriter(this.clearSocket.getOutputStream(), this.getControlEncoding())); }//from w w w. j ava 2s . com return reply; }
From source file:ca.nrc.cadc.web.ServerToServerFTPTransfer.java
private FTPClient connect() throws IOException { final FTPClient ftpClient = createFTPClient(); ftpClient.connect(hostname, port);/*from w w w . j a va 2s . c o m*/ final int reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftpClient.disconnect(); throw new IOException(String.format("FTP Connection failed with error code %d.", reply)); } else { return login(ftpClient); } }
From source file:at.beris.virtualfile.client.ftp.FtpClient.java
@Override public void createDirectory(final String path) throws IOException { LOGGER.debug("createDirectory (path : {})", path); executionHandler(new Callable<Void>() { @Override/*from w w w . j a v a2s . c o m*/ public Void call() throws Exception { int replyCode = ftpClient.mkd(path); String replyText = ftpClient.getReplyString(); if (!FTPReply.isPositiveCompletion(replyCode)) { // this replyText code is set when file already exists on the server if (FTPReply.FILE_UNAVAILABLE == replyCode) throw new FileAlreadyExistsException(path); else LOGGER.warn("Unexpected Reply (Code: {}, Text: '{}'", replyCode, replyText); } return null; } }); }