java web项目文件上传中常见的mime类型(基于Apache的tika)
Posted 学习笔记
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java web项目文件上传中常见的mime类型(基于Apache的tika)相关的知识,希望对你有一定的参考价值。
java代码
final Tika tika = new Tika(); Set<String> allowMimeTypeAll = ImmutableSet.of("image/pjpeg", "application/pdf", "application/msword", "image/jpeg", "image/x-png", "image/tiff", "application/vnd.ms-excel", "application/zip", "image/bmp", "image/x-bitmap", "image/x-pixmap", "image/jpg", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /*xlsx*/ , "application/x-rar-compressed", "application/rtf", "application/x-tika-ooxml",/*xls*/ "application/vnd.openxmlformats-officedocument.spreadsheetml.template",/*xltx*/ "application/vnd.ms-spreadsheetml",/*excel*/"application/vnd.ms-xpsdocument",/*xps*/ "application/x-bplist"/*pdf*/, "application/x-mswrite",/*Ms Write*/ "application/vnd.ms-word.document.macroenabled.12"/*docm*/, "application/x-tika-msoffice"/*pdf*/, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", /*docx*/ "application/vnd.openxmlformats-officedocument.wordprocessingml.template", /*dotx*/ "application/vnd.ms-powerpoint",/*ppt*/"vnd.ms-project",/*Microsoft Project*/ "application/vnd.visio"/*Visio*/, "image/svg+xml",/*svg*/"application/vnd.ms-works",/*wps*/ "application/vnd.openxmlformats-officedocument.presentationml.slideshow",/*ppsx*/ "application/vnd.openxmlformats-officedocument.presentationml.presentation"/*pptx*/, "application/vnd.openxmlformats-officedocument.presentationml.template" , "application/x-7z-compressed", "application/vnd.ms-xpsdocument"/*xps*/); Set<String> allowMimeType = ImmutableSet.of("application/pdf", "application/x-bplist"/*pdf*/, "application/x-tika-msoffice"/*pdf*/); //final String allowExtensionAll ="目前支持的文件类型:pdf,zip,rar,7z,bmp,jpeg,jpg,png,tiff,svg;WPS,Excel,PPT,Microsoft Project,Visio,Word,RTF类型文件。"; final String allowExtension = "目前仅支持PDF文件类型附件";
校验代码
@RequestMapping(value = "start", method = RequestMethod.POST) @ResponseBody public String start(@Valid Project project, @RequestParam("prjInfoFile") MultipartFile prjInfoFile, @RequestParam("prjDesignFile") MultipartFile prjDesignFile, @RequestParam("prjFinanceFile") MultipartFile prjFinanceFile, @RequestParam("files") MultipartFile[] files, Model model) throws IOException { byte[] byteInfoFile = prjInfoFile.getBytes(); if (byteInfoFile.length > 0) { String filename = prjInfoFile.getOriginalFilename().toLowerCase(); String mimeType = tika.detect(byteInfoFile); log.info("文件类型:{}", mimeType); if (!(allowMimeType.contains(mimeType) || filename.endsWith(".pdf"))) { return "error:【项目情况表】服务器暂不接受此类型的附件。".concat(allowExtension); } } }
以上是关于java web项目文件上传中常见的mime类型(基于Apache的tika)的主要内容,如果未能解决你的问题,请参考以下文章