Validating a file when it is getting uploaded in the system is some time very necessary. We can check the extension of the file, but what if user has changed the extension to crack the system. So the best way is to find the mime type. There are multiple libraries that you can use to get the mime type of a file. Java 6 IO APIs can’t do that. I used JMimeMagic for this. Following is a code snippet to get the mime type of a file.
MagicMatch match = Magic.getMagicMatch(getPromotionOptIn().getUpload(),false); String mimeType = match.getMimeType();
You can download JMimeMagic lib from here.
JMimeMagic requires a dependency, that is Jakarata ORO. Jar can be downloaded from Apache archives.