Search code examples
javamimefile-type

java library to find the mime type from file content


I am searching for a java library which tells you the mime type by looking at the file content(byte array). I found this project using jmimemagic and it no longer supports newer file types (eg. MS word docx format) as it is inactive now (from 2006).


Solution

  • Use Apache tika for content detection. Please find the link below. http://tika.apache.org/0.8/detection.html. We have so many jar dependencies which you can find when you build tika using maven

            ByteArrayInputStream bai = new ByteArrayInputStream(pByte);
            ContentHandler contenthandler = new BodyContentHandler();
            Metadata metadata = new Metadata();
            Parser parser = new AutoDetectParser();
            try {
                  parser.parse(bai, contenthandler, metadata);
    
            } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (SAXException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (TikaException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }           
            System.out.println("Mime: " + metadata.get(Metadata.CONTENT_TYPE));
            return metadata.get(Metadata.CONTENT_TYPE);