Wednesday, July 22, 2009

Determine Mime type from bytes

If you have an image file stored in the database but don't have the extension of the file, then you may not know what the mime type should be.

There is a handy Java library that will inspect the bytes to determine the Mime type. The library can be found here:

MimeUtil

It's fairly easy to use. An example code is shown below:

byte[] bytes;
MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MimeMagicMimeDetector");
MimeUtil.getMimeTypes(bytes);
MimeUtil.unregisterMimeDetector("eu.medsea.mimeutil.detector.MimeMagicMimeDetector");


No comments:

Post a Comment