Class FileUploadUtils

java.lang.Object
org.primefaces.util.FileUploadUtils

public class FileUploadUtils extends Object
Utilities for FileUpload components.
  • Method Details

    • requireValidFilename

      public static String requireValidFilename(String filename)
      Checks file name for validity based on Windows or Linux rules.
      Parameters:
      filename - the name of the file to check
      Returns:
      the extracted file name
    • requireValidFilePath

      public static String requireValidFilePath(String filePath, boolean mustExist)
      Check the file path to ensure its valid and prevent directory traversal security issues.
      Parameters:
      filePath - the file path to test
      mustExist - true if the directory must exist on disk, false if not
      Returns:
      the updated file path
    • validationError

      public static javax.faces.validator.ValidatorException validationError(String message)
    • containsInvalidCharacters

      public static Character containsInvalidCharacters(String s)
    • isValidType

      public static boolean isValidType(PrimeApplicationContext context, UploadedFile uploadedFile, String allowTypes, String accept)
      Check if an uploaded file meets all specifications regarding its filename and content type. It evaluates allowTypes as well as accept and uses the installed FileTypeDetector implementation. For most reliable content type checking it's recommended to plug in Apache Tika as an implementation.
      Parameters:
      context - the PrimeApplicationContext
      uploadedFile - the details of the uploaded file
      allowTypes - the Javascript regex
      accept - the accept types
      Returns:
      true, if all validations regarding filename and content type passed, false else
    • convertJavaScriptRegex

      protected static String convertJavaScriptRegex(String jsRegex)
      Converts a JavaScript regular expression like '/(\.|\/)(gif|jpeg|jpg|png)$/i' to the Java usable format '(\\.|\\/)(gif|jpeg|jpg|png)$'
      Parameters:
      jsRegex - the client side JavaScript regex
      Returns:
      the Java converted version of the regex
    • performVirusScan

      public static void performVirusScan(javax.faces.context.FacesContext facesContext, UploadedFile file) throws VirusException
      Throws:
      VirusException
    • listChunks

      public static List<Path> listChunks(Path path)
    • listChunks

      public static <T extends javax.servlet.http.HttpServletRequest> List<Path> listChunks(T request)
    • getFileUploadChunkDecoder

      public static <T extends javax.servlet.http.HttpServletRequest> FileUploadChunkDecoder<T> getFileUploadChunkDecoder(T request)
    • getChunkDir

      public static <T extends javax.servlet.http.HttpServletRequest> Path getChunkDir(T request)
    • getWebkitRelativePath

      public static <T extends javax.servlet.http.HttpServletRequest> String getWebkitRelativePath(T request)
    • formatAllowTypes

      public static String formatAllowTypes(String allowTypes)
      Formats the allowTypes regex pattern in a more human-friendly format.
      Parameters:
      allowTypes - The allowTypes regex pattern to format
      Returns:
      The allowTypes formatted in a more human-friendly format.
    • formatBytes

      public static String formatBytes(Long bytes, Locale locale)