org.springframework.util

Class MimeTypeUtils

  • java.lang.Object
    • org.springframework.util.MimeTypeUtils


  • public abstract class MimeTypeUtils
    extends java.lang.Object
    Miscellaneous MimeType utility methods.
    Since:
    4.0
    Author:
    Arjen Poutsma, Rossen Stoyanchev
    • Field Detail

      • ALL

        public static final MimeType ALL
        Public constant mime type that includes all media ranges (i.e. "*/*").
      • APPLICATION_JSON

        public static final MimeType APPLICATION_JSON
        Public constant mime type for application/json.
      • APPLICATION_OCTET_STREAM

        public static final MimeType APPLICATION_OCTET_STREAM
        Public constant mime type for application/octet-stream.
      • APPLICATION_XML

        public static final MimeType APPLICATION_XML
        Public constant mime type for application/xml.
      • IMAGE_GIF

        public static final MimeType IMAGE_GIF
        Public constant mime type for image/gif.
      • IMAGE_JPEG

        public static final MimeType IMAGE_JPEG
        Public constant mime type for image/jpeg.
      • IMAGE_PNG

        public static final MimeType IMAGE_PNG
        Public constant mime type for image/png.
      • TEXT_HTML

        public static final MimeType TEXT_HTML
        Public constant mime type for text/html.
      • TEXT_PLAIN

        public static final MimeType TEXT_PLAIN
        Public constant mime type for text/plain.
      • TEXT_XML

        public static final MimeType TEXT_XML
        Public constant mime type for text/xml.
    • Constructor Detail

      • MimeTypeUtils

        public MimeTypeUtils()
    • Method Detail

      • parseMimeType

        public static MimeType parseMimeType(java.lang.String mimeType)
        Parse the given String into a single MimeType.
        Parameters:
        mimeType - the string to parse
        Returns:
        the mime type
        Throws:
        InvalidMimeTypeException - if the string cannot be parsed
      • parseMimeTypes

        public static java.util.List<MimeType> parseMimeTypes(java.lang.String mimeTypes)
        Parse the given, comma-separated string into a list of MimeType objects.
        Parameters:
        mimeTypes - the string to parse
        Returns:
        the list of mime types
        Throws:
        java.lang.IllegalArgumentException - if the string cannot be parsed
      • toString

        public static java.lang.String toString(java.util.Collection<? extends MimeType> mimeTypes)
        Return a string representation of the given list of MimeType objects.
        Parameters:
        mimeTypes - the string to parse
        Returns:
        the list of mime types
        Throws:
        java.lang.IllegalArgumentException - if the String cannot be parsed
      • sortBySpecificity

        public static void sortBySpecificity(java.util.List<MimeType> mimeTypes)
        Sorts the given list of MimeType objects by specificity.

        Given two mime types:

        1. if either mime type has a wildcard type, then the mime type without the wildcard is ordered before the other.
        2. if the two mime types have different types, then they are considered equal and remain their current order.
        3. if either mime type has a wildcard subtype , then the mime type without the wildcard is sorted before the other.
        4. if the two mime types have different subtypes, then they are considered equal and remain their current order.
        5. if the two mime types have a different amount of parameters, then the mime type with the most parameters is ordered before the other.

        For example:

        audio/basic < audio/* < */*
        audio/basic;level=1 < audio/basic
        audio/basic == text/html
        audio/basic == audio/wave
        Parameters:
        mimeTypes - the list of mime types to be sorted
        See Also:
        HTTP 1.1: Semantics and Content, section 5.3.2
      • generateMultipartBoundary

        public static byte[] generateMultipartBoundary()
        Generate a random MIME boundary as bytes, often used in multipart mime types.
      • generateMultipartBoundaryString

        public static java.lang.String generateMultipartBoundaryString()
        Generate a random MIME boundary as String, often used in multipart mime types.