文件内容查找java,java库从文件内容中查找mime类型
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).
解決方案
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);
總結
以上是生活随笔為你收集整理的文件内容查找java,java库从文件内容中查找mime类型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 链表 传递_java 链表 传
- 下一篇: java toregex_Java Re