org.apache.commons.net.ftp.ftpclient Java Examples
The following examples show how to use
org.apache.commons.net.ftp.ftpclient.
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: FileUtils.java From webcurator with Apache License 2.0 | 7 votes |
public static void removeFTPDirectory(FTPClient ftpClient, String directoryName) { try { ftpClient.changeWorkingDirectory(directoryName); for (FTPFile file : ftpClient.listFiles()) { if (file.isDirectory()) { FileUtils.removeFTPDirectory(ftpClient, file.getName()); } else { log.debug("Deleting " + file.getName()); ftpClient.deleteFile(file.getName()); } } ftpClient.changeWorkingDirectory(directoryName); ftpClient.changeToParentDirectory(); log.debug("Deleting " + directoryName); ftpClient.removeDirectory(directoryName); } catch (Exception ex) { } }
Example #2
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 #3
Source File: FTPUploader.java From azure-gradle-plugins with MIT License | 6 votes |
/** * Upload directory to specified FTP server without retries. * * @param ftpServer * @param username * @param password * @param sourceDirectoryPath * @param targetDirectoryPath * @return Boolean to indicate whether uploading is successful. */ protected boolean uploadDirectory(final String ftpServer, final String username, final String password, final String sourceDirectoryPath, final String targetDirectoryPath) { logger.debug("FTP username: " + username); try { final FTPClient ftpClient = getFTPClient(ftpServer, username, password); logger.quiet(String.format(UPLOAD_DIR_START, sourceDirectoryPath, targetDirectoryPath)); uploadDirectory(ftpClient, sourceDirectoryPath, targetDirectoryPath, ""); logger.quiet(String.format(UPLOAD_DIR_FINISH, sourceDirectoryPath, targetDirectoryPath)); ftpClient.disconnect(); return true; } catch (Exception e) { logger.error(String.format(UPLOAD_DIR_FAILURE, sourceDirectoryPath, targetDirectoryPath), e); } return false; }
Example #4
Source File: FTPOperationHandler.java From CloverETL-Engine with GNU Lesser General Public License v2.1 | 6 votes |
@Override public SingleCloverURI create(SingleCloverURI target, CreateParameters params) throws IOException { if (params.getLastModified() != null) { throw new UnsupportedOperationException(FileOperationMessages.getString("FTPOperationHandler.setting_date_not_supported")); //$NON-NLS-1$ } URI uri = target.toURI().normalize(); FTPClient ftp = null; try { ftp = connect(uri); if (create(ftp, uri, params)) { return CloverURI.createSingleURI(uri); } } finally { disconnect(ftp); } return null; }
Example #5
Source File: FTPTransfer.java From localization_nifi with Apache License 2.0 | 6 votes |
@Override public void ensureDirectoryExists(final FlowFile flowFile, final File directoryName) throws IOException { if (directoryName.getParent() != null && !directoryName.getParentFile().equals(new File(File.separator))) { ensureDirectoryExists(flowFile, directoryName.getParentFile()); } final String remoteDirectory = directoryName.getAbsolutePath().replace("\\", "/").replaceAll("^.\\:", ""); final FTPClient client = getClient(flowFile); final boolean cdSuccessful = setWorkingDirectory(remoteDirectory); if (!cdSuccessful) { logger.debug("Remote Directory {} does not exist; creating it", new Object[] {remoteDirectory}); if (client.makeDirectory(remoteDirectory)) { logger.debug("Created {}", new Object[] {remoteDirectory}); } else { throw new IOException("Failed to create remote directory " + remoteDirectory); } } }
Example #6
Source File: FTPUtil.java From seed with Apache License 2.0 | 6 votes |
/** * 登出FTP服务器 * <p> * 由于本工具类会自动维护FTPClient连接,故调用该方法便可直接登出FTP * </p> */ public static void logout(){ FTPClient ftpClient = ftpClientMap.get(); ftpClientMap.remove(); if(null != ftpClient){ String ftpRemoteAddress = ftpClient.getRemoteAddress().toString(); try{ ftpClient.logout(); LogUtil.getLogger().debug("FTP服务器[" + ftpRemoteAddress + "]登出成功..."); }catch (IOException e){ LogUtil.getLogger().warn("FTP服务器[" + ftpRemoteAddress + "]登出时发生异常,堆栈轨迹如下", e); }finally{ if(ftpClient.isConnected()){ try { ftpClient.disconnect(); LogUtil.getLogger().debug("FTP服务器[" + ftpRemoteAddress + "]连接释放完毕..."); } catch (IOException ioe) { LogUtil.getLogger().warn("FTP服务器[" + ftpRemoteAddress + "]连接释放时发生异常,堆栈轨迹如下", ioe); } } } } }
Example #7
Source File: FTPUtil.java From seed with Apache License 2.0 | 6 votes |
/** * 创建远程目录 * @param remotePath 不含文件名的远程路径(格式为/a/b/c) */ private static void createRemoteFolder(FTPClient ftpClient, String remotePath) throws IOException{ String[] folders = remotePath.split("/"); String remoteTempPath = ""; for(String folder : folders){ if(StringUtils.isNotBlank(folder)){ remoteTempPath += "/" + folder; boolean flag = ftpClient.changeWorkingDirectory(remoteTempPath); LogUtil.getLogger().info("change working directory : " + remoteTempPath + "-->" + (flag?"SUCCESS":"FAIL")); if(!flag){ flag = ftpClient.makeDirectory(remoteTempPath); LogUtil.getLogger().info("make directory : " + remoteTempPath + "-->" + (flag?"SUCCESS":"FAIL")); } } } }
Example #8
Source File: FTPUtil.java From seed with Apache License 2.0 | 6 votes |
/** * 上传文件 * <p> * 该方法与{@link #uploadAndLogout(String, String, String, String, InputStream)}的区别是: * 上传完文件后没有登出服务器及释放连接,但会关闭输入流; * 之所以提供该方法是用于同时上传多个文件的情况下,使之能够共用一个FTP连接 * </p> * @param hostname 目标主机地址 * @param username FTP登录用户 * @param password FTP登录密码 * @param remoteURL 保存在FTP上的含完整路径和后缀的完整文件名 * @param is 文件输入流 * @return True if successfully completed, false if not. */ public static boolean upload(String hostname, String username, String password, String remoteURL, InputStream is){ if(!login(hostname, username, password, DEFAULT_DEFAULT_TIMEOUT, DEFAULT_CONNECT_TIMEOUT, DEFAULT_DATA_TIMEOUT)){ return false; } FTPClient ftpClient = ftpClientMap.get(); try{ remoteURL = FilenameUtils.separatorsToUnix(remoteURL); if(!ftpClient.changeWorkingDirectory(FilenameUtils.getFullPathNoEndSeparator(remoteURL))){ createRemoteFolder(ftpClient, FilenameUtils.getFullPathNoEndSeparator(remoteURL)); ftpClient.changeWorkingDirectory(FilenameUtils.getFullPathNoEndSeparator(remoteURL)); } String remoteFile = new String(FilenameUtils.getName(remoteURL).getBytes(DEFAULT_CHARSET), "ISO-8859-1"); ftpClient.setCopyStreamListener(new FTPProcess(is.available(), System.currentTimeMillis())); return ftpClient.storeFile(remoteFile, is); }catch(IOException e){ LogUtil.getLogger().error("文件["+remoteURL+"]上传到FTP服务器["+hostname+"]失败,堆栈轨迹如下", e); return false; }finally{ IOUtils.closeQuietly(is); } }
Example #9
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 #10
Source File: FtpConnection.java From xian with Apache License 2.0 | 6 votes |
FTPClient connect(String path, String addr, int port, String username, String password) throws Exception { FTPClient ftp = new FTPClient(); int reply; ftp.connect(addr, port); ftp.login(username, password); ftp.configure(new FTPClientConfig(ftp.getSystemType())); ftp.setControlEncoding("UTF-8"); ftp.setFileType(FTPClient.BINARY_FILE_TYPE); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return null; } ftp.changeWorkingDirectory(path); return ftp; }
Example #11
Source File: FTPFileSystem.java From hadoop with Apache License 2.0 | 6 votes |
/** * Convenience method, so that we don't open a new connection when using this * method from within another method. Otherwise every API invocation incurs * the overhead of opening/closing a TCP connection. */ private boolean delete(FTPClient client, Path file, boolean recursive) throws IOException { Path workDir = new Path(client.printWorkingDirectory()); Path absolute = makeAbsolute(workDir, file); String pathName = absolute.toUri().getPath(); try { FileStatus fileStat = getFileStatus(client, absolute); if (fileStat.isFile()) { return client.deleteFile(pathName); } } catch (FileNotFoundException e) { //the file is not there return false; } FileStatus[] dirEntries = listStatus(client, absolute); if (dirEntries != null && dirEntries.length > 0 && !(recursive)) { throw new IOException("Directory: " + file + " is not empty."); } for (FileStatus dirEntry : dirEntries) { delete(client, new Path(absolute, dirEntry.getPath()), recursive); } return client.removeDirectory(pathName); }
Example #12
Source File: FtpFileUtil.java From hsac-fitnesse-fixtures with Apache License 2.0 | 6 votes |
/** * Delete given directory from FTP server (directory must be empty). * @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 remotePath the path to the directory on the FTP to be removed * @return true if file has been removed and false otherwise. * @throws RuntimeException in case any exception has been thrown. */ public static boolean deleteDirectoryFromFTPServer(String hostName, Integer port, String userName, String password, String remotePath) { boolean deleted = false; FTPClient ftpClient = new FTPClient(); String errorMessage = "Could not delete the directory '%s' from FTP server '%s'. Cause: %s"; try { connectAndLoginOnFTPServer(ftpClient, hostName, port, userName, password); deleted = ftpClient.removeDirectory(remotePath); } catch (IOException ex) { throw new RuntimeException(String.format(errorMessage, remotePath, hostName), ex); } finally { disconnectAndLogoutFromFTPServer(ftpClient, hostName); } return deleted; }
Example #13
Source File: FTPUtil.java From anyline with Apache License 2.0 | 6 votes |
/** * 下载单个文件 * @param remote remote * @param local local * @return return */ public boolean downloadFile(String remote, File local) { boolean success = false; long fr = System.currentTimeMillis(); try { client.setFileType(FTPClient.BINARY_FILE_TYPE); File _localDir = local.getParentFile(); if(!_localDir.exists()){ _localDir.mkdirs(); } OutputStream is = new FileOutputStream(local); client.retrieveFile(remote, is); success = true; } catch (IOException e) { e.printStackTrace(); } log.warn("[ftp download file][耗时:{}][length:{}][remote:{}][local:{}]",DateUtil.conversion(System.currentTimeMillis()-fr),FileUtil.length(local.length()),remote, local.getAbsolutePath()); return success; }
Example #14
Source File: NetUtils.java From ApprovalTests.Java with Apache License 2.0 | 6 votes |
public static void ftpUpload(FTPConfig config, String directory, File file, String remoteFileName) throws IOException { FTPClient server = new FTPClient(); server.connect(config.host, config.port); assertValidReplyCode(server.getReplyCode(), server); server.login(config.userName, config.password); assertValidReplyCode(server.getReplyCode(), server); assertValidReplyCode(server.cwd(directory), server); server.setFileTransferMode(FTP.BINARY_FILE_TYPE); server.setFileType(FTP.BINARY_FILE_TYPE); server.storeFile(remoteFileName, new FileInputStream(file)); assertValidReplyCode(server.getReplyCode(), server); server.sendNoOp(); server.disconnect(); }
Example #15
Source File: MultipleConnectionTestCase.java From commons-vfs with Apache License 2.0 | 6 votes |
@Test public void testUnderlyingConnect() throws SocketException, IOException { final FTPClient client1 = new FTPClient(); final FTPClient client2 = new FTPClient(); try { final String hostname = "localhost"; client1.connect(hostname, FtpProviderTestCase.getSocketPort()); client2.connect(hostname, FtpProviderTestCase.getSocketPort()); } finally { if (client1 != null) { client1.disconnect(); } if (client2 != null) { client2.disconnect(); } } }
Example #16
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 #17
Source File: FTPFileSystem.java From hadoop with Apache License 2.0 | 6 votes |
/** * Convenience method, so that we don't open a new connection when using this * method from within another method. Otherwise every API invocation incurs * the overhead of opening/closing a TCP connection. */ private FileStatus[] listStatus(FTPClient client, Path file) throws IOException { Path workDir = new Path(client.printWorkingDirectory()); Path absolute = makeAbsolute(workDir, file); FileStatus fileStat = getFileStatus(client, absolute); if (fileStat.isFile()) { return new FileStatus[] { fileStat }; } FTPFile[] ftpFiles = client.listFiles(absolute.toUri().getPath()); FileStatus[] fileStats = new FileStatus[ftpFiles.length]; for (int i = 0; i < ftpFiles.length; i++) { fileStats[i] = getFileStatus(ftpFiles[i], absolute); } return fileStats; }
Example #18
Source File: NetUtils.java From ApprovalTests.Java with Apache License 2.0 | 6 votes |
private static void assertValidReplyCode(int code, FTPClient ftp) { if (FTPReply.isPositiveCompletion(code)) { //good SimpleLogger.variable("Good Completion code " + code); } else if (FTPReply.isPositiveIntermediate(code)) { // do nothing SimpleLogger.variable("Good Intermediate code " + code); } else if (FTPReply.isPositivePreliminary(code)) { // do nothing SimpleLogger.variable("Good Preliminary code " + code); } else { // bad throw new Error("Problem encountered with FTP Server, returned Code " + code + ", replied '" + ftp.getReplyString() + "'"); } }
Example #19
Source File: FTPFileSystem.java From RDFS with Apache License 2.0 | 5 votes |
@Override public boolean rename(Path src, Path dst) throws IOException { FTPClient client = connect(); try { boolean success = rename(client, src, dst); return success; } finally { disconnect(client); } }
Example #20
Source File: FtpFileAdaptor.java From xenon with Apache License 2.0 | 5 votes |
private void connectToServer(String host, int port, FTPClient ftp) throws XenonException { try { ftp.connect(host, port); } catch (IOException e) { throw new XenonException(getName(), "Failed to connect", e); } }
Example #21
Source File: FtpFileMoverTest.java From webcurator with Apache License 2.0 | 5 votes |
@Test public void test_calling_create_And_Change_To_Directory() throws IOException { Mockery mockContext = constructMockContext(); final FTPClient mockedFtpClient = mockContext.mock(FTPClient.class); final FtpClientFactory mockedFactory = mockContext.mock(FtpClientFactory.class); final String directoryName = "directoryName"; mockContext.checking(new Expectations() { { one(mockedFactory).createInstance(); will(returnValue(mockedFtpClient)); one(mockedFtpClient).connect(with(any(String.class))); one(mockedFtpClient).user(with(any(String.class))); will(returnValue(1)); one(mockedFtpClient).pass(with(any(String.class))); one(mockedFtpClient).setFileType(FTP.BINARY_FILE_TYPE); one(mockedFtpClient).makeDirectory(directoryName); will(returnValue(true)); one(mockedFtpClient).changeWorkingDirectory(directoryName); } }); WctDepositParameter depositParameter = new WctDepositParameter(); FtpFileMover ftpFileMover = new FtpFileMover(mockedFactory); ftpFileMover.connect(depositParameter); ftpFileMover.createAndChangeToDirectory(directoryName); mockContext.assertIsSatisfied(); }
Example #22
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 #23
Source File: FTPInputStream.java From big-c with Apache License 2.0 | 5 votes |
public FTPInputStream(InputStream stream, FTPClient client, FileSystem.Statistics stats) { if (stream == null) { throw new IllegalArgumentException("Null InputStream"); } if (client == null || !client.isConnected()) { throw new IllegalArgumentException("FTP client null or not connected"); } this.wrappedStream = stream; this.client = client; this.stats = stats; this.pos = 0; this.closed = false; }
Example #24
Source File: FtpUtil.java From learning-taotaoMall with MIT License | 5 votes |
/** * Description: 从FTP服务器下载文件 * @param host FTP服务器hostname * @param port FTP服务器端口 * @param username FTP登录账号 * @param password FTP登录密码 * @param remotePath FTP服务器上的相对路径 * @param fileName 要下载的文件名 * @param localPath 下载后保存到本地的路径 * @return */ public static boolean downloadFile(String host, int port, String username, String password, String remotePath, String fileName, String localPath) { boolean result = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(host, port); // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器 ftp.login(username, password);// 登录 reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录 FTPFile[] fs = ftp.listFiles(); for (FTPFile ff : fs) { if (ff.getName().equals(fileName)) { File localFile = new File(localPath + "/" + ff.getName()); OutputStream is = new FileOutputStream(localFile); ftp.retrieveFile(ff.getName(), is); is.close(); } } ftp.logout(); result = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return result; }
Example #25
Source File: FTPOperationHandler.java From CloverETL-Engine with GNU Lesser General Public License v2.1 | 5 votes |
@Override public List<Info> list(SingleCloverURI parent, ListParameters params) throws IOException { URI parentUri = parent.toURI(); FTPClient ftp = null; try { ftp = connect(parentUri); return list(parentUri, ftp, params); } finally { disconnect(ftp); } }
Example #26
Source File: FtpUploadTemplate.java From MicroCommunity with Apache License 2.0 | 5 votes |
public void closeConnect(FTPClient ftpClient) { try { ftpClient.disconnect(); } catch (Exception e) { e.printStackTrace(); } }
Example #27
Source File: FtpServiceImp.java From imageServer with Apache License 2.0 | 5 votes |
/** * 删除FTP服务器上的指定文件 * * @param filePath 被删除文件在FTP服务器上的全路径 * @return true : 删除成功 * @throws IOException 删除失败 */ @Override synchronized public boolean deleteFile(String filePath) throws IOException { FTPClient ftpClient = FTPUtil.getFTPClient(ftpConnectAttr); boolean result = ftpClient != null && ftpClient.deleteFile(filePath); if(result && ftpClient.isConnected()) ftpClient.disconnect(); return result; }
Example #28
Source File: FtpConnectionFactory.java From BigDataScript with Apache License 2.0 | 5 votes |
/** * List files using an FTP client */ public FTPFile[] listFiles(FTPClient ftp, URI uri) { synchronized (ftp) { try { return ftp.listFiles(uri.getPath()); } catch (IOException e) { String msg = "Error reading remote directory '" + uri + "'"; Timer.showStdErr(msg); throw new RuntimeException(msg, e); } } }
Example #29
Source File: FtpsClientWrapper.java From commons-vfs with Apache License 2.0 | 5 votes |
@Override protected FTPClient createClient(final GenericFileName rootName, final UserAuthenticationData authData) throws FileSystemException { return FtpsClientFactory.createConnection(rootName.getHostName(), rootName.getPort(), UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName())), UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(rootName.getPassword())), rootName.getPath(), getFileSystemOptions()); }
Example #30
Source File: FtpClientManager.java From onetwo with Apache License 2.0 | 5 votes |
public void init() { if(initialized){ return ; } FTPClient ftpClient = new FTPClient(); ftpClient.setControlEncoding(ftpConfig.getEncoding()); ftpClient.setBufferSize(ftpConfig.getBufferSize()); ftpClient.setPassiveNatWorkaround(ftpConfig.isPasv()); this.ftpClient = ftpClient; this.initialized = true; }