Documentation of 'org.clapper.util.misc.MIMETypeUtil' Java class
MIMETypeUtil
org.clapper.util.misc

Class MIMETypeUtil



  • public class MIMETypeUtil
    extends java.lang.Object
    The MIMETypeUtil class provides some general purpose MIME type utilities not found in the JDK. Among other methods, this class provides the 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:
    1. First, it looks for the file .mime.types in the user's home directory.
    2. Next, it looks for mime.types (no leading ".") in all the directories in the CLASSPATH
    3. Last, it loads a default set of mappings shipped with this library

    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".

    • Field Summary

      Fields 
      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.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      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.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • fileExtensionForMIMEType

        public static java.lang.String fileExtensionForMIMEType(java.lang.String mimeType)
        Get an appropriate extension for a MIME type.
        Parameters:
        mimeType - the String MIME type
        Returns:
        the appropriate file name extension, or a default extension if not found. The extension will not have the leading "." character.
      • MIMETypeForFileName

        public static java.lang.String MIMETypeForFileName(java.lang.String fileName)
        Get the MIME type for a name file. This method is simply a convenient front-end for java.net.FileNameMap.getContentTypeFor(), but it applies a consistent default when getContentTypeFor() returns null (which can happen).
        Parameters:
        fileName - the file name
        Returns:
        the MIME type to use
        See Also:
        MIMETypeForFile(File), MIMETypeForFileName(String,String), DEFAULT_MIME_TYPE
      • parseContentTypeHeader

        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" ":" 

        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.

        Parameters:
        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.

DMelt 3.0 © DataMelt by jWork.ORG

You see the box below because you did not login.