Is there a java library equivalent to file command in unix
You could look at jmimemagic (tutorial). We've been using it for a while to validate uploaded images. No problems so far.
I am not sure it is exactly what you are looking for, but the following link can maybe help you :
http://www.rgagnon.com/javadetails/java-0487.html
A quick google search (for the admittedly non-obvious) "java magic file detection" brings up a fairly nice looking article, "Get the Mime Type from a File" which suggests you use one of the following:
- Apache Tika
- JMimeMagic
Since Java 1.7 you can use Files.probeContentType() to probe a file. Out of box it uses the mechanism on the platform to guess the content type, or you can plugin your own detector if you want.