public String getfiletype(String file){
File f = new File(file);
AutoDetectReader dr = null;
Tika tika = new Tika(); //创建一个Tika类
//利用Tika的detect方法检测文件的实际类型
System.out.println("filetype:"+tika.detect(file));
return tika.detect(file);
public String getrealfiletype(String file) {
InputStream is = null;
BufferedInputStream bis = null;
try {
is = new FileInputStream(new File(file));
bis = new BufferedInputStream(is);
AutoDetectParser parser = new AutoDetectParser();
Detector detector = parser.getDetector();
Metadata md = new Metadata();
md.add(Metadata.RESOURCE_NAME_KEY, file);
MediaType mediaType = detector.detect(bis, md);
return mediaType.toString();//返回的就是文件类型
} catch (Exception e) {
e.printStackTrace();
return null;
mimetypes mimes
“image/jpeg” “jpg”
“image/jpeg” “jpeg”
“image/png” “png”
“image/webp” “webp”
“application/vnd.ms-excel” “xls”
“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet” “xlsx”
“application/msword” “doc”
“application/vnd.openxmlformats-officedocument.wordprocessingml.document” “docx”
“application/vnd.openxmlformats-officedocument.presentationml.presentation” “pptx”
“application/vnd.ms-powerpoint” “ppt”
“application/pdf” “pdf”
“application/x-rar-compressed” “rar”
“application/zip” “zip”
“application/x-7z-compressed” “7z”
最全的类型,请见:
http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
public String getfiletype(String file){ File f = new File(file); AutoDetectReader dr = null; Tika tika = new Tika(); //创建一个Tika类 //利用Tika的detect方法检测文件的实际类型 Sy...
检测文件类型靠读取后缀名的做法,是极度不安全可靠的。后缀名是可以任意修改的,很容易识别错误。
我们使用Apache Tika来解析每个文件的文件头信息,返回具体的特征码,与映射表进行比对,就能知道文件的真实类型。
代码只是项目的一部分,只供参考
具体的操作如下:
public class FileUtil {
* Apache Tika 利用现有的解析类库,从不同格式的文档中(例如HTML, PDF, Doc),侦测和提取出元数据和结构化内容
Tika应用层架构
应用程序员可以很容易地在他们的应用程序集成Tika。Tika提供了一个命令行界面和图形用户界面,使它比较人性化。
在本章中,我们将讨论构成Tika架构的四个重要模块。下图显示了Tika的四个模块的体系结构:
语言检测机制。MIME检测机制。Parser接口。Tika Facade 类.
语言检测机制
每当一个文本文件被传递到Tika,它将检测在其中的语
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..
使用Apache Tika实现内容分析
Apache Tika可以抽取不同类型的内容和元信息的开源工具,如word、excel、pdf,甚至多媒体文件如JPEG、MP4。所有基于文本的和多媒体文件都可以使用通用接口进行解析,这使得Tika成为功能强大且用途广泛的内容分析库。
本文将介绍Apache Tika,包括解析API、如何自动监测文档内容类型,同时提供示例说明。
为了使...
Tika类型检测
Tika支持MIME所提供的所有互联网媒体文件类型。每当一个文件通过Tika检测到该文件,其文件类型。检测的介质类型,Tika内部通过以下机制。
MIME标准
多用途Internet邮件扩...
利用Tika,我们可以获得文件的实际类型、文件的编码格式、字符串的语言、文件的文本内容。Tika集成了许多jar包,包括poi和pdfbox,通过Tika对象的parseToString(File file)方法可以读取TXT、Word、Excel、PPT、PDF、HTML、XML等文件的文本内容。import java.io.File;
import java.io.FileInputStream