java库从文件内容中查找mime类型 [英] java library to find the mime type from file content

查看:151
本文介绍了java库从文件内容中查找mime类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索一个java库,它通过查看文件内容(字节数组)告诉你mime类型。我发现这个项目使用jmimemagic,它不再支持更新的文件类型(例如MS word docx格式),因为它现在处于非活动状态(从2006年开始)。

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).

推荐答案

使用Apache tika进行内容检测。请在下面找到链接。 http://tika.apache.org/0.8/detection.html 。我们有很多jar依赖项,你可以在使用maven构建tika时找到它。

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库从文件内容中查找mime类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆