判断文件类型一般可采用两种方式
-
后缀名判断
简单易操作,但无法准确判断类型
-
文件头信息判断
通常可以判断文件类型,但有些文件类型无法判断(如word和excel头信息的前几个字节是一样的,无法判断)
使用apache.tika可轻松解决以上两种方式存在的问题
使用apache.tika判断文件类型
1. maven依赖
<!-- https://mvnrepository.com/artifact/org.apache.tika/tika-core -->
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>1.9</version>
</dependency>
2. 具体实现
private static String getMimeType(File file) {
if (file.isDirectory()) {
return "the target is a directory";
AutoDetectParser parser = new AutoDetectParser();
parser.setParsers(new HashMap<MediaType, Parser>());
Metadata metadata = new Metadata();
metadata.add(TikaMetadataKeys.RESOURCE_NAME_KEY, file.getName());
InputStream stream;
try {
stream = new FileInputStream(file);
parser.parse(stream, new DefaultHandler(), metadata, new ParseContext());
stream.close();
} catch (TikaException | SAXException | IOException e) {
e.printStackTrace();
return metadata.get(HttpHeaders.CONTENT_TYPE);
3. 常见文件类型
MimeType | 文件类型 |
---|
application/msword | word(.doc) |
application/vnd.ms-powerpoint | powerpoint(.ppt) |
application/vnd.ms-excel | excel(.xls) |
application/vnd.openxmlformats-officedocument.wordprocessingml.document | word(.docx) |
application/vnd.openxmlformats-officedocument.presentationml.presentation | powerpoint(.pptx) |
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet | excel(.xlsx) |
application/x-rar-compressed | rar |
application/zip | zip |
application/pdf | pdf |
video/* | 视频文件 |
image/* | 图片文件 |
text/plain | 纯文本 |
text/css | css文件 |
text/html | html文件 |
text/x-java-source | java源代码 |
text/x-csrc | c源代码 |
text/x-c++src | c++源代码 |
判断文件类型一般可采用两种方式后缀名判断 简单易操作,但无法准确判断类型 文件头信息判断 通常可以判断文件类型,但有些文件类型无法判断(如word和excel头信息的前几个字节是一样的,无法判断)使用apache.tika可轻松解决以上两种方式存在的问题 使用apache.tika判断文件类型1. maven依赖&lt;!-- https://mvnreposit...
Next, we’ll make use of the detect() method to resolve the type:
@Test
public void whenUsingTika_thenSuccess() {
File file = new File(“product.png”);
Tika tika = new Tika();
String mimeType = tika.detect(file);
assertEqu..
.doc application/msword
.dot application/msword
.docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
.dotx application/vnd.openxmlformats-officedocument.wordprocessingml.template
.docm application/vnd.ms-word.documen
转载自:https://www.jianshu.com/p/bc333fdcd1d3
Apache Tika是基于java的内容检测和分析的工具包,可检测并提取来自上千种不同文件类型(如PPT,XLS和PDF)中的元数据和结构化文本。 它提供了命令行界面、GUI界面和一个java库。Tika可帮助搜索引擎抓取内容后的数据处理。
Tika的处理过程
内置解析器会在后台通过外部程序提供的API与...
Tika应用层架构
应用程序员可以很容易地在他们的应用程序集成Tika。Tika提供了一个命令行界面和图形用户界面,使它比较人性化。
在本章中,我们将讨论构成Tika架构的四个重要模块。下图显示了Tika的四个模块的体系结构:
语言检测机制。MIME检测机制。Parser接口。Tika Facade 类.
语言检测机制
每当一个文本文件被传递到Tika,它将检测在其中的语
try (InputStream is = theInputStream;
BufferedInputStream bis = new BufferedInputStream(is);) {
AutoDetectParser parser = new AutoDetectParser();
Detector detector = parser.getDetecto...
使用tika判断文件类型,不会产生临时文件的方法
因为后缀判断文件不安全,所以最近找了找判断文件类型的其他方法,总结就是使用文件后缀和文件头来判断或者基于tika和文件后缀一同来做文件类型的判断,本次使用tika的方式来做文件类型判断,至于于配合个文件后缀的方法就不写了不是很难,就自己加上吧动动双手成就未来.
首先是依赖
就去maven repository中搜索tika-core就好了
还有一个tika-parsers咱没弄明白他是做什么的如果有了解的大佬可以评论一下
以下就是我的的代码区别就是因为使用s
文章目录1、基本介绍2、Tika使用2.1、解析器接口(The Parser interface)2.1.1、自定义Parser类2.2、检测器接口2.3、Tika配置
1、基本介绍
Apache Tika(文本分析工具包)能够检测并提取来自上千种不同文件类型(如PPT、XLS和PDF)的元数据和文本;所有这些文件类型都可以通过一个接口进行解析,这使得Tika在搜索引擎索引、内容分析、翻译等方面非常有用。
2、Tika使用
介绍下Tika在Maven项目中的使用,首先介绍下Tika相关的两个Maven依赖:
public String getfiletype(String file){
File f = new File(file);
AutoDetectReader dr = null;
Tika tika = new Tika(); //创建一个Tika类
//利用Tika的detect方法检测文件的实际类型
Sy...
使用Apache Tika实现内容分析
Apache Tika可以抽取不同类型的内容和元信息的开源工具,如word、excel、pdf,甚至多媒体文件如JPEG、MP4。所有基于文本的和多媒体文件都可以使用通用接口进行解析,这使得Tika成为功能强大且用途广泛的内容分析库。
本文将介绍Apache Tika,包括解析API、如何自动监测文档内容类型,同时提供示例说明。
为了使...