Is there a java library equivalent to file command in unix

前端 未结 7 2336
粉色の甜心
粉色の甜心 2020-12-03 14:16

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         


        
相关标签:
7条回答
  • 2020-12-03 14:16

    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.

    0 讨论(0)
  • 2020-12-03 14:22

    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

    0 讨论(0)
  • 2020-12-03 14:27

    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
    0 讨论(0)
  • 2020-12-03 14:28

    The closest thing in the JDK is URLConnection.guessContentTypeFromStream

    0 讨论(0)
  • 2020-12-03 14:31

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

    0 讨论(0)
  • 2020-12-03 14:31

    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.

    0 讨论(0)
提交回复
热议问题