Getting A File's Mime Type In Java

后端 未结 23 2727
南方客
南方客 2020-11-21 06:53

I was just wondering how most people fetch a mime type from a file in Java? So far I\'ve tried two utils: JMimeMagic & Mime-Util.

Th

23条回答
  •  情歌与酒
    2020-11-21 07:22

    if you work on linux OS ,there is a command line file --mimetype:

    String mimetype(file){
    
       //1. run cmd
       Object cmd=Runtime.getRuntime().exec("file --mime-type "+file);
    
       //2 get output of cmd , then 
        //3. parse mimetype
        if(output){return output.split(":")[1].trim(); }
        return "";
    }
    

    Then

    mimetype("/home/nyapp.war") //  'application/zip'
    
    mimetype("/var/www/ggg/au.mp3") //  'audio/mp3'
    

提交回复
热议问题