Class CustomMimeTypesFileTypeDetector

  • All Implemented Interfaces:

    
    public class CustomMimeTypesFileTypeDetector
    extends FileTypeDetector
                        

    Utility class to guess the mime-type of a file from its path or URI.

    As JDK's built-in doesn't recognize all possible mime-types in particular for newer file types like YAML, or returns odd mime-types like for the TypeScript .ts extension, this class allows you to define your own extension to mime-type mapping, in addition to providing a few extra ones built-in.

    When the mime-type can't be found in those mappings, or from JDK's built-in, it tries, as a fallback, to use URLConnection's capability, but it's slower as it needs to connect to the underlying URL.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      CustomMimeTypesFileTypeDetector() Create a file mime-type detector, using the internal default mappings, and fallback to JDK's built-in capabilities, when no suitable mapping is found.
      CustomMimeTypesFileTypeDetector(Map<String, String> customMapping) Create a file mime-type detector, using your own custom file extension to mime-type mappings, with a fallback to JDK's built-in capabilities, when no suitable mapping is found
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Constructor Detail

      • CustomMimeTypesFileTypeDetector

        CustomMimeTypesFileTypeDetector()
        Create a file mime-type detector, using the internal default mappings, and fallback to JDK's built-in capabilities, when no suitable mapping is found.
      • CustomMimeTypesFileTypeDetector

        CustomMimeTypesFileTypeDetector(Map<String, String> customMapping)
        Create a file mime-type detector, using your own custom file extension to mime-type mappings, with a fallback to JDK's built-in capabilities, when no suitable mapping is found
        Parameters:
        customMapping - map of custom file extension to mime-type mapping
    • Method Detail

      • probeContentType

         String probeContentType(Path path)

        Guess the mime-type of a given path.

        Parameters:
        path - the path to the file whose mime-type we want to retrieve.
        Returns:

        a string representing the mime-type of the file denoted by the path parameter, returns null if no mapping was found.

      • probeContentType

         String probeContentType(URI uri)

        Guess the mime-type of a given URI.

        Parameters:
        uri - the URI to the file whose mime-type we want to retrieve.
        Returns:

        a string representing the mime-type of the file denoted by the URI parameter, returns null if no mapping was found.