views:

207

answers:

5

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.

+3  A: 

You could look at jmimemagic (tutorial). We've been using it for a while to validate uploaded images. No problems so far.

sfussenegger
+5  A: 

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:

Hasturkun
Thanks.I think one of these two should do the trick. Its really documents and images I need.
Shervin
+1  A: 

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

Laurent
+1 for giving the JDK-only solution. I'm not sure how good that method is, but it could be what the user is looking for.
bkail
@bkail `javax.activation.MimetypesFileTypeMap` only checks the file extension which surely isn't a reliable way of determining "if the file is really what I want it to be"
sfussenegger
Yes the JDK solution is not working like `file` in unix. `file` really looks at the file and tells what it is. Doesn't care about extension like windows does
Shervin
@sfussenegger java.net.URLConnection.guessContentTypeFromStream(InputStream) surely does not use file extensions to make its determination.
bkail
@bkail That wasn't mentioned in the linked article, was it? Anyway, it's not working reliably. I've just tried and it failed for a simple jpg image.
sfussenegger
@sfussenegger Huh, I thought getContent/getContentType used guessContentTypeFromStream. Ah, apparently only for "jar:" URLs. Thanks for the clarification...
bkail
+1  A: 

Have a look at mime-utils. It works with content and/or with extensions.

openCage
A: 

The closest thing in the JDK is URLConnection.guessContentTypeFromStream

finnw