Class ContentTypeUtils


  • public final class ContentTypeUtils
    extends Object
    Performs content type detection by utilizing the Apache Tika framework.
    • Method Detail

      • detectMimeType

        public static String detectMimeType​(InputStream contentInputStream)
                                     throws IOException
        Makes a best attempt to detect the MIME type of the provided input stream's content.

        This method returns application/octet-stream when no more specific type could be detected.

        The given input stream will be closed by this method.

        Parameters:
        contentInputStream - the input stream to be used for content type detection.
        Returns:
        the detected MIME type (e.g. "image/png" for a PNG image).
        Throws:
        NullPointerException - if the passed input stream is null
        IOException - When the input stream could not be read.
      • detectMimeType

        public static String detectMimeType​(String fileName)
        Makes a best attempt to detect the MIME type for the given file name.

        This method returns application/octet-stream when no more specific type could be detected.

        Parameters:
        fileName - Name of the file to detect the MIME type for.
        Returns:
        The detected MIME type, e.g. image/png for a PNG image. If detection fails, this method returns application/octet-stream.