类 MediaType

java.lang.Object
cn.taketoday.util.MimeType
cn.taketoday.http.MediaType
所有已实现的接口:
Serializable, Comparable<cn.taketoday.util.MimeType>

public class MediaType extends cn.taketoday.util.MimeType implements Serializable
A subclass of MimeType that adds support for quality parameters as defined in the HTTP specification.
从以下版本开始:
2.1.7 2019-12-08 20:02
作者:
Arjen Poutsma, Juergen Hoeller, Rossen Stoyanchev, Sebastien Deleuze, Kazuki Shimizu, Sam Brannen, Harry Yang
另请参阅:
  • 字段详细资料

    • serialVersionUID

      private static final long serialVersionUID
      另请参阅:
    • ALL

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

      public static final String ALL_VALUE
      A String equivalent of ALL.
      另请参阅:
    • APPLICATION_ATOM_XML

      public static final MediaType APPLICATION_ATOM_XML
      Public constant media type for application/atom+xml.
    • APPLICATION_ATOM_XML_VALUE

      public static final String APPLICATION_ATOM_XML_VALUE
      A String equivalent of APPLICATION_ATOM_XML.
      另请参阅:
    • APPLICATION_CBOR

      public static final MediaType APPLICATION_CBOR
      Public constant media type for application/cbor.
    • APPLICATION_CBOR_VALUE

      public static final String APPLICATION_CBOR_VALUE
      A String equivalent of APPLICATION_CBOR.
      另请参阅:
    • APPLICATION_FORM_URLENCODED

      public static final MediaType APPLICATION_FORM_URLENCODED
      Public constant media type for application/x-www-form-urlencoded.
    • APPLICATION_FORM_URLENCODED_VALUE

      public static final String APPLICATION_FORM_URLENCODED_VALUE
      A String equivalent of APPLICATION_FORM_URLENCODED.
      另请参阅:
    • APPLICATION_JSON

      public static final MediaType APPLICATION_JSON
      Public constant media type for application/json.
    • APPLICATION_JSON_VALUE

      public static final String APPLICATION_JSON_VALUE
      A String equivalent of APPLICATION_JSON.
      另请参阅:
    • APPLICATION_JSON_UTF8_VALUE

      public static final String APPLICATION_JSON_UTF8_VALUE
      另请参阅:
    • APPLICATION_OCTET_STREAM

      public static final MediaType APPLICATION_OCTET_STREAM
      Public constant media type for application/octet-stream.
    • APPLICATION_OCTET_STREAM_VALUE

      public static final String APPLICATION_OCTET_STREAM_VALUE
      A String equivalent of APPLICATION_OCTET_STREAM.
      另请参阅:
    • APPLICATION_PDF

      public static final MediaType APPLICATION_PDF
      Public constant media type for application/pdf.
    • APPLICATION_PDF_VALUE

      public static final String APPLICATION_PDF_VALUE
      A String equivalent of APPLICATION_PDF.
      另请参阅:
    • APPLICATION_PROBLEM_JSON

      public static final MediaType APPLICATION_PROBLEM_JSON
      Public constant media type for application/problem+json.*
      另请参阅:
    • APPLICATION_PROBLEM_JSON_VALUE

      public static final String APPLICATION_PROBLEM_JSON_VALUE
      A String equivalent of APPLICATION_PROBLEM_JSON.
      另请参阅:
    • APPLICATION_PROBLEM_XML

      public static final MediaType APPLICATION_PROBLEM_XML
      Public constant media type for application/problem+xml.
      另请参阅:
    • APPLICATION_PROBLEM_XML_VALUE

      public static final String APPLICATION_PROBLEM_XML_VALUE
      A String equivalent of APPLICATION_PROBLEM_XML.
      另请参阅:
    • APPLICATION_RSS_XML

      public static final MediaType APPLICATION_RSS_XML
      Public constant media type for application/rss+xml.
    • APPLICATION_RSS_XML_VALUE

      public static final String APPLICATION_RSS_XML_VALUE
      A String equivalent of APPLICATION_RSS_XML.
      另请参阅:
    • APPLICATION_NDJSON

      public static final MediaType APPLICATION_NDJSON
      Public constant media type for application/x-ndjson.
    • APPLICATION_NDJSON_VALUE

      public static final String APPLICATION_NDJSON_VALUE
      A String equivalent of APPLICATION_NDJSON.
      另请参阅:
    • APPLICATION_STREAM_JSON

      public static final MediaType APPLICATION_STREAM_JSON
      Public constant media type for application/stream+json.
    • APPLICATION_STREAM_JSON_VALUE

      public static final String APPLICATION_STREAM_JSON_VALUE
      A String equivalent of APPLICATION_STREAM_JSON.
      另请参阅:
    • APPLICATION_XHTML_XML

      public static final MediaType APPLICATION_XHTML_XML
      Public constant media type for application/xhtml+xml.
    • APPLICATION_XHTML_XML_VALUE

      public static final String APPLICATION_XHTML_XML_VALUE
      A String equivalent of APPLICATION_XHTML_XML.
      另请参阅:
    • APPLICATION_XML

      public static final MediaType APPLICATION_XML
      Public constant media type for application/xml.
    • APPLICATION_XML_VALUE

      public static final String APPLICATION_XML_VALUE
      A String equivalent of APPLICATION_XML.
      另请参阅:
    • IMAGE_GIF

      public static final MediaType IMAGE_GIF
      Public constant media type for image/gif.
    • IMAGE_GIF_VALUE

      public static final String IMAGE_GIF_VALUE
      A String equivalent of IMAGE_GIF.
      另请参阅:
    • IMAGE_JPEG

      public static final MediaType IMAGE_JPEG
      Public constant media type for image/jpeg.
    • IMAGE_JPEG_VALUE

      public static final String IMAGE_JPEG_VALUE
      A String equivalent of IMAGE_JPEG.
      另请参阅:
    • IMAGE_PNG

      public static final MediaType IMAGE_PNG
      Public constant media type for image/png.
    • IMAGE_PNG_VALUE

      public static final String IMAGE_PNG_VALUE
      A String equivalent of IMAGE_PNG.
      另请参阅:
    • MULTIPART_FORM_DATA

      public static final MediaType MULTIPART_FORM_DATA
      Public constant media type for multipart/form-data.
    • MULTIPART_FORM_DATA_VALUE

      public static final String MULTIPART_FORM_DATA_VALUE
      A String equivalent of MULTIPART_FORM_DATA.
      另请参阅:
    • MULTIPART_MIXED

      public static final MediaType MULTIPART_MIXED
      Public constant media type for multipart/mixed.
    • MULTIPART_MIXED_VALUE

      public static final String MULTIPART_MIXED_VALUE
      A String equivalent of MULTIPART_MIXED.
      另请参阅:
    • TEXT_EVENT_STREAM

      public static final MediaType TEXT_EVENT_STREAM
      Public constant media type for text/event-stream.
      另请参阅:
    • TEXT_EVENT_STREAM_VALUE

      public static final String TEXT_EVENT_STREAM_VALUE
      A String equivalent of TEXT_EVENT_STREAM.
      另请参阅:
    • TEXT_HTML

      public static final MediaType TEXT_HTML
      Public constant media type for text/html.
    • TEXT_HTML_VALUE

      public static final String TEXT_HTML_VALUE
      A String equivalent of TEXT_HTML.
      另请参阅:
    • TEXT_MARKDOWN

      public static final MediaType TEXT_MARKDOWN
      Public constant media type for text/markdown.
    • TEXT_MARKDOWN_VALUE

      public static final String TEXT_MARKDOWN_VALUE
      A String equivalent of TEXT_MARKDOWN.
      另请参阅:
    • TEXT_PLAIN

      public static final MediaType TEXT_PLAIN
      Public constant media type for text/plain.
    • TEXT_PLAIN_VALUE

      public static final String TEXT_PLAIN_VALUE
      A String equivalent of TEXT_PLAIN.
      另请参阅:
    • TEXT_XML

      public static final MediaType TEXT_XML
      Public constant media type for text/xml.
    • TEXT_XML_VALUE

      public static final String TEXT_XML_VALUE
      A String equivalent of TEXT_XML.
      另请参阅:
    • PARAM_QUALITY_FACTOR

      private static final String PARAM_QUALITY_FACTOR
      另请参阅:
    • QUALITY_VALUE_COMPARATOR

      public static final Comparator<MediaType> QUALITY_VALUE_COMPARATOR
      Comparator used by sortByQualityValue(List).
    • SPECIFICITY_COMPARATOR

      public static final Comparator<MediaType> SPECIFICITY_COMPARATOR
      Comparator used by sortBySpecificity(List).
  • 构造器详细资料

    • MediaType

      public MediaType(String type)
      Create a new MediaType for the given primary type.

      The subtype is set to "*", parameters empty.

      参数:
      type - the primary type
      抛出:
      IllegalArgumentException - if any of the parameters contain illegal characters
    • MediaType

      public MediaType(String type, String subtype)
      Create a new MediaType for the given primary type and subtype.

      The parameters are empty.

      参数:
      type - the primary type
      subtype - the subtype
      抛出:
      IllegalArgumentException - if any of the parameters contain illegal characters
    • MediaType

      public MediaType(String type, String subtype, Charset charset)
      Create a new MediaType for the given type, subtype, and character set.
      参数:
      type - the primary type
      subtype - the subtype
      charset - the character set
      抛出:
      IllegalArgumentException - if any of the parameters contain illegal characters
    • MediaType

      public MediaType(String type, String subtype, double qualityValue)
      Create a new MediaType for the given type, subtype, and quality value.
      参数:
      type - the primary type
      subtype - the subtype
      qualityValue - the quality value
      抛出:
      IllegalArgumentException - if any of the parameters contain illegal characters
    • MediaType

      public MediaType(MediaType other, Charset charset)
      Copy-constructor that copies the type, subtype and parameters of the given MediaType, and allows to set the specified character set.
      参数:
      other - the other media type
      charset - the character set
      抛出:
      IllegalArgumentException - if any of the parameters contain illegal characters
    • MediaType

      public MediaType(MediaType other, @Nullable Map<String,String> parameters)
      Copy-constructor that copies the type and subtype of the given MediaType, and allows for different parameters.
      参数:
      other - the other media type
      parameters - the parameters, may be null
      抛出:
      IllegalArgumentException - if any of the parameters contain illegal characters
    • MediaType

      public MediaType(String type, String subtype, @Nullable Map<String,String> parameters)
      Create a new MediaType for the given type, subtype, and parameters.
      参数:
      type - the primary type
      subtype - the subtype
      parameters - the parameters, may be null
      抛出:
      IllegalArgumentException - if any of the parameters contain illegal characters
  • 方法详细资料

    • checkParameters

      protected void checkParameters(String attribute, String value)
      覆盖:
      checkParameters 在类中 cn.taketoday.util.MimeType
    • getQualityValue

      public double getQualityValue()
      Return the quality factor, as indicated by a q parameter, if any. Defaults to 1.0.
      返回:
      the quality factor as double value
    • includes

      public boolean includes(MediaType other)
      Indicate whether this MediaType includes the given media type.

      For instance, text/* includes text/plain and text/html, and application/*+xml includes application/soap+xml, etc. This method is not symmetric.

      Simply calls MimeType.includes(MimeType) but declared with a MediaType parameter for binary backwards compatibility.

      参数:
      other - the reference media type with which to compare
      返回:
      true if this media type includes the given media type; false otherwise
    • isCompatibleWith

      public boolean isCompatibleWith(MediaType other)
      Indicate whether this MediaType is compatible with the given media type.

      For instance, text/* is compatible with text/plain, text/html, and vice versa. In effect, this method is similar to includes(cn.taketoday.http.MediaType), except that it is symmetric.

      Simply calls MimeType.isCompatibleWith(MimeType) but declared with a MediaType parameter for binary backwards compatibility.

      参数:
      other - the reference media type with which to compare
      返回:
      true if this media type is compatible with the given media type; false otherwise
    • copyQualityValue

      public MediaType copyQualityValue(MediaType mediaType)
      Return a replica of this instance with the quality value of the given MediaType.
      返回:
      the same instance if the given MediaType doesn't have a quality value, or a new one otherwise
    • removeQualityValue

      public MediaType removeQualityValue()
      Return a replica of this instance with its quality value removed.
      返回:
      the same instance if the media type doesn't contain a quality value, or a new one otherwise
    • valueOf

      public static MediaType valueOf(String value)
      Parse the given String value into a MediaType object
      参数:
      value - the string to parse
      抛出:
      InvalidMediaTypeException - if the media type value cannot be parsed
      另请参阅:
    • parseMediaType

      public static MediaType parseMediaType(String mediaType)
      Parse the given String into a single MediaType.
      参数:
      mediaType - the string to parse
      返回:
      the media type
      抛出:
      InvalidMediaTypeException - if the media type value cannot be parsed
    • parseMediaTypes

      public static List<MediaType> parseMediaTypes(String mediaTypes)
      Parse the comma-separated string into a list of MediaType objects.

      This method can be used to parse an Accept or Content-Type header.

      参数:
      mediaTypes - the string to parse
      返回:
      the list of media types
      抛出:
      InvalidMediaTypeException - if the media type value cannot be parsed
    • parseMediaTypes

      public static List<MediaType> parseMediaTypes(List<String> mediaTypes)
      Parse the given list of (potentially) comma-separated strings into a list of MediaType objects.

      This method can be used to parse an Accept or Content-Type header.

      参数:
      mediaTypes - the string to parse
      返回:
      the list of media types
      抛出:
      InvalidMediaTypeException - if the media type value cannot be parsed
    • asMediaTypes

      public static List<MediaType> asMediaTypes(List<cn.taketoday.util.MimeType> mimeTypes)
      Re-create the given mime types as media types.
    • asMediaType

      public static MediaType asMediaType(cn.taketoday.util.MimeType mimeType)
      Re-create the given mime type as a media type.
    • toString

      public static String toString(Collection<MediaType> mediaTypes)
      Return a string representation of the given list of MediaType objects.

      This method can be used to for an Accept or Content-Type header.

      参数:
      mediaTypes - the media types to create a string representation for
      返回:
      the string representation
    • isMoreSpecific

      public boolean isMoreSpecific(cn.taketoday.util.MimeType other)
      Indicates whether this MediaType more specific than the given type.
      1. if this media type has a quality factor higher than the other, then this method returns true.
      2. if this media type has a quality factor lower than the other, then this method returns false.
      3. if this mime type has a wildcard type, and the other does not, then this method returns false.
      4. if this mime type does not have a wildcard type, and the other does, then this method returns true.
      5. if this mime type has a wildcard type, and the other does not, then this method returns false.
      6. if this mime type does not have a wildcard type, and the other does, then this method returns true.
      7. if the two mime types have identical type and subtype, then the mime type with the most parameters is more specific than the other.
      8. Otherwise, this method returns false.
      覆盖:
      isMoreSpecific 在类中 cn.taketoday.util.MimeType
      参数:
      other - the MimeType to be compared
      返回:
      the result of the comparison
      从以下版本开始:
      4.0
      另请参阅:
    • isLessSpecific

      public boolean isLessSpecific(cn.taketoday.util.MimeType other)
      Indicates whether this MediaType more specific than the given type.
      1. if this media type has a quality factor higher than the other, then this method returns false.
      2. if this media type has a quality factor lower than the other, then this method returns true.
      3. if this mime type has a wildcard type, and the other does not, then this method returns true.
      4. if this mime type does not have a wildcard type, and the other does, then this method returns false.
      5. if this mime type has a wildcard type, and the other does not, then this method returns true.
      6. if this mime type does not have a wildcard type, and the other does, then this method returns false.
      7. if the two mime types have identical type and subtype, then the mime type with the least parameters is less specific than the other.
      8. Otherwise, this method returns false.
      覆盖:
      isLessSpecific 在类中 cn.taketoday.util.MimeType
      参数:
      other - the MimeType to be compared
      返回:
      the result of the comparison
      从以下版本开始:
      4.0
      另请参阅:
    • sortBySpecificity

      public static void sortBySpecificity(List<MediaType> mediaTypes)
      Sorts the given list of MediaType objects by specificity.

      Given two media types:

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

      For example:

      audio/basic < audio/* < */*
      audio/* < audio/*;q=0.7; audio/*;q=0.3
      audio/basic;level=1 < audio/basic
      audio/basic == text/html
      audio/basic == audio/wave
      参数:
      mediaTypes - the list of media types to be sorted
      另请参阅:
    • sortByQualityValue

      public static void sortByQualityValue(List<MediaType> mediaTypes)
      Sorts the given list of MediaType objects by quality value.

      Given two media types:

      1. if the two media types have different quality value, then the media type with the highest quality value is ordered before the other.
      2. if either media type has a wildcard type, then the media type without the wildcard is ordered before the other.
      3. if the two media types have different types, then they are considered equal and remain their current order.
      4. if either media type has a wildcard subtype, then the media type without the wildcard is sorted before the other.
      5. if the two media types have different subtypes, then they are considered equal and remain their current order.
      6. if the two media types have a different amount of parameters, then the media type with the most parameters is ordered before the other.
      参数:
      mediaTypes - the list of media types to be sorted
      另请参阅:
    • sortBySpecificityAndQuality

      public static void sortBySpecificityAndQuality(List<MediaType> mediaTypes)
      Sorts the given list of MediaType objects by specificity as the primary criteria and quality value the secondary.
      另请参阅:
    • fromResource

      @Nullable public static MediaType fromResource(@Nullable cn.taketoday.core.io.Resource resource)
      Determine a media type for the given resource, if possible.
      参数:
      resource - the resource to introspect
      返回:
      the corresponding media type, or null if none found
    • fromFileName

      @Nullable public static MediaType fromFileName(String filename)
      Determine a media type for the given file name, if possible.
      参数:
      filename - the file name plus extension
      返回:
      the corresponding media type, or null if none found