Java Code Examples for org.apache.commons.net.ftp.FTPClient#enterLocalPassiveMode()
The following examples show how to use
org.apache.commons.net.ftp.FTPClient#enterLocalPassiveMode() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FtpFileSystem.java From xenon with Apache License 2.0 | 6 votes |
@Override public OutputStream writeToFile(Path path, long size) throws XenonException { LOGGER.debug("writeToFile path = {} size = {}", path, size); assertIsOpen(); Path absPath = toAbsolutePath(path); assertPathNotExists(absPath); assertParentDirectoryExists(absPath); // Since FTP connections can only do a single thing a time, we need a // new FTPClient to handle the stream. FTPClient newClient = adaptor.connect(getLocation(), credential); newClient.enterLocalPassiveMode(); try { newClient.setFileType(FTPClient.BINARY_FILE_TYPE); OutputStream out = newClient.storeFileStream(absPath.toString()); checkClientReply(newClient, "Failed to write to path: " + absPath.toString()); return new TransferClientOutputStream(out, new CloseableClient(newClient)); } catch (IOException e) { throw new XenonException(ADAPTOR_NAME, "Failed to write to path: " + absPath); } }
Example 2
Source File: FtpFileSystem.java From xenon with Apache License 2.0 | 6 votes |
@Override public InputStream readFromFile(Path path) throws XenonException { LOGGER.debug("newInputStream path = {}", path); assertIsOpen(); Path absPath = toAbsolutePath(path); assertPathExists(absPath); assertPathIsFile(absPath); // Since FTP connections can only do a single thing a time, we need a // new FTPClient to handle the stream. FTPClient newClient = adaptor.connect(getLocation(), credential); newClient.enterLocalPassiveMode(); try { InputStream in = newClient.retrieveFileStream(absPath.toString()); checkClientReply(newClient, "Failed to read from path: " + absPath.toString()); return new TransferClientInputStream(in, new CloseableClient(newClient)); } catch (IOException e) { throw new XenonException(ADAPTOR_NAME, "Failed to read from path: " + absPath); } }
Example 3
Source File: Ftp.java From Lottery with GNU General Public License v2.0 | 6 votes |
private FTPClient getClient() throws SocketException, IOException { FTPClient ftp = new FTPClient(); ftp.addProtocolCommandListener(new PrintCommandListener( new PrintWriter(System.out))); ftp.setDefaultPort(getPort()); ftp.connect(getIp()); int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { log.warn("FTP server refused connection: {}", getIp()); ftp.disconnect(); return null; } if (!ftp.login(getUsername(), getPassword())) { log.warn("FTP server refused login: {}, user: {}", getIp(), getUsername()); ftp.logout(); ftp.disconnect(); return null; } ftp.setControlEncoding(getEncoding()); ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.enterLocalPassiveMode(); return ftp; }
Example 4
Source File: FtpFileSystem.java From xenon with Apache License 2.0 | 6 votes |
@Override public OutputStream appendToFile(Path path) throws XenonException { LOGGER.debug("appendToFile path = {}", path); assertIsOpen(); Path absPath = toAbsolutePath(path); assertPathExists(absPath); assertPathIsNotDirectory(absPath); try { // Since FTP connections can only do a single thing a time, we need // a new FTPClient to handle the stream. FTPClient newClient = adaptor.connect(getLocation(), credential); newClient.enterLocalPassiveMode(); OutputStream out = newClient.appendFileStream(absPath.toString()); if (out == null) { checkClientReply("Failed to append to path: " + absPath.toString()); } return new TransferClientOutputStream(out, new CloseableClient(newClient)); } catch (IOException e) { throw new XenonException(ADAPTOR_NAME, "Failed to append to path: " + absPath); } }
Example 5
Source File: FtpUtils.java From kkFileView with Apache License 2.0 | 6 votes |
public static void download(String ftpUrl, String localFilePath, String ftpUsername, String ftpPassword, String ftpControlEncoding) throws IOException { String username = StringUtils.isEmpty(ftpUsername) ? ConfigConstants.getFtpUsername() : ftpUsername; String password = StringUtils.isEmpty(ftpPassword) ? ConfigConstants.getFtpPassword() : ftpPassword; String controlEncoding = StringUtils.isEmpty(ftpControlEncoding) ? ConfigConstants.getFtpControlEncoding() : ftpControlEncoding; URL url = new URL(ftpUrl); String host = url.getHost(); int port = (url.getPort() == -1) ? url.getDefaultPort() : url.getPort(); String remoteFilePath = url.getPath(); LOGGER.debug("FTP connection url:{}, username:{}, password:{}, controlEncoding:{}, localFilePath:{}", ftpUrl, username, password, controlEncoding, localFilePath); FTPClient ftpClient = connect(host, port, username, password, controlEncoding); OutputStream outputStream = new FileOutputStream(localFilePath); ftpClient.enterLocalPassiveMode(); boolean downloadResult = ftpClient.retrieveFile(new String(remoteFilePath.getBytes(controlEncoding), StandardCharsets.ISO_8859_1), outputStream); LOGGER.debug("FTP download result {}", downloadResult); outputStream.flush(); outputStream.close(); ftpClient.logout(); ftpClient.disconnect(); }
Example 6
Source File: BugReportSenderFtp.java From YalpStore with GNU General Public License v2.0 | 6 votes |
private boolean uploadAll() { FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(FTP_HOST, FTP_PORT); if (!ftpClient.login(FTP_USER, FTP_PASSWORD)) { return false; } String dirName = getDirName(); ftpClient.makeDirectory(dirName); ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE); ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE); boolean result = true; for (File file: files) { result &= upload(ftpClient, file, dirName + "/" + file.getName()); } return result; } catch (IOException e) { Log.e(BugReportSenderFtp.class.getSimpleName(), "FTP network error: " + e.getMessage()); } finally { closeSilently(ftpClient); } return false; }
Example 7
Source File: FTPClientFactory.java From hsweb-framework with Apache License 2.0 | 6 votes |
public PooledObject<FTPClient> makeObject() throws Exception { FTPClient ftpClient = new FTPClient(); ftpClient.setConnectTimeout(config.getClientTimeout()); ftpClient.connect(config.getHost(), config.getPort()); int reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftpClient.disconnect(); logger.warn("FTPServer refused connection"); return null; } boolean result = ftpClient.login(config.getUsername(), config.getPassword()); if (!result) { throw new ConnectException("ftp登陆失败:" + config.getUsername() + "/password:" + config.getPassword() + "@" + config.getHost()); } ftpClient.setFileType(config.getTransferFileType()); ftpClient.setBufferSize(1024); ftpClient.setControlEncoding(config.getEncoding()); if (config.isPassiveMode()) { ftpClient.enterLocalPassiveMode(); } return new DefaultPooledObject<>(ftpClient); }
Example 8
Source File: FtpHelper.java From openemm with GNU Affero General Public License v3.0 | 6 votes |
/** * Connect. * * @throws Exception the exception */ @Override public void connect() throws Exception { try { ftpClient = new FTPClient(); ftpClient.connect(host, port); if (usePassiveMode) { ftpClient.enterLocalPassiveMode(); } if (!ftpClient.login(user, password)) { ftpClient.logout(); throw new Exception("Authentication failed"); } } catch (Exception e) { close(); throw new Exception("Connection failed"); } }
Example 9
Source File: FTPUploader.java From journaldev with MIT License | 5 votes |
public FTPUploader(String host, String user, String pwd) throws Exception{ ftp = new FTPClient(); ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); int reply; ftp.connect(host); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); throw new Exception("Exception in connecting to FTP Server"); } ftp.login(user, pwd); ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.enterLocalPassiveMode(); }
Example 10
Source File: Scar.java From scar with BSD 3-Clause "New" or "Revised" License | 5 votes |
static public boolean ftpUpload (String server, String user, String password, String dir, Paths paths, boolean passive) throws IOException { FTPClient ftp = new FTPClient(); InetAddress address = InetAddress.getByName(server); if (DEBUG) debug("scar", "Connecting to FTP server: " + address); ftp.connect(address); if (passive) ftp.enterLocalPassiveMode(); if (!ftp.login(user, password)) { if (ERROR) error("scar", "FTP login failed for user: " + user); return false; } if (!ftp.changeWorkingDirectory(dir)) { if (ERROR) error("scar", "FTP directory change failed: " + dir); return false; } ftp.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE); for (String path : paths) { if (INFO) info("scar", "FTP upload: " + path); BufferedInputStream input = new BufferedInputStream(new FileInputStream(path)); try { ftp.storeFile(new File(path).getName(), input); } finally { try { input.close(); } catch (Exception ignored) { } } } ftp.logout(); ftp.disconnect(); return true; }
Example 11
Source File: FtpFileUtil.java From hsac-fitnesse-fixtures with Apache License 2.0 | 5 votes |
/** * Reads content of file on from FTP server to String. * @param hostName the FTP server host name to connect * @param port the port to connect * @param userName the user name * @param password the password * @param filePath file to read. * @return file's content. * @throws RuntimeException in case any exception has been thrown. */ public static String loadFileFromFTPServer(String hostName, Integer port, String userName, String password, String filePath, int numberOfLines) { String result = null; FTPClient ftpClient = new FTPClient(); InputStream inputStream = null; String errorMessage = "Unable to connect and download file '%s' from FTP server '%s'."; try { connectAndLoginOnFTPServer(ftpClient, hostName, port, userName, password); // load file into string ftpClient.enterLocalPassiveMode(); inputStream = ftpClient.retrieveFileStream(filePath); validatResponse(ftpClient); result = FileUtil.streamToString(inputStream, filePath, numberOfLines); ftpClient.completePendingCommand(); } catch (IOException ex) { throw new RuntimeException(String.format(errorMessage, filePath, hostName), ex); } finally { closeInputStream(inputStream); disconnectAndLogoutFromFTPServer(ftpClient, hostName); } return result; }
Example 12
Source File: FTPOperationHandler.java From CloverETL-Engine with GNU Lesser General Public License v2.1 | 5 votes |
protected FTPClient connect(URI uri) throws IOException { FTPClient ftp = new FTPClient(); // FTPClientConfig config = new FTPClientConfig(); // config.setServerTimeZoneId("GMT+0"); // ftp.configure(config); ftp.setListHiddenFiles(true); UserInfo userInfo = UserInfo.fromURI(uri); try { int port = uri.getPort(); if (port < 0) { port = 21; } ftp.connect(uri.getHost(), port); if (!ftp.login(userInfo.getUser(), userInfo.getPassword())) { ftp.logout(); throw new IOException(FileOperationMessages.getString("FTPOperationHandler.authentication_failed")); //$NON-NLS-1$ } ftp.enterLocalPassiveMode(); int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { throw new IOException(FileOperationMessages.getString("FTPOperationHandler.connection_failed")); //$NON-NLS-1$ } return ftp; } catch (IOException ioe) { disconnect(ftp); throw new IOException(FileOperationMessages.getString("FTPOperationHandler.connection_failed"), ioe); //$NON-NLS-1$ } }
Example 13
Source File: RomLoader.java From zxpoly with GNU General Public License v3.0 | 5 votes |
static byte[] loadFTPArchive(final String host, final String path, final String name, final String password) throws IOException { final FTPClient client = new FTPClient(); client.connect(host); int replyCode = client.getReplyCode(); if (FTPReply.isPositiveCompletion(replyCode)) { try { client.login(name == null ? "" : name, password == null ? "" : password); client.setFileType(FTP.BINARY_FILE_TYPE); client.enterLocalPassiveMode(); final ByteArrayOutputStream out = new ByteArrayOutputStream(300000); if (client.retrieveFile(path, out)) { return out.toByteArray(); } else { throw new IOException( "Can't load file 'ftp://" + host + path + "\' status=" + client.getReplyCode()); } } finally { client.disconnect(); } } else { client.disconnect(); throw new IOException("Can't connect to ftp '" + host + "'"); } }
Example 14
Source File: FTPDownloader.java From journaldev with MIT License | 5 votes |
public FTPDownloader(String host, String user, String pwd) throws Exception { ftp = new FTPClient(); ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); int reply; ftp.connect(host); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); throw new Exception("Exception in connecting to FTP Server"); } ftp.login(user, pwd); ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.enterLocalPassiveMode(); }
Example 15
Source File: PFtpClient.java From PHONK with GNU General Public License v3.0 | 5 votes |
@PhonkMethod(description = "Connect to a ftp server", example = "") @PhonkMethodParam(params = {"host", "port", "username", "password", "function(connected)"}) public void connect(final String host, final int port, final String username, final String password, final FtpConnectedCb callback) { mFTPClient = new FTPClient(); Thread t = new Thread(() -> { try { mFTPClient.connect(host, port); MLog.d(TAG, "1"); if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) { boolean logged = mFTPClient.login(username, password); mFTPClient.setFileType(FTP.BINARY_FILE_TYPE); mFTPClient.enterLocalPassiveMode(); isConnected = logged; callback.event(logged); } MLog.d(TAG, "" + isConnected); } catch (Exception e) { MLog.d(TAG, "connection failed error:" + e); } }); t.start(); }
Example 16
Source File: FTPUploader.java From azure-gradle-plugins with MIT License | 5 votes |
protected FTPClient getFTPClient(final String ftpServer, final String username, final String password) throws Exception { final FTPClient ftpClient = new FTPClient(); ftpClient.connect(ftpServer); ftpClient.login(username, password); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); return ftpClient; }
Example 17
Source File: FTPClientTemplate.java From MicroCommunity with Apache License 2.0 | 5 votes |
/** * 返回一个FTPClient实例 * * @throws Exception */ private FTPClient getFTPClient()throws Exception { if (ftpClientThreadLocal.get() != null && ftpClientThreadLocal.get().isConnected()) { return ftpClientThreadLocal.get(); } else { FTPClient ftpClient = new FTPClient(); //构造一个FtpClient实例 ftpClient.setControlEncoding(encoding); //设置字符集 connect(ftpClient); //连接到ftp服务器 //设置为passive模式 if (passiveMode) { ftpClient.enterLocalPassiveMode(); String replystr=ftpClient.getReplyString(); ftpClient.sendCommand("PASV"); replystr=ftpClient.getReplyString(); } setFileType(ftpClient); //设置文件传输类型 try { ftpClient.setSoTimeout(clientTimeout); ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); } catch (SocketException e) { throw new Exception("Set timeout error.", e); } ftpClientThreadLocal.set(ftpClient); return ftpClient; } }
Example 18
Source File: CfdaServiceImpl.java From kfs with GNU Affero General Public License v3.0 | 4 votes |
/** * @return * @throws IOException */ public SortedMap<String, CFDA> getGovCodes() throws IOException { Calendar calendar = dateTimeService.getCurrentCalendar(); SortedMap<String, CFDA> govMap = new TreeMap<String, CFDA>(); // ftp://ftp.cfda.gov/programs09187.csv String govURL = parameterService.getParameterValueAsString(CfdaBatchStep.class, KFSConstants.SOURCE_URL_PARAMETER); String fileName = StringUtils.substringAfterLast(govURL, "/"); govURL = StringUtils.substringBeforeLast(govURL, "/"); if (StringUtils.contains(govURL, "ftp://")) { govURL = StringUtils.remove(govURL, "ftp://"); } // need to pull off the '20' in 2009 String year = "" + calendar.get(Calendar.YEAR); year = year.substring(2, 4); fileName = fileName + year; // the last 3 numbers in the file name are the day of the year, but the files are from "yesterday" fileName = fileName + String.format("%03d", calendar.get(Calendar.DAY_OF_YEAR) - 1); fileName = fileName + ".csv"; LOG.info("Getting government file: " + fileName + " for update"); InputStream inputStream = null; FTPClient ftp = new FTPClient(); try { ftp.connect(govURL); int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { LOG.error("FTP connection to server not established."); throw new IOException("FTP connection to server not established."); } boolean isLoggedIn = ftp.login("anonymous", ""); if (!isLoggedIn) { LOG.error("Could not login as anonymous."); throw new IOException("Could not login as anonymous."); } LOG.info("Successfully connected and logged in"); ftp.enterLocalPassiveMode(); inputStream = ftp.retrieveFileStream(fileName); if (inputStream != null) { LOG.info("reading input stream"); InputStreamReader screenReader = new InputStreamReader(inputStream); BufferedReader screen = new BufferedReader(screenReader); CSVReader csvReader = new CSVReader(screenReader, ',', '"', 1); List<String[]> lines = csvReader.readAll(); for (String[] line : lines) { String title = line[0]; String number = line[1]; CFDA cfda = new CFDA(); cfda.setCfdaNumber(number); cfda.setCfdaProgramTitleName(title); govMap.put(number, cfda); } } ftp.logout(); ftp.disconnect(); } finally { if (ftp.isConnected()) { ftp.disconnect(); } } return govMap; }
Example 19
Source File: FtpCheck.java From commons-vfs with Apache License 2.0 | 4 votes |
public static void main(final String[] args) throws Exception { if (args.length < 3) { throw new IllegalArgumentException("Usage: FtpCheck user pass host dir"); } final String user = args[0]; final String pass = args[1]; final String host = args[2]; String dir = null; if (args.length == 4) { dir = args[3]; } final FTPClient client = new FTPClient(); client.connect(host); final int reply = client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { throw new IllegalArgumentException("cant connect: " + reply); } if (!client.login(user, pass)) { throw new IllegalArgumentException("login failed"); } client.enterLocalPassiveMode(); final OutputStream os = client.storeFileStream(dir + "/test.txt"); if (os == null) { throw new IllegalStateException(client.getReplyString()); } os.write("test".getBytes(Charset.defaultCharset())); os.close(); client.completePendingCommand(); if (dir != null && !client.changeWorkingDirectory(dir)) { throw new IllegalArgumentException("change dir to '" + dir + "' failed"); } System.err.println("System: " + client.getSystemType()); final FTPFile[] files = client.listFiles(); for (int i = 0; i < files.length; i++) { final FTPFile file = files[i]; if (file == null) { System.err.println("#" + i + ": " + null); } else { System.err.println("#" + i + ": " + file.getRawListing()); System.err.println("#" + i + ": " + file.toString()); System.err.println("\t name:" + file.getName() + " type:" + file.getType()); } } client.disconnect(); }
Example 20
Source File: RinexNavigation.java From GNSS_Compare with Apache License 2.0 | 4 votes |
private RinexNavigationParserGps getFromFTP(String url) throws IOException{ RinexNavigationParserGps rnp = null; String origurl = url; if(negativeChache.containsKey(url)){ if(System.currentTimeMillis()-negativeChache.get(url).getTime() < 60*60*1000){ throw new FileNotFoundException("cached answer"); }else{ negativeChache.remove(url); } } String filename = url.replaceAll("[ ,/:]", "_"); if(filename.endsWith(".Z")) filename = filename.substring(0, filename.length()-2); File rnf = new File(RNP_CACHE,filename); if(rnf.exists()){ System.out.println(url+" from cache file "+rnf); rnp = new RinexNavigationParserGps(rnf); try{ rnp.init(); return rnp; } catch( Exception e ){ rnf.delete(); } } // if the file doesn't exist of is invalid System.out.println(url+" from the net."); FTPClient ftp = new FTPClient(); try { int reply; System.out.println("URL: "+url); url = url.substring("ftp://".length()); String server = url.substring(0, url.indexOf('/')); String remoteFile = url.substring(url.indexOf('/')); String remotePath = remoteFile.substring(0,remoteFile.lastIndexOf('/')); remoteFile = remoteFile.substring(remoteFile.lastIndexOf('/')+1); ftp.connect(server); ftp.login("anonymous", "[email protected]"); System.out.print(ftp.getReplyString()); // After connection attempt, you should check the reply code to // verify // success. reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); System.err.println("FTP server refused connection."); return null; } ftp.enterLocalPassiveMode(); ftp.setRemoteVerificationEnabled(false); System.out.println("cwd to "+remotePath+" "+ftp.changeWorkingDirectory(remotePath)); System.out.println(ftp.getReplyString()); ftp.setFileType(FTP.BINARY_FILE_TYPE); System.out.println(ftp.getReplyString()); System.out.println("open "+remoteFile); InputStream is = ftp.retrieveFileStream(remoteFile); System.out.println(ftp.getReplyString()); if(ftp.getReplyString().startsWith("550")){ negativeChache.put(origurl, new Date()); throw new FileNotFoundException(); } InputStream uis = is; if(remoteFile.endsWith(".Z")){ uis = new UncompressInputStream(is); } rnp = new RinexNavigationParserGps(uis,rnf); rnp.init(); is.close(); ftp.completePendingCommand(); ftp.logout(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { // do nothing } } } return rnp; }