当前位置:网站首页>基于tika实现对文件类型进行判断
基于tika实现对文件类型进行判断
2022-08-01 07:37:00 【天青色 等烟雨】
基于tika实现对文件类型进行判断
判断文件类型一般可采用两种方式
1. 后缀名判断
简单易操作,但无法准确判断类型
2. 文件头信息判断
通常可以判断文件类型,但有些文件类型无法判断(如word和excel头信息的前几个字节是一样的,无法判断)
使用tika就可轻松解决这两种方式存在的问题
首先要做的就是导入tika的包坐标
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>1.24.1</version>
</dependency>
具体实现
package hkyxg.education.util;
import org.apache.tika.metadata.HttpHeaders;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.metadata.TikaMetadataKeys;
import org.apache.tika.mime.MediaType;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.springframework.web.multipart.MultipartFile;
import org.xml.sax.helpers.DefaultHandler;
import java.io.InputStream;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FileTypeUtil {
private static Pattern pattern = Pattern.compile("image/.*");
private static Pattern pattern2 = Pattern.compile("application/pdf");
/**
* 获取类型
* @param file
* @return
*/
public static String getMimeType(MultipartFile file) {
AutoDetectParser parser = new AutoDetectParser();
parser.setParsers(new HashMap<MediaType, Parser>());
Metadata metadata = new Metadata();
metadata.add(TikaMetadataKeys.RESOURCE_NAME_KEY, file.getName());
try (InputStream stream = file.getInputStream()) {
parser.parse(stream, new DefaultHandler(), metadata, new ParseContext());
}catch (Exception e){
throw new RuntimeException();
}
return metadata.get(HttpHeaders.CONTENT_TYPE);
}
/**
* 判断是否是图片
* @param file
* @return
*/
public static boolean isImage(MultipartFile file){
String type = getMimeType(file);
System.out.println(type);
//对比对应的文件类型的mime就好了
Matcher m = pattern.matcher(type);
return m.matches();
}
public static boolean isPdf(MultipartFile file){
String type = getMimeType(file);
System.out.println(type);
Matcher m = pattern2.matcher(type);
return m.matches();
}
}
tika是通过文件流来对文件类型进行判断的,与其他两种方式相比更加安全,方法也比较简单。
一些常见的mime type类型
image/bmp
image/x-bitmap
image/x-pixmap
image/jpg
image/png
image/jpeg
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx
application/x-rar-compressed
application/vnd.openxmlformats-officedocument.wordprocessingml.document docx
application/zip zip
image/tiff tiff
image/bmp bmp
application/rtf rtf
application/x-tika-ooxml xls
application/x-bplist pdf
application/pdf
application/vnd.ms-word.document.macroenabled.12 docm
image/gif
application/vnd.openxmlformats-officedocument.presentationml.presentation pptx
application/x-tika-msoffice pdf
application/msword
application/x-7z-compressed 7z
application/vnd.ms-xpsdocument xps
边栏推荐
- Vim扩展内容
- 图片无损压缩软件哪个好用:试试完全免费的JPG-C 图片批量修整压缩减肥工具吧 | 最新jpg批量修整工具下载
- Offer刷题——1
- 22牛客多校1 J.Serval and Essay (启发式合并)
- return; represents meaning
- 根据指定区域内容生成图片并进行分享总结
- Data organization -- singly linked list of the linear table
- LabVIEW RT中的用户界面更新速度
- pytest interface automation testing framework | single/multiple parameters
- GO error handling
猜你喜欢

Introduction to the basic principles, implementation and problem solving of crawler

Dbeaver connect the MySQL database and error Connection refusedconnect processing

nodetype中值1、2、3分别代表什么意思

配置我的kitty

小程序通过云函数操作数据库【使用get取数据库】

Guest brush SQL - 2

mysql中添加字段的相关问题

Vim简介

第02章 MySQL的数据目录【1.MySQL架构篇】【MySQL高级】

小程序全面屏手势配置案例
随机推荐
R语言使用tidyquant包的tq_transmute函数计算持有某只股票的天、月、周收益率、ggplot2使用条形图可视化股票月收益率数据、使用百分比显示Y轴坐标数据、使用不同的色彩表征正负收益率
【HDLBits 刷题】Circuits(1)Combinational Logic
rhcsa 第四天
请问用flinksql写入数据到clickhouse需要引入什么依赖吗?
拳头游戏免版权音乐下载,英雄联盟无版权音乐,可用于视频创作、直播
pytest接口自动化测试框架 | 使用函数返回值的形式传入参数值
爬虫框架 Scrapy 详解
pytest接口自动化测试框架 | 单个/多个参数
Datagrip error "The specified database userpassword combination is rejected..."Solutions
Data Analysis 6
The Bean's life cycle
VoLTE Basic Learning Series | Enterprise Voice Network Brief
zip打包目录所有文件(含隐藏文件/夹)
Flink SQL - client, how to deal with the source side and to increase the target, the SQL - client including mapping table and the JOB such as
MATLAB program design and application of MATLAB 2.5
选择排序—直接选择排序和堆排序
mysql中添加字段的相关问题
POJ1251丛林之路题解
【南瓜书ML】(task4)神经网络中的数学推导(更新ing)
R语言使用gt包和gtExtras包优雅地、漂亮地显示表格数据:gtExtras包的pad_fn函数与gt::fmt函数一起用于填充包含数值的特定列、对数据列的数值进行十进制对齐(从小数点对齐)