一、背景
判断文件类型一般可采用两种方式
1、后缀名判断
简单易操作,但无法准确判断类型
2、文件头信息判断
通常可以判断文件类型,但有些文件类型无法判断(如word和excel头信息的前几个字节是一样的, 无法判断)
使用apache.tika可轻松解决以上两种方式存在的问题
二、tika介绍
apachhe tika是基于java的内容检测和分析的工具包,可检测并提取来自上千种不同文件类型(如ppt,xls和pdf)中的元数据和结构化文本。
三、使用方法
maven工程中引入依赖
<dependency>
<groupid>org.apache.tika</groupid>
<artifactid>tika-core</artifactid>
<version>1.24.1</version>
</dependency>注意tika-core和tika-parsers的区别,我这里导入的是tika-core
工具类(可现用)
使用示例 string filename = multipartfile.getoriginalfilename(); filetypecheck.getfiletypebyfilename(filename,multipartfile);
@slf4j
public class filetypecheck {
public static void getfiletypebyfilename(final string filename, multipartfile mfile) {
tika defaulttika = new tika();
string filetype;
final file file = new file(filename);
try {
fileutils.copyinputstreamtofile(mfile.getinputstream(), file);
filetype = defaulttika.detect(file);
if (!filetypecontant.file_type_xls.equals(filetype) && !filetypecontant.file_type_xlsx.equals(filetype)
&& !filetypecontant.file_type_doc.equals(filetype) && !filetypecontant.file_type_docx.equals(filetype) && !filetypecontant.file_type_pdf.equals(filetype)
&& !filetypecontant.file_type_json.equals(filetype) && !filetypecontant.file_type_xml.equals(filetype)
&& !filetypecontant.file_type_svg.equals(filetype) && !filetypecontant.file_type_png.equals(filetype)) {
throw new ztbusinessexception("请上传正确的文件类型!!!");
}
} catch (ioexception e) {
log.error("getfiletypebyfilename exception:", e);
} finally {
if (file.exists()) {
file.delete();
}
}
}
}
public class filetypecontant {
public static final string file_type_xls = "application/vnd.ms-excel";
public static final string file_type_xlsx = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
public static final string file_type_doc = "application/msword";
public static final string file_type_docx = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
public static final string file_type_pdf = "application/pdf";
public static final string file_type_json = "application/json";
public static final string file_type_xml = "application/xml";
public static final string file_type_png = "image/png";
public static final string file_type_svg = "image/svg";
}
四、multipartfile类转file类
其中,前端传过来的是multipartfile类,所以后端要转换为file类,转换方法为
//multipartfile转file
file file = new file(path);
fileutils.copyinputstreamtofile(multipartfile.getinputstream(), file);
/*其中file可以
file file = new file(mfile.getoriginalfilename());
这是保存在了工程路径下,用完要删除
file.delete();
*/
//file转multipartfile
file file = new file("src/test/resources/input.txt");
fileinputstream input = new fileinputstream(file);
multipartfile multipartfile =new mockmultipartfile("file", file.getname(), "text/plain", ioutils.tobytearray(input));
五、常见文件类型
| 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 | |
| video/* | 视频文件 |
| image/* | 图片文件 |
| text/plain | 纯文本 |
| text/css | css文件 |
| text/html | html文件 |
| text/x-java-source | java源代码 |
| text/x-csrc | c源代码 |
| text/x-c++src | c++源代码 |
六、总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论