栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

判断java文件类型TikaToolKit的实例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

判断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;
 

@Slf4j
public class TikaToolUtil {
 
  
  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);
  }
 
  
  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();
  }
 
  
  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();
  }
 
  
  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("application/dicom"),
    
    ZIP("application/zip"),
    
    IMAGE("image/.*"),
    
    VIDEO("video
  public static void main(String[] args) throws Exception {
    FileInputStream is = new FileInputStream("F:\相册\微信图片\03.jpg");
    byte[] b = new byte[3];
    is.read(b, 0, b.length);
    String xxx = bytesToHexString(b);
    xxx = xxx.toUpperCase();
    System.out.println("头文件是:" + xxx);
    String ooo = TypeDict.checkType(xxx);
    System.out.println("后缀名是:" + ooo);

  }  
}
public class TypeDict {

  public static String checkType(String xxxx) {
    
    switch (xxxx) {
    case "FFD8FF": return "jpg";
    case "89504E": return "png";
    case "474946": return "jif";

    default: return "0000";
    }
  }
}

以上这篇判断java文件类型TikaToolKit的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/131024.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号