public class MIMETypeUtil
extends java.lang.Object
fileExtensionForMIMEType()
method, which converts a MIME type to a file extension. That method uses
a traditional mime.types files, similar to the file shipped
with with web servers such as Apache. It looks for a suitable file in
the following locations:
It loads all the matching files it finds; the first mapping found for a given MIME type is the one that is used. The files are only loaded once within a given running Java VM.
The syntax of the file follows the classic mime.types syntax:
# The format is <mime type> <space separated file extensions> # Comments begin with a '#' text/plain txt text TXT text/html html htm HTML HTM ...
When mapping a MIME type to an extension,
fileExtensionForMIMEType()
uses the first extension it finds in the mime.types file.
MIME types that cannot be found in the file are mapped to extension
".dat".
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DEFAULT_MIME_TYPE
Default MIME type, when a MIME type cannot be determined from a file's
extension.
|
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
fileExtensionForMIMEType(java.lang.String mimeType)
Get an appropriate extension for a MIME type.
|
static java.lang.String |
MIMETypeForFile(java.io.File file)
Get the MIME type for a file.
|
static java.lang.String |
MIMETypeForFile(java.io.File file,
java.lang.String defaultMIMEType)
Get the MIME type for a file.
|
static java.lang.String |
MIMETypeForFileExtension(java.lang.String extension)
Get the MIME type for a filename extension.
|
static java.lang.String |
MIMETypeForFileExtension(java.lang.String extension,
java.lang.String defaultMIMEType)
Get the MIME type for a filename extension.
|
static java.lang.String |
MIMETypeForFileName(java.lang.String fileName)
Get the MIME type for a name file.
|
static java.lang.String |
MIMETypeForFileName(java.lang.String fileName,
java.lang.String defaultMIMEType)
Get the MIME type for a file name.
|
static void |
parseContentTypeHeader(java.lang.String contentTypeHeader,
java.lang.StringBuffer mimeType,
java.util.Map<java.lang.String,java.lang.String> parameters)
This method parses an HTTP-style "Content-type" header into
its constituent pieces.
|
public static final java.lang.String DEFAULT_MIME_TYPE
public static java.lang.String fileExtensionForMIMEType(java.lang.String mimeType)
mimeType
- the String MIME typepublic static java.lang.String MIMETypeForFileExtension(java.lang.String extension)
extension
- the extension, without the "."MIMETypeForFileExtension(String,String)
,
MIMETypeForFile(File)
,
MIMETypeForFile(File,String)
,
MIMETypeForFileName(String)
,
MIMETypeForFileName(String,String)
public static java.lang.String MIMETypeForFileExtension(java.lang.String extension, java.lang.String defaultMIMEType)
extension
- the extension, without the "."defaultMIMEType
- the default MIME type to use if one cannot
be determined from the extension, or null to
use DEFAULT_MIME_TYPE
MIMETypeForFileExtension(String)
,
MIMETypeForFile(File)
,
MIMETypeForFile(File,String)
,
MIMETypeForFileName(String)
,
MIMETypeForFileName(String,String)
public static java.lang.String MIMETypeForFile(java.io.File file)
file
- the fileMIMETypeForFile(File,String)
,
MIMETypeForFileName(String)
,
MIMETypeForFileExtension(String)
,
MIMETypeForFileExtension(String,String)
,
DEFAULT_MIME_TYPE
public static java.lang.String MIMETypeForFile(java.io.File file, java.lang.String defaultMIMEType)
file
- the filedefaultMIMEType
- the default MIME type to use if one cannot
be determined from the file's name, or null to
use DEFAULT_MIME_TYPE
MIMETypeForFile(File)
,
MIMETypeForFileName(String,String)
,
MIMETypeForFileExtension(String)
,
MIMETypeForFileExtension(String,String)
,
DEFAULT_MIME_TYPE
public static java.lang.String MIMETypeForFileName(java.lang.String fileName)
fileName
- the file nameMIMETypeForFile(File)
,
MIMETypeForFileName(String,String)
,
DEFAULT_MIME_TYPE
public static java.lang.String MIMETypeForFileName(java.lang.String fileName, java.lang.String defaultMIMEType)
fileName
- the file namedefaultMIMEType
- the default MIME type to use if one cannot
be determined from the file name, or null to
use DEFAULT_MIME_TYPE
MIMETypeForFile(File,String)
,
MIMETypeForFileName(String)
,
MIMETypeForFileExtension(String)
,
MIMETypeForFileExtension(String,String)
,
DEFAULT_MIME_TYPE
public static void parseContentTypeHeader(java.lang.String contentTypeHeader, java.lang.StringBuffer mimeType, java.util.Map<java.lang.String,java.lang.String> parameters)
This method parses an HTTP-style "Content-type" header into its constituent pieces. The HTTP specification (RFC 2616) defines the Content-type header as:
"Content-type" ":" <media-type>
A media-type is a MIME type ("type/subtype"), with optional name=value pair parameters. The parameters are separated from the media type, and from each other, by ";" characters.
A common example of a Content-type header is:
Content-type: text/html; charset=ISO-8859-1
This method parses apart the MIME type and the parameters, saving each one separately in caller-supplied objects.
contentTypeHeader
- the header value (without the
Content-type: prefix). This value
is what's typically returned by
URLConnection.getContentType().mimeType
- a StringBuffer to receive the
MIME type, or null if you don't care about
the MIME type. This method clears the string
buffer before storing anything in it.parameters
- a Map to receive the parameters,
or null if you don't care about the
parameters. The parameter names are used
as the map's keys. This method clears the
map before storing anything in it.