java.lang.Object
org.springframework.integration.file.support.FileUtils

public final class FileUtils extends Object
Utilities for operations on Files.
Since:
5.0
Author:
Gary Russell, Artem Bilan
  • Field Details

    • IS_POSIX

      public static final boolean IS_POSIX
  • Method Details

    • purgeUnwantedElements

      public static <F> F[] purgeUnwantedElements(F[] fileArray, Predicate<? extends F> predicate, @Nullable Comparator<? extends F> comparator)
      Remove entries from the array if the predicate returns true for an element.
      Type Parameters:
      F - the file type.
      Parameters:
      fileArray - the array.
      predicate - the predicate.
      comparator - an optional comparator to sort the results.
      Returns:
      the array of remaining elements.
      Since:
      5.0.7
    • newFileInDirectoryIfValid

      public static File newFileInDirectoryIfValid(File directory, String fileName)
      Create a new file instance representing the specified file within the given base directory, ensuring that the resulting file remains within the intended target directory. Prevent Path Traversal vulnerabilities by explicitly rejecting file names that are absolute paths or that resolve to canonical paths outside the provided base directory.
      Parameters:
      directory - The base directory where the file is intended to be placed.
      fileName - The name of the file or the relative path to be resolved against the base directory.
      Returns:
      A File object representing the securely resolved destination path.
      Throws:
      InvalidPathException - If the fileName is an absolute path or attempts to traverse outside the directory.
      UncheckedIOException - If an I/O error occurs while resolving the canonical paths.
      Since:
      5.5.21