Class MimeTypesUtils

java.lang.Object
com.axelor.common.MimeTypesUtils

public class MimeTypesUtils extends Object
This class defines from static helper methods to deal with Mime Types.
  • Constructor Details

    • MimeTypesUtils

      public MimeTypesUtils()
  • Method Details

    • getContentType

      public static String getContentType(File file)
      Returns the content type from the file.
      Parameters:
      file - the file
      Returns:
      detected content type if it is a supported format or "application/octet-stream" if it is an unsupported format
    • getContentType

      public static String getContentType(Path path)
      Returns the content type from the path.
      Parameters:
      path - the path
      Returns:
      detected content type if it is a supported format or "application/octet-stream" if it is an unsupported format
    • getContentType

      public static String getContentType(String fileName)
      Detects the content type of the given file name. The type detection is based on known file name extensions.
      Parameters:
      fileName - the file name
      Returns:
      detected content type if it is a supported format or "application/octet-stream" if it is an unsupported format
    • getExtensionContentType

      public static String getExtensionContentType(String extension)
      Returns the content type from the file extension.
      Parameters:
      extension - the extension of the file (e.g., "doc")
      Returns:
      detected content type if it is a supported format or "application/octet-stream" if it is an unsupported format
    • getContentType

      public static String getContentType(File file, String fileName)
      Returns the content type from the file and file name.
      Parameters:
      file - the file, can be null
      fileName - the file name or extension of the file (e.g., "doc")
      Returns:
      detected content type if it is a supported format or "application/octet-stream" if it is an unsupported format
    • getContentType

      public static String getContentType(InputStream inputStream, String fileName)
      Detects the content type based on the input stream and file name.

      This method is designed to be safe for all stream types. It guarantees that the provided InputStream is never consumed or corrupted.

      If the stream supports mark/reset (e.g., BufferedInputStream), this method will peek at the file header to determine the MIME type accurately, and then reset the stream to its original position.

      If the stream is null or does not support mark/reset (e.g., a raw FileInputStream), deep inspection is skipped to avoid consuming data. In this case, detection relies solely on the provided fileName extension.

      Parameters:
      inputStream - the input stream to inspect (can be null or raw)
      fileName - the file name or extension of the file (e.g., "doc")
      Returns:
      the detected content type, or "application/octet-stream" if detection fails