• 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);
                }
            }
    }
  • 相关阅读:
    Access中出现改变字段“自己主动编号”类型,不能再改回来!(已解决)
    移动闭塞
    信号系统基本设备介绍——应答器
    行车闭塞
    计轴
    联锁
    SqlServer 查看缓存 并合理设置最大内存
    SQL Server中bcp命令的用法以及数据批量导入导出
    5 个免费的受欢迎的 SQLite 管理工具
    Qt之操作数据库(SQLite)
  • 原文地址:https://www.cnblogs.com/passedbylove/p/12801686.html
Copyright © 2020-2023  润新知