java web项目文件上传中常见的mime类型(基于Apache的tika)

2021-02-03 10:19

阅读:756

标签:log   slides   template   文件   上传   star   ali   jpeg   proc   

java代码

  final Tika tika = new Tika();

    Set 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 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)

标签:log   slides   template   文件   上传   star   ali   jpeg   proc   

原文地址:https://www.cnblogs.com/passedbylove/p/12801686.html


评论


亲,登录后才可以留言!