Is there any java library that is similar to unix\'s command file
?
ie:
$ file somepicture.png
somepicture.png PNG image, 805 x 292, 8-b
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.
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:
The closest thing in the JDK is URLConnection.guessContentTypeFromStream
You could look at jmimemagic (tutorial). We've been using it for a while to validate uploaded images. No problems so far.
Was looking for the same thing and found this: https://github.com/j256/simplemagic - it seems to be a clone of 'file', it even uses a copy of files built-in magic file.