List of usage examples for org.apache.commons.net.ftp FTPClient listFiles
public FTPFile[] listFiles(String pathname) throws IOException
From source file:org.openspice.vfs.ftp.FtpTools.java
public static final boolean fileExists(final FTPClient ftpc, final String path) { boolean exists = false; try {/*from w w w . j a v a 2s .c o m*/ final FTPFile[] files = ftpc.listFiles(path); if (files.length == 1) { exists = true; } else if (files.length > 1) { throw new Alert("Cannot determine this path is a file").culprit("path", path).mishap(); } } catch (IOException e) { throw new RuntimeException(e); } return exists; }
From source file:org.openspice.vfs.ftp.FtpVFolder.java
private List list(final boolean want_folders, final boolean want_files) { // System.err.println( "folders = " + want_folders ); // System.err.println( "files = " + want_files ); try {/* w w w .j a v a2 s . c om*/ final FTPClient ftpc = this.fvol.getConnectedFTPClient(); final FTPFile[] files = ftpc.listFiles(this.path); // { // System.err.println( "List of files is " + files.length + " long" ); // for ( int n = 0; n < files.length; n++ ) { // System.err.println( "["+n+"]. " + files[n] + " " + ( files[n].isDirectory() ? "d" : "-" ) + ( files[n].isFile() ? "f" : "-" ) ); // } // } final List answer = new ArrayList(); for (int i = 0; i < files.length; i++) { final FTPFile file = files[i]; if (want_folders && file.isDirectory()) { // System.err.println( "adding FOLDER " + file ); answer.add(new FtpVFolder(this.fvol, FtpTools.folderName(this.path, file.getName()))); } else if (want_files && (file.isFile() || file.isSymbolicLink())) { // System.err.println( "adding FILE " + file ); answer.add(FtpVFile.uncheckedMake(this.fvol, FtpTools.fileName(this.path, file.getName()))); } } return answer; } catch (IOException e) { throw new RuntimeException(e); } }
From source file:org.ow2.proactive.scheduler.examples.FTPConnector.java
private List<String> ftpGet(FTPClient ftpClient) throws IOException { List<String> filesRelativePathName; getOut().println("Importing file(s) from " + ftpRemoteRelativePath + " to " + ftpLocalRelativePath); FTPFile[] ftpFile = ftpClient.listFiles(ftpRemoteRelativePath); if (ftpFile.length == 0) { throw new IllegalArgumentException(ftpRemoteRelativePath + " not found. Please, enter a valid path."); }// w ww . j a v a 2 s .c o m filesRelativePathName = new ArrayList<>(); // If it is a single file: if (ftpFile.length == 1 && ftpRemoteRelativePath.contains(ftpFile[0].getName())) { String saveFilePath = Paths .get(ftpLocalRelativePath, Paths.get(ftpFile[0].getName()).getFileName().toString()).toString(); filesRelativePathName.add(downloadSingleFile(ftpClient, ftpRemoteRelativePath, saveFilePath)); // If the file is a zip, and ftpExtractArchive is set to true if (ftpExtractArchive && ftpRemoteRelativePath.endsWith(".zip")) { ZipUtil.unpack(new File(saveFilePath), new File(ftpLocalRelativePath)); } } // If it is a folder, download all its contents recursively else { ftpClient.changeWorkingDirectory(ftpRemoteRelativePath); filesRelativePathName.addAll(new HashSet(downloadDirectory(ftpClient, "", "", ftpLocalRelativePath))); } getOut().println("END Import file(s) from FTP."); return filesRelativePathName; }
From source file:org.ow2.proactive.scheduler.examples.FTPConnector.java
private List<String> downloadDirectory(FTPClient ftpClient, String parentDir, String currentDir, String saveDir) throws IOException { List<String> filesRelativePathName = new ArrayList<>(); String dirToList = parentDir; if (!currentDir.isEmpty()) { dirToList = Paths.get(dirToList, currentDir).toString(); }//from w w w . j a v a 2s .com FTPFile[] subFiles = ftpClient.listFiles(dirToList); for (FTPFile aFile : subFiles) { String currentFileName = aFile.getName(); if (currentFileName.equals(CURRENT_FOLDER) || currentFileName.equals(PARENT_FOLDER)) { // skip parent directory and the directory itself continue; } String remoteFilePath = Paths.get(parentDir, currentDir, currentFileName).toString(); String savePath = Paths.get(saveDir, parentDir, currentDir, currentFileName).toString(); if (aFile.isDirectory()) { // create the directory savePath inside saveDir makeDirectories(savePath); // download the sub directory filesRelativePathName.addAll(downloadDirectory(ftpClient, dirToList, currentFileName, saveDir)); } else { // download the file filesRelativePathName.add(downloadSingleFile(ftpClient, remoteFilePath, savePath)); } } return filesRelativePathName; }
From source file:org.ramadda.repository.type.FtpTypeHandler.java
/** * _more_/*from w w w . j av a 2s . co m*/ * * @param request _more_ * @param mainEntry _more_ * @param parentEntry _more_ * @param synthId _more_ * * @return _more_ * * @throws Exception _more_ */ public List<String> getSynthIds(Request request, Entry mainEntry, Entry parentEntry, String synthId) throws Exception { long t0 = System.currentTimeMillis(); List<String> ids = new ArrayList<String>(); Object[] values = mainEntry.getValues(); String baseDir = (String) values[COL_BASEDIR]; String path = getPathFromId(synthId, baseDir); /* boolean descending = !request.get(ARG_ASCENDING, false); if (request.getString(ARG_ORDERBY, "").equals("name")) { files = IOUtil.sortFilesOnName(files, descending); } else { files = IOUtil.sortFilesOnAge(files, descending); }*/ long t1 = System.currentTimeMillis(); FTPClient ftpClient = getFtpClient(mainEntry); if (ftpClient == null) { return ids; } long t2 = System.currentTimeMillis(); // System.err.println ("getFtpClient:" + (t2-t1)); try { String pattern = (String) values[COL_FILE_PATTERN]; if ((pattern != null) && (pattern.trim().length() == 0)) { pattern = null; } boolean isDir = ftpClient.changeWorkingDirectory(path); if (isDir) { boolean checkReadme = parentEntry.getDescription().length() == 0; checkReadme = false; long t3 = System.currentTimeMillis(); FTPFile[] files = ftpClient.listFiles(path); long t4 = System.currentTimeMillis(); // System.err.println ("listFiles:" + (t4-t3)); for (int i = 0; i < files.length; i++) { String name = files[i].getName().toLowerCase(); if ((pattern != null) && !name.matches(pattern)) { continue; } if (checkReadme) { if (name.equals("readme") || name.equals("readme.txt")) { try { InputStream fis = ftpClient.retrieveFileStream(path + "/" + files[i].getName()); if (fis != null) { String desc = HtmlUtils.entityEncode(IOUtil.readInputStream(fis)); parentEntry.setDescription(HtmlUtils.pre(desc)); fis.close(); ftpClient.completePendingCommand(); } } catch (Exception exc) { // exc.printStackTrace(); } } } putCache(mainEntry, path + "/" + files[i].getName(), files[i]); ids.add(getSynthId(mainEntry, baseDir, path, files[i])); } } } finally { closeConnection(ftpClient); } long t5 = System.currentTimeMillis(); // System.err.println ("getSynthIds:" + (t5-t0)); return ids; }
From source file:org.ramadda.repository.type.FtpTypeHandler.java
/** * _more_// w ww . j ava 2 s . c om * * @param server _more_ * @param baseDir _more_ * @param user _more_ * @param password _more_ * * @return _more_ * * @throws Exception _more_ */ public static String test(String server, String baseDir, String user, String password) throws Exception { FTPClient ftpClient = new FTPClient(); try { String file = baseDir; ftpClient.connect(server); //System.out.print(ftp.getReplyString()); ftpClient.login(user, password); // System.out.print(ftpClient.getReplyString()); int reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftpClient.disconnect(); System.err.println("FTP server refused connection."); return null; } ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); boolean isDir = isDir(ftpClient, file); // System.err.println("file:" + file + " is dir: " + isDir); if (isDir) { FTPFile[] files = ftpClient.listFiles(file); for (int i = 0; i < files.length; i++) { // System.err.println ("f:" + files[i].getName() + " " + files[i].isDirectory() + " " + files[i].isFile()); } } else { ByteArrayOutputStream bos = new ByteArrayOutputStream(); if (ftpClient.retrieveFile(file, bos)) { // System.err.println(new String(bos.toByteArray())); } else { throw new IOException("Unable to retrieve file:" + file); } } return ""; } finally { closeConnection(ftpClient); } }
From source file:org.ramadda.repository.type.FtpTypeHandler.java
/** * _more_//from w ww . j a v a 2s . c om * * @param parentEntry _more_ * @param id _more_ * @param baseDir _more_ * * @return _more_ * * @throws Exception _more_ */ public MyFTPFile getFileFromId(Entry parentEntry, String id, String baseDir) throws Exception { String path; if ((id == null) || (id.length() == 0)) { FTPFile file = new FTPFile(); file.setName(baseDir); file.setType(FTPFile.DIRECTORY_TYPE); return new MyFTPFile(file, baseDir); } else { path = new String(RepositoryUtil.decodeBase64(id)); } FTPFile ftpFile = getCache(parentEntry).get(path); if (ftpFile != null) { return new MyFTPFile(ftpFile, path); } FTPClient ftpClient = getFtpClient(parentEntry); if (ftpClient == null) { return null; } // xxx try { boolean isDir = isDir(ftpClient, path); if (isDir) { File tmp = new File(path); String parent = tmp.getParent().replace("\\", "/"); // System.err.println("getFileFromId path=" + path +" parent:" + parent); String name = tmp.getName(); FTPFile[] files = ftpClient.listFiles(parent); MyFTPFile lookingForThisOne = null; for (int i = 0; i < files.length; i++) { String childPath = parent + "/" + files[i].getName(); putCache(parentEntry, childPath, files[i]); if (files[i].getName().equals(name)) { lookingForThisOne = new MyFTPFile(files[i], childPath); } } if (lookingForThisOne != null) { return lookingForThisOne; } System.err.println("Could not find directory:" + name + " path:" + path); return null; } else { // System.err.println("getFileFromId path=" + path); FTPFile[] files = ftpClient.listFiles(path); if (files.length == 1) { putCache(parentEntry, path, files[0]); return new MyFTPFile(files[0], path); } else { System.err.println("Got bad # of files when getting file:" + files.length + " " + path); } } return null; } finally { closeConnection(ftpClient); } }
From source file:org.ramadda.util.HtmlUtils.java
/** * _more_/*from w ww .j ava2 s .com*/ * * @param url _more_ * @param linkPattern _more_ * * @return _more_ * * @throws Exception _more_ */ public static List<Link> extractLinksFtp(URL url, String linkPattern) throws Exception { FTPClient ftpClient = null; try { ftpClient = Utils.makeFTPClient(url); if (ftpClient == null) { return null; } List<Link> links = new ArrayList<Link>(); FTPFile[] files = ftpClient.listFiles(url.getPath()); for (int i = 0; i < files.length; i++) { if (!files[i].isFile()) { continue; } String href = files[i].getName(); String label = href; if (linkPattern != null) { if (!(href.matches(linkPattern) || label.matches(linkPattern))) { continue; } } URL newUrl = new URL(url, href); links.add(new Link(newUrl, label, files[i].getSize())); } return links; } finally { Utils.closeConnection(ftpClient); } }
From source file:org.shept.util.FtpFileCopy.java
/** * Creates the needed directories if necessary. * @param ftpClient A <code>FTPClient</code> being <b>connected</b>. * @param basePath The base path. This one <b>has to exist</b> on the ftp server! * @param path The path to be created.//from w w w . java2s .c o m * @throws IOException IN case of an error. */ private boolean ensureFtpDirectory(FTPClient ftpClient, String basePath, String path) throws IOException { ftpClient.changeWorkingDirectory(basePath); StringTokenizer tokenizer = new StringTokenizer(path, "/"); while (tokenizer.hasMoreTokens()) { String folder = tokenizer.nextToken(); FTPFile[] ftpFile = ftpClient.listFiles(folder); if (ftpFile.length == 0) { // create the directoy if (!ftpClient.makeDirectory(folder)) { logger.error( "Ftp Creating the destination directory did not succeed " + ftpClient.getReplyString()); return false; } } ftpClient.changeWorkingDirectory(folder); } return true; }
From source file:org.soitoolkit.commons.mule.ftp.FtpUtil.java
/** * Deletes a directory with all its files and sub-directories. * /*ww w . j a va2 s . c o m*/ * @param ftpClient * @param path * @throws IOException */ static public void recursiveDeleteDirectory(FTPClient ftpClient, String path) throws IOException { logger.info("Delete directory: {}", path); FTPFile[] ftpFiles = ftpClient.listFiles(path); logger.debug("Number of files that will be deleted: {}", ftpFiles.length); for (FTPFile ftpFile : ftpFiles) { String filename = path + "/" + ftpFile.getName(); if (ftpFile.getType() == FTPFile.FILE_TYPE) { boolean deleted = ftpClient.deleteFile(filename); logger.debug("Deleted {}? {}", filename, deleted); } else { recursiveDeleteDirectory(ftpClient, filename); } } boolean dirDeleted = ftpClient.deleteFile(path); logger.debug("Directory {} deleted: {}", path, dirDeleted); }