common_helper/src/main/java/com/xiongyingqi/util/FileHelper.java
public static String getMimeType(File data) {
        if (!data.exists()) {
            return null;
        }
        ContentHandler contenthandler = new BodyContentHandler();
        Metadata metadata = new Metadata();
        metadata.set(Metadata.RESOURCE_NAME_KEY, data.getName());
        Parser parser = new AutoDetectParser();
        try {
            parser.parse(new FileInputStream(data), contenthandler, metadata, null);
        } catch (FileNotFoundException e) {
            LOGGER.error(e);
        } catch (IOException e) {
            LOGGER.error(e);
        } catch (SAXException e) {
            LOGGER.error(e);
        } catch (TikaException e) {
            LOGGER.error(e);
        }
        // System.out.println("Mime: " + metadata.get(Metadata.CONTENT_TYPE));
        // System.out.println("Mime str: " + metadata.toString());
        return metadata.get(Metadata.CONTENT_TYPE);
    }
public static String getMimeType(File data) {
        if (!data.exists()) {
            return null;
        }
        ContentHandler contenthandler = new BodyContentHandler();
        Metadata metadata = new Metadata();
        metadata.set(Metadata.RESOURCE_NAME_KEY, data.getName());
        Parser parser = new AutoDetectParser();
        try {
            parser.parse(new FileInputStream(data), contenthandler, metadata, null);
        } catch (FileNotFoundException e) {
            LOGGER.error(e);
        } catch (IOException e) {
            LOGGER.error(e);
        } catch (SAXException e) {
            LOGGER.error(e);
        } catch (TikaException e) {
            LOGGER.error(e);
        }
        // System.out.println("Mime: " + metadata.get(Metadata.CONTENT_TYPE));
        // System.out.println("Mime str: " + metadata.toString());
        return metadata.get(Metadata.CONTENT_TYPE);
    }
public static String getMimeType ( File data ) { if ( ! data . exists ( ) ) { return null ; } ContentHandler contenthandler = new BodyContentHandler ( ) ; Metadata metadata = new Metadata ( ) ; metadata . set ( Metadata . RESOURCE_NAME_KEY , data . getName ( ) ) ; Parser parser = new AutoDetectParser ( ) ; try { parser . parse ( new FileInputStream ( data ) , contenthandler , metadata , null ) ; } catch ( FileNotFoundException e ) { LOGGER . error ( e ) ; } catch ( IOException e ) { LOGGER . error ( e ) ; } catch ( SAXException e ) { LOGGER . error ( e ) ; } catch ( TikaException e ) { LOGGER . error ( e ) ; } // System.out.println("Mime: " + metadata.get(Metadata.CONTENT_TYPE)); // System.out.println("Mime str: " + metadata.toString()); return metadata . get ( Metadata . CONTENT_TYPE ) ; }
获取文件类型 <br>
2013-9-2 下午9:08:51

@param data
@return
获取文件类型 <br > 2013 - 9 - 2 下午9 : 08 : 51