List of usage examples for org.apache.commons.net.ftp FTPReply isPositiveCompletion
public static boolean isPositiveCompletion(int reply)
From source file:org.catrobat.catroid.web.ConnectionWrapper.java
public String doFtpPostFileUpload(String urlString, HashMap<String, String> postValues, String fileTag, String filePath, ResultReceiver receiver, String httpPostUrl, Integer notificationId) throws IOException, WebconnectionException { String answer = ""; try {/*from w w w . j a v a 2 s . com*/ // important to call this before connect ftpClient.setControlEncoding("UTF-8"); ftpClient.connect(urlString, ServerCalls.FTP_PORT); ftpClient.login(FTP_USERNAME, FTP_PASSWORD); int replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) { ftpClient.disconnect(); Log.e(TAG, "FTP server refused to connect"); throw new WebconnectionException(replyCode); } ftpClient.setFileType(FILE_TYPE); BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(filePath)); ftpClient.enterLocalPassiveMode(); String fileName = ""; if (filePath != null) { fileName = postValues.get("projectTitle"); String extension = filePath.substring(filePath.lastIndexOf(".") + 1).toLowerCase(Locale.ENGLISH); FtpProgressInputStream ftpProgressStream = new FtpProgressInputStream(inputStream, receiver, notificationId, fileName); boolean result = ftpClient.storeFile(fileName + "." + extension, ftpProgressStream); if (!result) { throw new IOException(); } } inputStream.close(); ftpClient.logout(); ftpClient.disconnect(); answer = sendUploadPost(httpPostUrl, postValues, fileTag, filePath); } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } return answer; }
From source file:org.covito.kit.file.support.FtpFileServiceImpl.java
/** * ?FTP Client/*from w ww .j a v a2 s. co m*/ * <p>??</p> * * @author covito */ protected void initFTPClient() { ftp = new FTPClient(); ftp.setControlEncoding("UTF-8"); try { ftp.connect(url, port); if (isPassiveMode) { ftp.enterLocalPassiveMode(); } ftp.login(username, password); int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); ftp = null; throw new FileServiceException("FTP ?"); } ftp.setFileType(FTPClient.BINARY_FILE_TYPE); } catch (Exception e) { e.printStackTrace(); throw new FileServiceException(e); } }
From source file:org.drftpd.tests.ConnectionStressTest.java
public void run() { try {/* ww w .j ava 2s . co m*/ FTPClient c = new FTPClient(); c.configure(ftpConfig); logger.debug("Trying to connect"); c.connect("127.0.0.1", 21211); logger.debug("Connected"); c.setSoTimeout(5000); if (!FTPReply.isPositiveCompletion(c.getReplyCode())) { logger.debug("Houston, we have a problem. D/C"); c.disconnect(); throw new Exception(); } if (c.login("drftpd", "drftpd")) { logger.debug("Logged-in, now waiting 5 secs and kill the thread."); _sc.addSuccess(); Thread.sleep(5000); c.disconnect(); } else { logger.debug("Login failed, D/C!"); throw new Exception(); } } catch (Exception e) { logger.debug(e, e); _sc.addFailure(); } logger.debug("exiting"); }
From source file:org.drools.process.workitem.ftp.FTPUploadWorkItemHandler.java
public void executeWorkItem(WorkItem workItem, WorkItemManager manager) { this.user = (String) workItem.getParameter("User"); this.password = (String) workItem.getParameter("Password"); this.filePath = (String) workItem.getParameter("FilePath"); client = new FTPClient(); try {/*w ww . ja v a 2 s . c o m*/ if (connection != null) { client.connect(connection.getHost(), Integer.parseInt(connection.getPort())); int reply = client.getReplyCode(); if (FTPReply.isPositiveCompletion(reply)) { if (client.login(user, password)) { InputStream input; input = new FileInputStream(filePath); client.setFileType(FTP.BINARY_FILE_TYPE); this.setResult(client.storeFile(filePath, input)); client.logout(); } } } } catch (SocketException ex) { Logger.getLogger(FTPUploadWorkItemHandler.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(FTPUploadWorkItemHandler.class.getName()).log(Level.SEVERE, null, ex); } manager.completeWorkItem(workItem.getId(), null); }
From source file:org.easysnap.util.ftp.FtpClient.java
/** * TODO: dependency injection config//from w w w. j a v a 2 s. c o m */ public boolean upload(File file, String fileName) { FTPClient client = new FTPClient(); FileInputStream fis = null; boolean isUploaded = false; try { client.connect(config.getFtpServer()); int reply = client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { client.disconnect(); System.err.println("FTP: server refused connection."); return false; } if (!client.login(config.getFtpUser(), config.getFtpPassword())) { client.logout(); client.disconnect(); System.err.println("FTP: Login failed."); return false; } client.enterRemotePassiveMode(); client.enterLocalPassiveMode(); client.setFileType(FTP.BINARY_FILE_TYPE); fis = new FileInputStream(file); isUploaded = client.storeFile(getUploadPath(fileName), fis); client.logout(); } catch (IOException e) { System.err.println("FTP: IOException"); System.err.println(e.getLocalizedMessage()); return false; } finally { try { if (fis != null) { fis.close(); } client.disconnect(); } catch (IOException e) { e.printStackTrace(); } } return isUploaded; }
From source file:org.easysnap.util.ftp.FtpClient.java
public boolean canConnect() { try {//from w ww . j a v a 2s. co m FTPClient client = new FTPClient(); client.connect(config.getFtpServer()); int reply = client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { client.disconnect(); System.err.println("FTP: server refused connection."); return false; } if (!client.login(config.getFtpUser(), config.getFtpPassword())) { client.logout(); client.disconnect(); System.err.println("FTP: Login failed."); return false; } client.enterRemotePassiveMode(); client.enterLocalPassiveMode(); client.setFileType(FTP.BINARY_FILE_TYPE); client.logout(); return true; } catch (IOException e) { System.err.println("FTP: IOException"); System.err.println(e.getLocalizedMessage()); } return false; }
From source file:org.eclipse.datatools.connectivity.sample.ftp.internal.FtpConnection.java
/** * Constructor//from www .j av a 2s. c o m * @param profile */ public FtpConnection(IConnectionProfile profile) { this.mProfile = profile; Properties props = profile.getBaseProperties(); String server = props.getProperty(IFtpProfileConstants.FTP_SERVER); String port = props.getProperty(IFtpProfileConstants.FTP_PORT); String user = props.getProperty(IFtpProfileConstants.FTP_UID); String pass = props.getProperty(IFtpProfileConstants.FTP_PWD); try { int reply; this.mFtpClient = new FTPClient(); this.mFtpClientObject = new FTPClientObject(profile, this.mFtpClient); if (port != null && port.length() != 0) this.mFtpClient.setDefaultPort(new Integer(port).intValue()); this.mFtpClient.setDefaultTimeout(2 * 60 * 1000); this.mFtpClient.setDataTimeout(2 * 60 * 1000); this.mFtpClient.connect(server); if (!this.mFtpClient.login(user, pass)) { throw new Exception(mFtpClient.getReplyString()); } reply = this.mFtpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { this.mFtpClient.disconnect(); throw new Exception(FTPProfileMessages.getString("FtpConnection.errormessage")); //$NON-NLS-1$ } } catch (Exception e) { this.mException = e; return; } this.mFtpClient.enterLocalPassiveMode(); FtpConnection.counter++; }
From source file:org.esa.nest.util.ftpUtils.java
private void connect() throws IOException { ftpClient.setRemoteVerificationEnabled(false); ftpClient.connect(server);//from w ww .ja v a2 s .c o m int reply = ftpClient.getReplyCode(); if (FTPReply.isPositiveCompletion(reply)) ftpClientConnected = ftpClient.login(user, password); if (!ftpClientConnected) { disconnect(); throw new IOException("Unable to connect to " + server); } else { ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.setDataTimeout(60000); } }
From source file:org.eurocarbdb.util.FTP_Client.java
/***************************************** *// ww w . jav a 2 s . co m * A convenience method for connecting and logging in. */ public void connectAndLogin(String host, String username, String password) throws IOException, UnknownHostException, FTPConnectionClosedException { this.host = host; this.userName = username; log.debug("attempting to connect to " + host); connect(host); if (FTPReply.isPositiveCompletion(getReplyCode())) { log.debug("connect successful, logging in as " + userName); login(userName, password); } else { log.warn("failed to connect to " + host + ", disconnecting..."); disconnect(); return; } log.info("connected to " + host + " as user " + userName); }
From source file:org.fao.fenix.ftp.FTPTask.java
public boolean ftpConnect() { try {/*ww w. j a va 2 s. com*/ if (getFtpClient() != null) { //try to connect getFtpClient().connect(serverAddress); //login to server if (!getFtpClient().login(username, password)) //if(!getFtpClient().login(user, "")) { getFtpClient().logout(); return false; } else { System.out.println("=== amis-ftp === FTPTask: ftpConnect: == INFO == Logged in ! for " + username + " to " + serverAddress); } int reply = getFtpClient().getReplyCode(); //FTPReply stores a set of constants for FTP reply codes. if (!FTPReply.isPositiveCompletion(reply)) { getFtpClient().disconnect(); return false; } else { System.out.println("=== amis-ftp === FTPTask: ftpConnect: == INFO == Positive Completion!"); return true; } } else { System.out.println( "=== amis-ftp === FTPTask: ftpConnect: == ERROR == ftpConnectFTP Client is Null "); } } catch (IOException ex) { ex.printStackTrace(); return false; } return true; }