Package org.springframework.util

# Class MimeTypeUtils

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

public abstract class MimeTypeUtils extends Object
Miscellaneous `MimeType` utility methods.

Since:
  4.0
Author:
  Arjen Poutsma, Rossen Stoyanchev, Dimitrios Liapis, Brian Clozel, Sam Brannen

## Field Summary

Fields
Modifier and Type
Field
Description
`static final MimeType`
`ALL`
Public constant mime type that includes all media ranges (i.e.
`static final String`
`ALL_VALUE`
A String equivalent of `ALL`.
`static final MimeType`
`APPLICATION_GRAPHQL`
Public constant mime type for `application/graphql+json`.
`static final String`
`APPLICATION_GRAPHQL_VALUE`
A String equivalent of `APPLICATION_GRAPHQL`.
`static final MimeType`
`APPLICATION_JSON`
Public constant mime type for `application/json`.
`static final String`
`APPLICATION_JSON_VALUE`
A String equivalent of `APPLICATION_JSON`.
`static final MimeType`
`APPLICATION_OCTET_STREAM`
Public constant mime type for `application/octet-stream`.
`static final String`
`APPLICATION_OCTET_STREAM_VALUE`
A String equivalent of `APPLICATION_OCTET_STREAM`.
`static final MimeType`
`APPLICATION_XML`
Public constant mime type for `application/xml`.
`static final String`
`APPLICATION_XML_VALUE`
A String equivalent of `APPLICATION_XML`.
`static final MimeType`
`IMAGE_GIF`
Public constant mime type for `image/gif`.
`static final String`
`IMAGE_GIF_VALUE`
A String equivalent of `IMAGE_GIF`.
`static final MimeType`
`IMAGE_JPEG`
Public constant mime type for `image/jpeg`.
`static final String`
`IMAGE_JPEG_VALUE`
A String equivalent of `IMAGE_JPEG`.
`static final MimeType`
`IMAGE_PNG`
Public constant mime type for `image/png`.
`static final String`
`IMAGE_PNG_VALUE`
A String equivalent of `IMAGE_PNG`.
`static final Comparator<MimeType>`
`SPECIFICITY_COMPARATOR`
Deprecated, for removal: This API element is subject to removal in a future version.
As of 6.0, with no direct replacement
`static final MimeType`
`TEXT_HTML`
Public constant mime type for `text/html`.
`static final String`
`TEXT_HTML_VALUE`
A String equivalent of `TEXT_HTML`.
`static final MimeType`
`TEXT_PLAIN`
Public constant mime type for `text/plain`.
`static final String`
`TEXT_PLAIN_VALUE`
A String equivalent of `TEXT_PLAIN`.
`static final MimeType`
`TEXT_XML`
Public constant mime type for `text/xml`.
`static final String`
`TEXT_XML_VALUE`
A String equivalent of `TEXT_XML`.

## Constructor Summary

Constructors
Constructor
Description
`MimeTypeUtils()`

## Method Summary

Modifier and Type
Method
Description
`static byte[]`
`generateMultipartBoundary()`
Generate a random MIME boundary as bytes, often used in multipart mime types.
`static String`
`generateMultipartBoundaryString()`
Generate a random MIME boundary as String, often used in multipart mime types.
`static MimeType`
`parseMimeType(String mimeType)`
Parse the given String into a single `MimeType`.
`static List<MimeType>`
`parseMimeTypes(String mimeTypes)`
Parse the comma-separated string into a mutable list of `MimeType` objects.
`static <T extends MimeType>
void`
`sortBySpecificity(List<T> mimeTypes)`
Sort the given list of `MimeType` objects by specificity.
`static List<String>`
`tokenize(String mimeTypes)`
Tokenize the given comma-separated string of `MimeType` objects into a `List<String>`.
`static String`
`toString(Collection<? extends MimeType> mimeTypes)`
Generate a string representation of the given collection of `MimeType` objects.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### SPECIFICITY_COMPARATOR

@Deprecated(since="6.0", forRemoval=true) public static final Comparator<MimeType> SPECIFICITY_COMPARATOR
Deprecated, for removal: This API element is subject to removal in a future version.
As of 6.0, with no direct replacement
Comparator formally used by `sortBySpecificity(List)`.

### ALL

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

### ALL_VALUE

public static final String ALL_VALUE
A String equivalent of `ALL`.

See Also:
  - Constant Field Values

### APPLICATION_GRAPHQL

public static final MimeType APPLICATION_GRAPHQL
Public constant mime type for `application/graphql+json`.

Since:
  5.3.19
See Also:
  - GraphQL over HTTP spec

### APPLICATION_GRAPHQL_VALUE

public static final String APPLICATION_GRAPHQL_VALUE
A String equivalent of `APPLICATION_GRAPHQL`.

Since:
  5.3.19
See Also:
  - Constant Field Values

### APPLICATION_JSON

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

### APPLICATION_JSON_VALUE

public static final String APPLICATION_JSON_VALUE
A String equivalent of `APPLICATION_JSON`.

See Also:
  - Constant Field Values

### APPLICATION_OCTET_STREAM

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

### APPLICATION_OCTET_STREAM_VALUE

public static final String APPLICATION_OCTET_STREAM_VALUE
A String equivalent of `APPLICATION_OCTET_STREAM`.

See Also:
  - Constant Field Values

### APPLICATION_XML

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

### APPLICATION_XML_VALUE

public static final String APPLICATION_XML_VALUE
A String equivalent of `APPLICATION_XML`.

See Also:
  - Constant Field Values

### IMAGE_GIF

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

### IMAGE_GIF_VALUE

public static final String IMAGE_GIF_VALUE
A String equivalent of `IMAGE_GIF`.

See Also:
  - Constant Field Values

### IMAGE_JPEG

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

### IMAGE_JPEG_VALUE

public static final String IMAGE_JPEG_VALUE
A String equivalent of `IMAGE_JPEG`.

See Also:
  - Constant Field Values

### IMAGE_PNG

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

### IMAGE_PNG_VALUE

public static final String IMAGE_PNG_VALUE
A String equivalent of `IMAGE_PNG`.

See Also:
  - Constant Field Values

### TEXT_HTML

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

### TEXT_HTML_VALUE

public static final String TEXT_HTML_VALUE
A String equivalent of `TEXT_HTML`.

See Also:
  - Constant Field Values

### TEXT_PLAIN

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

### TEXT_PLAIN_VALUE

public static final String TEXT_PLAIN_VALUE
A String equivalent of `TEXT_PLAIN`.

See Also:
  - Constant Field Values

### TEXT_XML

public static final MimeType TEXT_XML
Public constant mime type for `text/xml`.

### TEXT_XML_VALUE

public static final String TEXT_XML_VALUE
A String equivalent of `TEXT_XML`.

See Also:
  - Constant Field Values

## Constructor Details

### MimeTypeUtils

public MimeTypeUtils()

## Method Details

### parseMimeType

public static MimeType parseMimeType(String mimeType)
Parse the given String into a single `MimeType`. Recently parsed `MimeType` are cached for further retrieval.

Parameters:
  `mimeType` - the string to parse
Returns:
  the mime type
Throws:
  `InvalidMimeTypeException` - if the string cannot be parsed

### parseMimeTypes

public static List<MimeType> parseMimeTypes(String mimeTypes)
Parse the comma-separated string into a mutable list of `MimeType` objects.

Parameters:
  `mimeTypes` - the string to parse
Returns:
  the list of mime types
Throws:
  `InvalidMimeTypeException` - if the string cannot be parsed

### tokenize

public static List<String> tokenize(String mimeTypes)
Tokenize the given comma-separated string of `MimeType` objects into a `List<String>`. Unlike simple tokenization by ",", this method takes into account quoted parameters.

Parameters:
  `mimeTypes` - the string to tokenize
Returns:
  the list of tokens
Since:
  5.1.3

### toString

public static String toString(Collection<? extends MimeType> mimeTypes)
Generate a string representation of the given collection of `MimeType` objects.

Parameters:
  `mimeTypes` - the `MimeType` objects
Returns:
  a string representation of the `MimeType` objects

### sortBySpecificity

public static <T extends MimeType> void sortBySpecificity(List<T> mimeTypes)
Sort the given list of `MimeType` objects by specificity.

Because of the computational cost, this method throws an exception if the given list contains too many elements.

Parameters:
  `mimeTypes` - the list of mime types to be sorted
Throws:
  `InvalidMimeTypeException` - if `mimeTypes` contains more than 50 elements
See Also:
  - HTTP 1.1: Semantics and Content, section 5.3.2
  - `MimeType.isMoreSpecific(MimeType)`

### generateMultipartBoundary

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

### generateMultipartBoundaryString

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