判断java文件类型TikaToolKit

引入依赖


    org.apache.tika
    tika-core
    1.20

compile 'org.apache.tika:tika-core:1.20'

工具类

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.tika.metadata.HttpHeaders;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.metadata.TikaMetadataKeys;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.xml.sax.helpers.DefaultHandler;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * tika
 *
 * @author zhy
 */
@Slf4j
public class TikaToolUtil {

    /**
     * 获取类型
     *
     * @param file 文件
     * @return String
     */
    public static String getMimeType(File file) {
        if (file.isDirectory()) {
            return "the target is a directory";
        }

        AutoDetectParser parser = new AutoDetectParser();
        parser.setParsers(new HashMap<>());
        Metadata metadata = new Metadata();
        metadata.add(TikaMetadataKeys.RESOURCE_NAME_KEY, file.getName());
        try (InputStream stream = new FileInputStream(file)) {
            parser.parse(stream, new DefaultHandler(), metadata, new ParseContext());
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return metadata.get(HttpHeaders.CONTENT_TYPE);
    }

    /**
     * 判断是否是图片
     *
     * @param file 文件
     * @return boolean
     */
    public static boolean isImage(File file) {
        String type = getMimeType(file);
        System.out.println(type);
        Pattern p = Pattern.compile(Type.IMAGE.name);
        Matcher m = p.matcher(type);
        return m.matches();
    }

    /**
     * 判断是否是zip
     *
     * @param file 文件
     * @return boolean
     */
    public static boolean isZip(File file) {
        String type = getMimeType(file);
        System.out.println(type);
        Pattern p = Pattern.compile(Type.ZIP.name);
        Matcher m = p.matcher(type);
        return m.matches();
    }

    /**
     * 判断是否是dicom
     *
     * @param file 文件
     * @return boolean
     */
    public static boolean isDicom(File file) {
        String type = getMimeType(file);
        Pattern p = Pattern.compile(Type.DCM.name);
        Matcher m = p.matcher(type);
        return m.matches();
    }

    /**
     * 类型枚举
     */
    @Getter
    @AllArgsConstructor
    public enum Type {
        /**
         * dcm
         */
        DCM("application/dicom"),
        /**
         * zip
         */
        ZIP("application/zip"),
        /**
         * 图片
         */
        IMAGE("image/.*"),
        /**
         * 视频
         */
        VIDEO("video/*");

        private String name;
    }

    public static void main(String[] args) {
        boolean image = isImage(new File("C:\\Users\\zhy\\Desktop\\dcm\\123.dcm"));
        System.out.println("zhy:" + image);
    }
}

 

你可能感兴趣的:(java后台)