亲宝软件园·资讯

展开

判断java文件类型TikaToolKit的实例

会迟到但不会缺席 人气:0

引入依赖

<dependency>
  <groupId>org.apache.tika</groupId>
  <artifactId>tika-core</artifactId>
  <version>1.20</version>
</dependency>

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根据文件流判断文件类型(后缀名)

我就废话不多说了,大家还是直接看代码吧~

import java.io.FileInputStream;

public class FileType{
  public static String bytesToHexString(byte[] src) {
    StringBuilder stringBuilder = new StringBuilder();
    if (src == null || src.length <= 0) {
      return null;
    }
    for (int i = 0; i < src.length; i++) {
      int v = src[i] & 0xFF;
      String hv = Integer.toHexString(v);
      if (hv.length() < 2) {
        stringBuilder.append(0);
      }
      stringBuilder.append(hv);
    }
    return stringBuilder.toString();
  }

  /**
   * @param args
   */
  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 {
/**
 常用文件的文件头如下:(以前六位为准)
JPEG (jpg),文件头:FFD8FF 
PNG (png),文件头:89504E47 
GIF (gif),文件头:47494638 
TIFF (tif),文件头:49492A00 
Windows Bitmap (bmp),文件头:424D 
CAD (dwg),文件头:41433130 
Adobe Photoshop (psd),文件头:38425053 
Rich Text Format (rtf),文件头:7B5C727466 
XML (xml),文件头:3C3F786D6C 
HTML (html),文件头:68746D6C3E 
Email [thorough only] (eml),文件头:44656C69766572792D646174653A 
Outlook Express (dbx),文件头:CFAD12FEC5FD746F 
Outlook (pst),文件头:2142444E 
MS Word/Excel (xls.or.doc),文件头:D0CF11E0 
MS Access (mdb),文件头:5374616E64617264204A 
WordPerfect (wpd),文件头:FF575043 
Postscript (eps.or.ps),文件头:252150532D41646F6265 
Adobe Acrobat (pdf),文件头:255044462D312E 
Quicken (qdf),文件头:AC9EBD8F 
Windows Password (pwl),文件头:E3828596 
ZIP Archive (zip),文件头:504B0304 
RAR Archive (rar),文件头:52617221 
Wave (wav),文件头:57415645 
AVI (avi),文件头:41564920 
Real Audio (ram),文件头:2E7261FD 
Real Media (rm),文件头:2E524D46 
MPEG (mpg),文件头:000001BA 
MPEG (mpg),文件头:000001B3 
Quicktime (mov),文件头:6D6F6F76 
Windows Media (asf),文件头:3026B2758E66CF11 
MIDI (mid),文件头:4D546864 
*/
  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的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论