List of usage examples for org.apache.commons.net.ftp FTPReply isPositiveCompletion
public static boolean isPositiveCompletion(int reply)
From source file:com.zxy.commons.net.ftp.FtpUtils.java
/** * FTP handle// w ww. j av a2 s.c om * * @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:com.toolsverse.io.FtpUtils.java
/** * Connects to the FTP server.//from w ww.j a va 2s .c om * * @param urlStr the url * @param loginId the login id * @param loginPswd the login ppassword * @param passiveMode the passive mode flag * @throws Exception in case of any error */ public void connect(String urlStr, String loginId, String loginPswd, boolean passiveMode) throws Exception { boolean success = false; URL url = new URL(urlStr); String host = url.getHost(); int port = url.getPort(); if (port == -1) port = FTP_PORT; ftpClient.connect(host, port); int reply = ftpClient.getReplyCode(); if (FTPReply.isPositiveCompletion(reply)) { success = ftpClient.login(!Utils.isNothing(loginId) ? loginId : "anonymous", Utils.makeString(loginPswd)); if (!success) disconnect(); else { ftpClient.setFileType(FTP.BINARY_FILE_TYPE); if (passiveMode) ftpClient.enterLocalPassiveMode(); } } }
From source file:com.busfixer.avlpositionlogger.BusfixerFTP.java
public boolean ftpConnect(String host, String username, String password, int port) { try {/*ww w. j av a 2s .c om*/ 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) { Log.d(TAG, "Error: could not connect to host " + host); } return false; }
From source file:architecture.common.adaptor.connector.ftp.AbstractFtpConnector.java
protected Object pull() { try {// ww w . j av a 2 s.co m int reply; if (port > 0) ftp.connect(hostname, port); else ftp.connect(hostname); log.debug("Connected to " + hostname + " on " + (port > 0 ? port : ftp.getDefaultPort())); // After connection attempt, you should check the reply code to // verify // success. reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); log.error("FTP server refused connection."); } if (!ftp.login(username, password)) { ftp.logout(); } log.debug("Remote system is " + ftp.getSystemType()); } catch (Exception e) { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException f) { // do nothing } log.error("FTP server refused connection."); } } return null; }
From source file:gov.nih.nci.cacis.ip.mirthconnect.FTPSSenderTest.java
@Test public void sendDocument() throws Exception { final FTPSClient ftpsClient = sender.getFtpsClient(); int numFiles = getNumFiles(); final File inputFile = new File( Thread.currentThread().getContextClassLoader().getResource("Sample_SFTP_File.xml").toURI()); final InputStream inputStream = new FileInputStream(inputFile); sender.sendDocument(inputStream, TEST_SERVER, EXTENSION); assertTrue(FTPReply.isPositiveCompletion(ftpsClient.getReplyCode())); assertEquals(numFiles + 1, getNumFiles()); }
From source file:Cursling.getFtpDetails.java
public String getFtpMonitorDetails(String url, String text_check) throws IOException { try {// w w w .ja v a 2s . c o m try { ftpClient.logout(); ftpClient.disconnect(); } catch (Exception e) { //ignore line } ftpClient = new FTPClient(); // ftpClient.setSoTimeout(10); ftpClient.setConnectTimeout(global_ftp_timeout * 1000); String ftp_host = ""; int ftp_port = 0000; String ftp_user = ""; String ftp_pass = ""; String isTextPresenset = "No"; //Parsing ftp url String[] details = obj_parseftpurl.getParseFTPUrl(url); ftp_host = details[0]; ftp_port = Integer.parseInt(details[1]); ftp_user = details[2]; ftp_pass = details[3]; if (!ftp_host.equalsIgnoreCase("") && ftp_port != 0000) { ftpClient.connect(ftp_host, ftp_port); } if (!ftp_host.equalsIgnoreCase("") && ftp_port == 0000) { ftpClient.connect(ftp_host); } if (ftpClient.isConnected()) { status = "Up"; state = "alive"; } else { status = "Down"; state = "dead"; } if (!ftp_user.equalsIgnoreCase("") && !ftp_pass.equalsIgnoreCase("")) { ftpClient.login(ftp_user, ftp_pass); int reply = ftpClient.getReplyCode(); if (FTPReply.isPositiveCompletion(reply)) { status = "Up"; state = "alive"; if (text_check.equalsIgnoreCase("---")) { //do nothing isTextPresenset = "---"; } else { FTPFile[] files = ftpClient.listFiles(text_check); if (files.length == 1) { isTextPresenset = "Yes"; } else if (files.length == 0) { isTextPresenset = "No"; } } } else { status = "Auth Failed"; isTextPresenset = "No"; state = "dead"; } } else { isTextPresenset = "---"; } ftpClient.logout(); ftpClient.disconnect(); return ("\"" + url + "\",\"" + status + "\",\"" + "---" + "\",\"" + isTextPresenset + "\",\"" + state + "\""); } catch (Exception e) { if (e.toString().matches(".*java.net.ConnectException.*")) { logger.error("Error : java.net.ConnectException - " + url); status = "ConnectException"; } else if (e.toString().matches(".*java.net.UnknownHostException.*")) { logger.error("Error : java.net.UnknownHostException - " + url); status = "UnknownHost"; } else if (e.toString().matches(".*Timed.*out.*")) { logger.error("Error : " + e + " --- " + url); status = "TimeOut"; } else { logger.error("Error - " + e + " --- " + url); status = "Failed"; } return ("\"" + url + "\",\"" + status + "\",\"" + "---" + "\",\"" + "---" + "\",\"" + "dead" + "\""); } }
From source file:com.eryansky.common.utils.ftp.FtpFactory.java
/** * ?FTP?./*from w ww . j a va2 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; }
From source file:net.shopxx.plugin.ftpStorage.FtpStoragePlugin.java
@Override public void upload(String path, File file, String contentType) { PluginConfig pluginConfig = getPluginConfig(); if (pluginConfig != null) { String host = pluginConfig.getAttribute("host"); Integer port = Integer.valueOf(pluginConfig.getAttribute("port")); String username = pluginConfig.getAttribute("username"); String password = pluginConfig.getAttribute("password"); FTPClient ftpClient = new FTPClient(); InputStream inputStream = null; try {/* ww w . ja v a2 s . c o m*/ inputStream = new BufferedInputStream(new FileInputStream(file)); ftpClient.connect(host, port); ftpClient.login(username, password); ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { String directory = StringUtils.substringBeforeLast(path, "/"); String filename = StringUtils.substringAfterLast(path, "/"); if (!ftpClient.changeWorkingDirectory(directory)) { String[] paths = StringUtils.split(directory, "/"); String p = "/"; ftpClient.changeWorkingDirectory(p); for (String s : paths) { p += s + "/"; if (!ftpClient.changeWorkingDirectory(p)) { ftpClient.makeDirectory(s); ftpClient.changeWorkingDirectory(p); } } } ftpClient.storeFile(filename, inputStream); ftpClient.logout(); } } catch (SocketException e) { throw new RuntimeException(e.getMessage(), e); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } finally { IOUtils.closeQuietly(inputStream); try { if (ftpClient.isConnected()) { ftpClient.disconnect(); } } catch (IOException e) { } } } }
From source file:egovframework.com.ext.jfile.sample.SampleFileUploadCluster.java
public void uploadCompleted(String fileId, String sourceRepositoryPath, String maskingFileName, String originalFileName) { if (logger.isDebugEnabled()) { logger.debug("SampleUploadCluster.process called"); }//from ww w. j a v a2 s . c o m FTPClient ftp = new FTPClient(); OutputStream out = null; File file = new File(sourceRepositoryPath + "/" + maskingFileName); FileInputStream fin = null; BufferedInputStream bin = null; String storeFileName = null; String server = "?IP";//?? ? . ex) 210.25.3.21 try { ftp.connect(server); if (FTPReply.isPositiveCompletion(ftp.getReplyCode())) { ftp.setFileType(FTPClient.BINARY_FILE_TYPE); storeFileName = maskingFileName + originalFileName.substring(originalFileName.lastIndexOf("."), originalFileName.length()); fin = new FileInputStream(file); bin = new BufferedInputStream(fin); ftp.login("testId", "testPassword" + server.substring(server.lastIndexOf(".") + 1, server.length())); if (logger.isDebugEnabled()) { logger.debug(server + " connect success !!! "); } ftp.changeWorkingDirectory("/testdir1/testsubdir2/testupload/"); out = ftp.storeFileStream(storeFileName); FileCopyUtils.copy(fin, out); if (logger.isDebugEnabled()) { logger.debug(" cluster success !!! "); } } else { ftp.disconnect(); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) out.close(); if (bin != null) bin.close(); ftp.logout(); out = null; bin = null; } catch (Exception e2) { e2.printStackTrace(); } } }
From source file:com.denayer.ovsr.MyFTPClient.java
public boolean ftpConnect(String host, String username, String password, int port) { try {//from w w w . j a v a 2 s . c om 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.ASCII_FILE_TYPE); mFTPClient.enterLocalPassiveMode(); return status; } } catch (Exception e) { Log.d(TAG, "Error: could not connect to host " + host); } return false; }