|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objecteu.medsea.util.MimeUtil
MimeUtil instead!
public class MimeUtil
The MimeUtil utility class is used to detect mime types from
either a files extension or by looking into the file at various offsets and
comparing or looking for certain values in the same way as the Unix
file(1) command.
It is important to note that mime matching is not an exact science meaning that a positive match does not guarantee the returned mime type is correct. It is only a best guess method of matching and should be used with this in mind.
Both the file extension mapping and the magic mime number rules can be extended by the user.
The extension mime mappings are loaded in the following way.
eu.medsea.mime.mime-types.properties..mime-types.properties from the
users home directory if one exists.mime-types.properties from the
classpath if one existsmime-mappings i.e.
-Dmime-mappings=../my-mime-types.propertiesWe acquired many mappings from many different sources on the net for the extension mappings. The internal list is quite large and there can be many associated mime types. These may not match what you are expecting so you can add the mapping you want to change to your own property file following the rules above. If you provide a mapping for an extension then any previously loaded mappings will be removed and only the mappings you define will be returned. This can be used to map certain extensions that are incorrectly returned for our environment defined in the internal property file.
If we have not provided a mapping for a file extension that you know the mime type for you can add this to your custom property file so that a correct mime type is returned for you.
The magic mime rules files are loaded in the following way.
magic-mime i.e
-Dmagic-mime=../my/magic/mime/rulesmagic.mime that can be found on the
classpath.magic.mime in the users home directory/usr/share/file/magic.mime
and /etc/magic.mime (in that order)magic.mime file
eu.medsea.mime.magic.mime if, and only if, no files are located
in step 4 above.As with the extension mappings you can add new mime mapping rules using the syntax defined for the Unix magic.mime file by placing these rules in any of the files or locations listed above. You can also change an existing mapping rule by redefining the existing rule in one of the files listed above. This is handy for some of the more sketchy rules defined in the existing Unix magic.mime files.
When using the utility methods we always try to return a mime type even if no
mapping can be found. In the case of no mapping we have defined the utility
to return application/octet-stream by default. This can be
overridden and you can have a no-match return any mime type you define, even
unofficial mime types that you made up just for your application such as
application/x-unknown-mime-type. This allows your application to
provide special handling on a no-match such as executing a custom business
process.
We use the application/directory mime type to identify
directories. Even though this is not an official mime type it seems to be
well accepted on the net as an unofficial mime type so we thought it was OK
for us to use as well.
| Field Summary | |
|---|---|
static String |
DIRECTORY_MIME_TYPE
Deprecated. Mime type used to identify a directory |
static String |
UNKNOWN_MIME_TYPE
Deprecated. Mime type used to identify no match |
| Constructor Summary | |
|---|---|
MimeUtil()
Deprecated. |
|
| Method Summary | |
|---|---|
static void |
addKnownMimeType(String mimeType)
Deprecated. While all of the property files and magic.mime files are being loaded the utility keeps a list of mime types it's seen. |
static String |
getExtensionMimeTypes(File file)
Deprecated. Get the mime type of a file using file extension mappings. |
static String |
getExtensionMimeTypes(String fname)
Deprecated. Get the mime type of a file using file extension mappings. |
static String |
getFileExtension(File file)
Deprecated. Get the extension part of a file name defined by the file parameter. |
static String |
getFileExtension(String fileName)
Deprecated. Get the extension part of a file name defined by the fname parameter. |
static String |
getFirstMimeType(String mimeTypes)
Deprecated. Get the first in a comma separated list of mime types. |
static String |
getMagicMimeType(File file)
Deprecated. Get the mime type of a file using the magic.mime rules
files. |
static String |
getMagicMimeType(String fname)
Deprecated. Get the mime type of a file using the magic.mime rules
files. |
static String |
getMajorComponent(String mimeType)
Deprecated. Utility method to get the major part of a mime type i.e. |
static double |
getMimeQuality(String mimeType)
Deprecated. Utility method to get the quality part of a mime type. |
static String |
getMimeType(File f)
Deprecated. This is a convenience method where the order of lookup is set to extension mapping first. |
static String |
getMimeType(File file,
boolean extFirst)
Deprecated. Get the mime type of a file using a File object which can be
relative to the JVM or an absolute path. |
static String |
getMimeType(InputStream in)
Deprecated. Get the mime type of the data in the specified InputStream. |
static String |
getMimeType(String fname)
Deprecated. This is a convenience method where the order of lookup is set to extension mapping first. |
static String |
getMimeType(String fname,
boolean extFirst)
Deprecated. Get the mime type of a file using a path which can be relative to the JVM or an absolute path. |
static String |
getMinorComponent(String mimeType)
Deprecated. Utility method to get the minor part of a mime type i.e. |
static ByteOrder |
getNativeOrder()
Deprecated. Get the native byte order of the OS on which you are running. |
static String |
getPreferedMimeType(String accept,
String canProvide)
Deprecated. Gives you the best match for your requirements. |
static boolean |
isMimeTypeKnown(String mimeType)
Deprecated. Check to see if this mime type is one of the types seen during initialisation or has been added at some later stage using addKnownMimeType(...) |
static void |
setUnknownMimeType(String mimeType)
Deprecated. The default mime type returned by a no match i.e. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static String UNKNOWN_MIME_TYPE
public static final String DIRECTORY_MIME_TYPE
| Constructor Detail |
|---|
public MimeUtil()
| Method Detail |
|---|
public static ByteOrder getNativeOrder()
public static String getMimeType(InputStream in)
throws MimeException
InputStream.
Therefore, the InputStream must support mark and reset (see
InputStream.markSupported()). If it does not support mark and
reset, an MimeException is thrown.
in - the stream from which to read the data.
null (if the mime type
cannot be found, UNKNOWN_MIME_TYPE is returned).
MimeException - if the specified InputStream does not support
mark and reset (see InputStream.markSupported()).
public static String getMimeType(String fname,
boolean extFirst)
throws MimeException
UNKNOWN_MIME_TYPEis returned.
Their is an exception to this and that is if the fname
parameter does NOT point to a real file or directory and extFirst is
true then a match against the file extension could be found
and would be returned.
fname - points to a file or directoryextFirst - if true will first use file extension mapping and
then then magic.mime rules. If false
it will try to match the other way around i.e.
magic.mime rules and then file extension.
null (if the mime type
cannot be found, UNKNOWN_MIME_TYPE is returned).
MimeException - if while using the magic.mime rules there is a
problem processing the file.
public static String getMimeType(String fname)
throws MimeException
MimeExceptiongetMimeType(String fname, boolean extFirst)
public static String getMimeType(File file,
boolean extFirst)
throws MimeException
File object which can be
relative to the JVM or an absolute path. The path can point to a file or
directory location and if the path does not point to an actual file or
directory the UNKNOWN_MIME_TYPEis returned.
Their is an exception to this and that is if the file
parameter does NOT point to a real file or directory and extFirst is
true then a match against the file extension could be found
and would be returned.
file - points to a file or directoryextFirst - if true will first use file extension mapping and
then then magic.mime rules. If false
it will try to match the other way around i.e.
magic.mime rules and then file extension.
null (if the mime type
cannot be found, UNKNOWN_MIME_TYPE is returned).
MimeException - if while using the magic.mime rules there is a
problem processing the file.
public static String getMimeType(File f)
throws MimeException
MimeExceptiongetMimeType(File f, boolean extFirst)
public static String getPreferedMimeType(String accept,
String canProvide)
You can pass the accept header from a browser request to this method along with a comma separated list of possible mime types returned from say getExtensionMimeTypes(...) and the best match according to the accept header will be returned.
The following is typical of what may be specified in an HTTP Accept header:
Accept: text/xml, application/xml, application/xhtml+xml, text/html;q=0.9, text/plain;q=0.8, video/x-mng, image/png, image/jpeg, image/gif;q=0.2, text/css, */*;q=0.1
The quality parameter (q) indicates how well the user agent handles the MIME type. A value of 1 indicates the MIME type is understood perfectly, and a value of 0 indicates the MIME type isn't understood at all.
The reason the image/gif MIME type contains a quality parameter of 0.2, is to indicate that PNG & JPEG are preferred over GIF if the server is using content negotiation to deliver either a PNG or a GIF to user agents. Similarly, the text/html quality parameter has been lowered a little, to ensure that the XML MIME types are given in preference if content negotiation is being used to serve an XHTML document.
accept - is a comma separated list of mime types you can accept
including QoS parameters. Can pass the Accept: header
directly.canProvide - is a comma separated list of mime types that can be provided
such as that returned from a call to
getExtensionMimeTypes(...)
public static double getMimeQuality(String mimeType)
throws MimeException
Thanks to the Apache organisation or these settings.
mimeType - a valid mime type string with or without a valid q parameter
MimeException - this is thrown if the mime type pattern is invalid.
public static String getMagicMimeType(File file)
throws MimeException
magic.mime rules
files.
file - is a File object that points to a file or directory.
null (if the mime type
cannot be found, UNKNOWN_MIME_TYPE is returned).
MimeException - if the file cannot be parsed.
public static String getMajorComponent(String mimeType)
throws MimeException
mimeType - you want to get the major part from
MimeException - if you pass in an invalid mime type structure
public static String getMinorComponent(String mimeType)
throws MimeException
mimeType - you want to get the minor part from
MimeException - if you pass in an invalid mime type structurepublic static String getFileExtension(File file)
file - a file object
public static String getFileExtension(String fileName)
fileName - a relative or absolute path to a file
public static void addKnownMimeType(String mimeType)
For instance if you had a mime type of abc/xyz and passed this to isMimeTypeKnown(...) it would return false unless you specifically add this to the know mime types using this method.
mimeType - a mime type you want to add to the known mime types.
Duplicates are ignored.isMimeTypeKnown(String mimetype)public static boolean isMimeTypeKnown(String mimeType)
mimeType -
addKnownMimeType(String mimetype)public static String getFirstMimeType(String mimeTypes)
mimeTypes - comma separated list of mime types
public static String getExtensionMimeTypes(File file)
file - is a File object that points to a file or
directory. If the file or directory cannot be found
UNKNOWN_MIME_TYPE is returned.
null (if the mime type
cannot be found, UNKNOWN_MIME_TYPE is returned).
MimeException - if the file cannot be parsed.public static String getExtensionMimeTypes(String fname)
fname - is a path that points to a file or directory. If the file or
directory cannot be found UNKNOWN_MIME_TYPE is
returned.
null (if the mime type
cannot be found, UNKNOWN_MIME_TYPE is returned).
MimeException - if the file cannot be parsed.
public static String getMagicMimeType(String fname)
throws MimeException
magic.mime rules
files.
fname - is a path location to a file or directory.
null (if the mime type
cannot be found, UNKNOWN_MIME_TYPE is returned).
MimeException - if the file cannot be parsed.public static void setUnknownMimeType(String mimeType)
mimeType - set the default returned mime type for a no match.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||