Java Code Examples for org.apache.commons.net.ftp.FTPFile#isDirectory()

The following examples show how to use org.apache.commons.net.ftp.FTPFile#isDirectory() . 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 vote down vote up
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: FTPUtil.java    From anyline with Apache License 2.0 6 votes vote down vote up
private void downloadDir(File localDir){ 
	try{ 
		FTPFile[] files = client.listFiles(); 
        for(FTPFile file:files){ 
        	if(file.isDirectory()){ 
        		cd(file.getName()); 
        		downloadDir(new File(localDir+"/"+file.getName())); 
        	}else{ 
	        	File local = new File(localDir,file.getName()); 
        		downloadFile(this.dir+"/"+file.getName(), local); 
        	} 
        } 
       }catch(Exception e){ 
   		e.printStackTrace(); 
   	} 
}
 
Example 3
Source File: FtpTransferManager.java    From desktop with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Map<String, RemoteFile> list() throws StorageException {
    connect();

    try {
        Map<String, RemoteFile> files = new HashMap<String, RemoteFile>();
        FTPFile[] ftpFiles = ftp.listFiles(getConnection().getPath());

        for (FTPFile f : ftpFiles) {
            files.put(f.getName(), new RemoteFile(f.getName(), f.getSize(), f));
            if (f.isDirectory()) {
                files.putAll(getDirectoryList(f.getName()));
            }
        }

        return files;
    } catch (IOException ex) {
        logger.error("Unable to list FTP directory.", ex);
        throw new StorageException(ex);
    }
}
 
Example 4
Source File: FtpFileSystemTestHelper.java    From iaf with Apache License 2.0 6 votes vote down vote up
@Override
public boolean _fileExists(String folder, String filename) throws IOException, FileSystemException {
	// TODO: must implement folder searching, probably via change-directory
	String path = folder != null ? folder + "/" + filename : filename;
	try {
		FTPFile[] files = ftpSession.ftpClient.listFiles(path);
		for (FTPFile o : files) {
			if (o.isDirectory()) {
				if ((filename.endsWith("/") ? o.getName() + "/" : o.getName()).equals(filename)) {
					return true;
				}
			} else if (o.getName().equals(filename)) {
				return true;
			}
		}
	} catch (IOException e) {
		throw new FileSystemException(e);
	}
	return false;
}
 
Example 5
Source File: FtpFileSystemTestHelper.java    From iaf with Apache License 2.0 6 votes vote down vote up
private void cleanFolder() {
	try {
		FTPFile[] files = ftpSession.ftpClient.listFiles();
		for (FTPFile o : files) {
			if (o.isDirectory() && !o.getName().equals(".") && !o.getName().equals("..")) {
				FTPFile[] filesInFolder = ftpSession.ftpClient.listFiles(o.getName());
				for (FTPFile ftpFile : filesInFolder) {
					ftpSession.ftpClient.deleteFile(o.getName()+"/"+ftpFile.getName());
				}
				ftpSession.ftpClient.removeDirectory(o.getName());
			} else {
				ftpSession.ftpClient.deleteFile(o.getName());
			}
		}
	} catch (IOException e) {
		System.err.println(e);
	}
}
 
Example 6
Source File: FTPFileSystem.java    From RDFS with Apache License 2.0 6 votes vote down vote up
/**
 * Convert the file information in FTPFile to a {@link FileStatus} object. *
 * 
 * @param ftpFile
 * @param parentPath
 * @return FileStatus
 */
private FileStatus getFileStatus(FTPFile ftpFile, Path parentPath) {
  long length = ftpFile.getSize();
  boolean isDir = ftpFile.isDirectory();
  int blockReplication = 1;
  // Using default block size since there is no way in FTP client to know of
  // block sizes on server. The assumption could be less than ideal.
  long blockSize = DEFAULT_BLOCK_SIZE;
  long modTime = ftpFile.getTimestamp().getTimeInMillis();
  long accessTime = 0;
  FsPermission permission = getPermissions(ftpFile);
  String user = ftpFile.getUser();
  String group = ftpFile.getGroup();
  Path filePath = new Path(parentPath, ftpFile.getName());
  return new FileStatus(length, isDir, blockReplication, blockSize, modTime,
      accessTime, permission, user, group, filePath.makeQualified(this));
}
 
Example 7
Source File: PooledFTPOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
public FTPInfo(FTPFile file, URI parent, URI self) throws UnsupportedEncodingException {
	this.file = file;
	this.parent = parent;
	String name = file.getName();
	Matcher m = FILENAME_PATTERN.matcher(name);
	if (m.matches()) {
		name = m.group(1); // some FTPs return full file paths as names, we want only the filename 
	}
	if (name.equals("/")) {
		name = ""; // root directory has no name
	} else if (name.endsWith(URIUtils.PATH_SEPARATOR)) {
		name = name.substring(0, name.length()-1);
	}
	this.name = name;
	// name is modified just for the URI
	if (file.isDirectory() && !name.endsWith(URIUtils.PATH_SEPARATOR)) {
		name = name + URIUtils.PATH_SEPARATOR;
	}
	if (self != null) {
		this.uri = self;
	} else {
		this.uri = URIUtils.getChildURI(parent, name);
	}
}
 
Example 8
Source File: FTPOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
public FTPInfo(FTPFile file, URI parent, URI self) throws UnsupportedEncodingException {
	this.file = file;
	this.parent = parent;
	String name = file.getName();
	Matcher m = FILENAME_PATTERN.matcher(name);
	if (m.matches()) {
		name = m.group(1); // some FTPs return full file paths as names, we want only the filename 
	}
	this.name = name;
	if (file.isDirectory() && !name.endsWith(URIUtils.PATH_SEPARATOR)) {
		name = name + URIUtils.PATH_SEPARATOR;
	}
	if (self != null) {
		this.uri = self;
	} else {
		this.uri = URIUtils.getChildURI(parent, name);
	}
}
 
Example 9
Source File: FtpTransferManager.java    From desktop with GNU General Public License v3.0 6 votes vote down vote up
public Map<String, RemoteFile> getDirectoryList(String folderPath) throws StorageException{
    
    try {
        Map<String, RemoteFile> files = new HashMap<String, RemoteFile>();
        FTPFile[] ftpFiles = ftp.listFiles(getConnection().getPath() + "/" + folderPath);

        for (FTPFile f : ftpFiles) {
            files.put(folderPath+"/"+f.getName(), new RemoteFile(folderPath+"/"+f.getName(), f.getSize(), f));
            if (f.isDirectory()) {
                files.putAll(getDirectoryList(folderPath+"/"+f.getName()));
            }
        }

        return files;
    } catch (IOException ex) {
        logger.error("Unable to list FTP directory.", ex);
        throw new StorageException(ex);
    }
}
 
Example 10
Source File: FTPFileSystem.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * Convert the file information in FTPFile to a {@link FileStatus} object. *
 * 
 * @param ftpFile
 * @param parentPath
 * @return FileStatus
 */
private FileStatus getFileStatus(FTPFile ftpFile, Path parentPath) {
  long length = ftpFile.getSize();
  boolean isDir = ftpFile.isDirectory();
  int blockReplication = 1;
  // Using default block size since there is no way in FTP client to know of
  // block sizes on server. The assumption could be less than ideal.
  long blockSize = DEFAULT_BLOCK_SIZE;
  long modTime = ftpFile.getTimestamp().getTimeInMillis();
  long accessTime = 0;
  FsPermission permission = getPermissions(ftpFile);
  String user = ftpFile.getUser();
  String group = ftpFile.getGroup();
  Path filePath = new Path(parentPath, ftpFile.getName());
  return new FileStatus(length, isDir, blockReplication, blockSize, modTime,
      accessTime, permission, user, group, filePath.makeQualified(this));
}
 
Example 11
Source File: FTPFileSystem.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Convert the file information in FTPFile to a {@link FileStatus} object. *
 * 
 * @param ftpFile
 * @param parentPath
 * @return FileStatus
 */
private FileStatus getFileStatus(FTPFile ftpFile, Path parentPath) {
  long length = ftpFile.getSize();
  boolean isDir = ftpFile.isDirectory();
  int blockReplication = 1;
  // Using default block size since there is no way in FTP client to know of
  // block sizes on server. The assumption could be less than ideal.
  long blockSize = DEFAULT_BLOCK_SIZE;
  long modTime = ftpFile.getTimestamp().getTimeInMillis();
  long accessTime = 0;
  FsPermission permission = getPermissions(ftpFile);
  String user = ftpFile.getUser();
  String group = ftpFile.getGroup();
  Path filePath = new Path(parentPath, ftpFile.getName());
  return new FileStatus(length, isDir, blockReplication, blockSize, modTime,
      accessTime, permission, user, group, filePath.makeQualified(this));
}
 
Example 12
Source File: FTPFileSystem.java    From hadoop-gpu with Apache License 2.0 6 votes vote down vote up
/**
 * Convert the file information in FTPFile to a {@link FileStatus} object. *
 * 
 * @param ftpFile
 * @param parentPath
 * @return FileStatus
 */
private FileStatus getFileStatus(FTPFile ftpFile, Path parentPath) {
  long length = ftpFile.getSize();
  boolean isDir = ftpFile.isDirectory();
  int blockReplication = 1;
  // Using default block size since there is no way in FTP client to know of
  // block sizes on server. The assumption could be less than ideal.
  long blockSize = DEFAULT_BLOCK_SIZE;
  long modTime = ftpFile.getTimestamp().getTimeInMillis();
  long accessTime = 0;
  FsPermission permission = getPermissions(ftpFile);
  String user = ftpFile.getUser();
  String group = ftpFile.getGroup();
  Path filePath = new Path(parentPath, ftpFile.getName());
  return new FileStatus(length, isDir, blockReplication, blockSize, modTime,
      accessTime, permission, user, group, filePath.makeQualified(this));
}
 
Example 13
Source File: FTPClientWrapper.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * Function to list files of a directory on an FTP server.
 *
 * @param filePath file path to list files
 * @return list of files
 * @throws IOException if listing set of files fails
 */
public List<FTPFile> listFiles(String filePath) throws IOException {
    List<FTPFile> files = new ArrayList<>();
    try{
        FTPFile[] filesAndDirectories = ftpClient.listFiles(filePath);

        for (FTPFile file : filesAndDirectories) {
            if (!file.isDirectory()) {
                files.add(file);
            }
        }
    } catch (IOException ex){
        throw new IOException("Error occurred while listing files", ex);
    }
    return files;
}
 
Example 14
Source File: FtpResponse.java    From anthelion with Apache License 2.0 5 votes vote down vote up
private byte[] list2html(List list, String path, boolean includeDotDot) {

    //StringBuffer x = new StringBuffer("<!doctype html public \"-//ietf//dtd html//en\"><html><head>");
    StringBuffer x = new StringBuffer("<html><head>");
    x.append("<title>Index of "+path+"</title></head>\n");
    x.append("<body><h1>Index of "+path+"</h1><pre>\n");

    if (includeDotDot) {
      x.append("<a href='../'>../</a>\t-\t-\t-\n");
    }

    for (int i=0; i<list.size(); i++) {
      FTPFile f = (FTPFile) list.get(i);
      String name = f.getName();
      String time = HttpDateFormat.toString(f.getTimestamp());
      if (f.isDirectory()) {
        // some ftp server LIST "." and "..", we skip them here
        if (name.equals(".") || name.equals(".."))
          continue;
        x.append("<a href='"+name+"/"+"'>"+name+"/</a>\t");
        x.append(time+"\t-\n");
      } else if (f.isFile()) {
        x.append("<a href='"+name+    "'>"+name+"</a>\t");
        x.append(time+"\t"+f.getSize()+"\n");
      } else {
        // ignore isSymbolicLink()
        // ignore isUnknown()
      }
    }

    x.append("</pre></body></html>\n");

    return new String(x).getBytes();
  }
 
Example 15
Source File: FTPOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
private List<Info> list(URI parentUri, FTPClient ftp, ListParameters params) throws IOException {
	if (Thread.currentThread().isInterrupted()) {
		throw new IOException(FileOperationMessages.getString("IOperationHandler.interrupted")); //$NON-NLS-1$
	}
	Info rootInfo = info(parentUri, ftp);
	if (rootInfo == null) {
		throw new FileNotFoundException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.file_not_found"), parentUri.toString())); //$NON-NLS-1$
	}
	if (parentUri.toString().endsWith(URIUtils.PATH_SEPARATOR) && !rootInfo.isDirectory()) {
		throw new FileNotFoundException(format(FileOperationMessages.getString("IOperationHandler.not_a_directory"), parentUri)); //$NON-NLS-1$
	}
	if (!rootInfo.isDirectory()) {
		return Arrays.asList(rootInfo);
	} else {
		FTPFile[] files = ftp.listFiles(getPath(parentUri));
		List<Info> result = new ArrayList<Info>(files.length);
		for (FTPFile file: files) {
			if ((file != null) && !file.getName().equals(URIUtils.CURRENT_DIR_NAME) && !file.getName().equals(URIUtils.PARENT_DIR_NAME)) {
				Info child = info(file, parentUri, null);
				result.add(child);
				if (params.isRecursive() && file.isDirectory()) {
					result.addAll(list(child.getURI(), ftp, params));
				}
			}
		}
		return result;
	}
}
 
Example 16
Source File: PooledFTPOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
private List<Info> list(URI parentUri, FTPClient ftp, ListParameters params) throws IOException {
	if (Thread.currentThread().isInterrupted()) {
		throw new IOException(FileOperationMessages.getString("IOperationHandler.interrupted")); //$NON-NLS-1$
	}
	Info rootInfo = info(parentUri, ftp);
	if (rootInfo == null) {
		throw new FileNotFoundException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.file_not_found"), parentUri.toString())); //$NON-NLS-1$
	}
	if (parentUri.toString().endsWith(URIUtils.PATH_SEPARATOR) && !rootInfo.isDirectory()) {
		throw new FileNotFoundException(format(FileOperationMessages.getString("IOperationHandler.not_a_directory"), parentUri)); //$NON-NLS-1$
	}
	if (!rootInfo.isDirectory()) {
		return Arrays.asList(rootInfo);
	} else {
		FTPFile[] files = listFiles(getPath(parentUri), ftp);
		List<Info> result = new ArrayList<Info>(files.length);
		for (FTPFile file: files) {
			if ((file != null) && !file.getName().equals(URIUtils.CURRENT_DIR_NAME) && !file.getName().equals(URIUtils.PARENT_DIR_NAME)) {
				Info child = info(file, parentUri, null);
				result.add(child);
				if (params.isRecursive() && file.isDirectory()) {
					result.addAll(list(child.getURI(), ftp, params));
				}
			}
		}
		return result;
	}
}
 
Example 17
Source File: FtpConnection.java    From spring-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 把指定路径中的 FTPFile 类型文件,转换为 JFtpFile
 *
 * @param ftpFile             FTPFile 类型的文件
 * @param remoteDirectoryPath 文件所在目录
 * @return
 * @throws IOException
 */
private JFTPFile toFtpFile(FTPFile ftpFile, String remoteDirectoryPath) throws IOException {

    String name = ftpFile.getName();
    long fileSize = ftpFile.getSize();
    String fullPath = String.format("%s%s%s", remoteDirectoryPath, "/", ftpFile.getName()); // ftp 路径的分隔符都为 "/" ,所以 File.pathSeparator 不对
    long mTime = ftpFile.getTimestamp().getTime().getTime();
    //  logger.info(" 1.  getTimestamp : "+new DateTime(mTime).withZone(DateTimeZone.forTimeZone(TimeZone.getDefault())));
    // logger.info(" 2.  getTimestamp : "+ftpFile.getTimestamp().getTimeInMillis());

    return new JFTPFile(name, fileSize, fullPath, mTime, ftpFile.isDirectory());
}
 
Example 18
Source File: FtpResponse.java    From nutch-htmlunit with Apache License 2.0 5 votes vote down vote up
private byte[] list2html(List<FTPFile> list, String path, boolean includeDotDot) {

    //StringBuffer x = new StringBuffer("<!doctype html public \"-//ietf//dtd html//en\"><html><head>");
    StringBuffer x = new StringBuffer("<html><head>");
    x.append("<title>Index of "+path+"</title></head>\n");
    x.append("<body><h1>Index of "+path+"</h1><pre>\n");

    if (includeDotDot) {
      x.append("<a href='../'>../</a>\t-\t-\t-\n");
    }

    for (int i=0; i<list.size(); i++) {
      FTPFile f = (FTPFile) list.get(i);
      String name = f.getName();
      String time = HttpDateFormat.toString(f.getTimestamp());
      if (f.isDirectory()) {
        // some ftp server LIST "." and "..", we skip them here
        if (name.equals(".") || name.equals(".."))
          continue;
        x.append("<a href='"+name+"/"+"'>"+name+"/</a>\t");
        x.append(time+"\t-\n");
      } else if (f.isFile()) {
        x.append("<a href='"+name+    "'>"+name+"</a>\t");
        x.append(time+"\t"+f.getSize()+"\n");
      } else {
        // ignore isSymbolicLink()
        // ignore isUnknown()
      }
    }

    x.append("</pre></body></html>\n");

    return new String(x).getBytes();
  }
 
Example 19
Source File: FtpFileSystem.java    From iaf with Apache License 2.0 5 votes vote down vote up
FTPFilePathIterator(String folder, FTPFile filesArr[]) {
	prefix = folder != null ? folder + "/" : "";
	files = new ArrayList<FTPFile>();
	for (FTPFile ftpFile : filesArr) {
		if(!ftpFile.isDirectory()) {
			ftpFile.setName(prefix + ftpFile.getName());
			files.add(ftpFile);
		}
	}
}
 
Example 20
Source File: FTPTransfer.java    From nifi with Apache License 2.0 4 votes vote down vote up
private List<FileInfo> getListing(final String path, final int depth, final int maxResults) throws IOException {
    final List<FileInfo> listing = new ArrayList<>();
    if (maxResults < 1) {
        return listing;
    }

    if (depth >= 100) {
        logger.warn(this + " had to stop recursively searching directories at a recursive depth of " + depth + " to avoid memory issues");
        return listing;
    }

    final boolean ignoreDottedFiles = ctx.getProperty(FileTransfer.IGNORE_DOTTED_FILES).asBoolean();
    final boolean recurse = ctx.getProperty(FileTransfer.RECURSIVE_SEARCH).asBoolean();
    final boolean symlink = ctx.getProperty(FileTransfer.FOLLOW_SYMLINK).asBoolean();
    final String fileFilterRegex = ctx.getProperty(FileTransfer.FILE_FILTER_REGEX).getValue();
    final Pattern pattern = (fileFilterRegex == null) ? null : Pattern.compile(fileFilterRegex);
    final String pathFilterRegex = ctx.getProperty(FileTransfer.PATH_FILTER_REGEX).getValue();
    final Pattern pathPattern = (!recurse || pathFilterRegex == null) ? null : Pattern.compile(pathFilterRegex);
    final String remotePath = ctx.getProperty(FileTransfer.REMOTE_PATH).evaluateAttributeExpressions().getValue();

    // check if this directory path matches the PATH_FILTER_REGEX
    boolean pathFilterMatches = true;
    if (pathPattern != null) {
        Path reldir = path == null ? Paths.get(".") : Paths.get(path);
        if (remotePath != null) {
            reldir = Paths.get(remotePath).relativize(reldir);
        }
        if (reldir != null && !reldir.toString().isEmpty()) {
            if (!pathPattern.matcher(reldir.toString().replace("\\", "/")).matches()) {
                pathFilterMatches = false;
            }
        }
    }

    final FTPClient client = getClient(null);

    int count = 0;
    final FTPFile[] files;

    if (path == null || path.trim().isEmpty()) {
        files = client.listFiles(".");
    } else {
        files = client.listFiles(path);
    }
    if (files.length == 0 && path != null && !path.trim().isEmpty()) {
        // throw exception if directory doesn't exist
        final boolean cdSuccessful = setWorkingDirectory(path);
        if (!cdSuccessful) {
            throw new IOException("Cannot list files for non-existent directory " + path);
        }
    }

    for (final FTPFile file : files) {
        final String filename = file.getName();
        if (filename.equals(".") || filename.equals("..")) {
            continue;
        }

        if (ignoreDottedFiles && filename.startsWith(".")) {
            continue;
        }

        final File newFullPath = new File(path, filename);
        final String newFullForwardPath = newFullPath.getPath().replace("\\", "/");

        // if is a directory and we're supposed to recurse
        // OR if is a link and we're supposed to follow symlink
        if ((recurse && file.isDirectory()) || (symlink && file.isSymbolicLink())) {
            try {
                listing.addAll(getListing(newFullForwardPath, depth + 1, maxResults - count));
            } catch (final IOException e) {
                logger.error("Unable to get listing from " + newFullForwardPath + "; skipping", e);
            }
        }

        // if is not a directory and is not a link and it matches
        // FILE_FILTER_REGEX - then let's add it
        if (!file.isDirectory() && !file.isSymbolicLink() && pathFilterMatches) {
            if (pattern == null || pattern.matcher(filename).matches()) {
                listing.add(newFileInfo(file, path));
                count++;
            }
        }

        if (count >= maxResults) {
            break;
        }
    }

    return listing;
}