Reputation: 24509
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-bit/color RGB, non-interlaced
The file command is such a nice tool. I need something that can tell me if the file is really what I want it to be. (ie a picture, document etc)
I know I can run the command file
, but I am looking for a java library, not running the actual unix command.
Upvotes: 22
Views: 8123
Reputation: 211
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.
Upvotes: 0
Reputation: 2877
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.
Upvotes: 12
Reputation: 36412
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:
Upvotes: 19
Reputation: 48639
The closest thing in the JDK is URLConnection.guessContentTypeFromStream
Upvotes: 1
Reputation: 2775
Have a look at mime-utils. It works with content and/or with extensions.
Upvotes: 1
Reputation: 14401
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
Upvotes: 2
Reputation: 36105
You could look at jmimemagic (tutorial). We've been using it for a while to validate uploaded images. No problems so far.
Upvotes: 3