Class CustomMimeTypesFileTypeDetector
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.
-
Constructor Summary
ConstructorsConstructorDescriptionCreate 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 -
Method Summary
Modifier and TypeMethodDescriptionprobeContentType(String path) probeContentType(URI uri) Guess the mime-type of a given URI.probeContentType(Path path) Guess the mime-type of a given path.@Nullable StringGuess the mime-type of a given URI from its file name only, whatever its scheme: from the mappings of this detector, then fromURLConnection.guessContentTypeFromName(String).
-
Constructor Details
-
CustomMimeTypesFileTypeDetector
public 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
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 Details
-
probeContentType
Guess the mime-type of a given path.- Specified by:
probeContentTypein classFileTypeDetector- 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
-
probeContentType
Guess the mime-type of a given URI.For a URI with a scheme other than
file, such ashttps, the mime-type is guessed from the file name only, asprobeContentTypeFromName(URI)does. The local file system is not accessed for such URIs. For afileURI, or a URI without a scheme, the file itself can also be read to guess its mime-type.- 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.
-
probeContentTypeFromName
Guess the mime-type of a given URI from its file name only, whatever its scheme: from the mappings of this detector, then fromURLConnection.guessContentTypeFromName(String). The local file system is never accessed, and the path of the URI is not required to be a valid local path.- Parameters:
uri- the URI to the file whose mime-type we want to retrieve.- Returns:
- a string representing the mime-type of the file named by the URI parameter,
or
nullif the URI has no file name or no mapping was found.
-