From 7727e9e2bf44f3ee8db28351e81ceb7305e6beed Mon Sep 17 00:00:00 2001 From: hongawen <83944980@qq.com> Date: Fri, 23 Aug 2024 11:10:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E6=96=87=E4=BB=B6=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../enums/response/CommonResponseEnum.java | 2 + .../java/com/njcn/common/utils/FileUtil.java | 86 +++++++++++++++++++ .../controller/SupvFileController.java | 34 +++++--- 3 files changed, 108 insertions(+), 14 deletions(-) diff --git a/pqs-common/common-core/src/main/java/com/njcn/common/pojo/enums/response/CommonResponseEnum.java b/pqs-common/common-core/src/main/java/com/njcn/common/pojo/enums/response/CommonResponseEnum.java index 44fe80668..7bc4cb32b 100644 --- a/pqs-common/common-core/src/main/java/com/njcn/common/pojo/enums/response/CommonResponseEnum.java +++ b/pqs-common/common-core/src/main/java/com/njcn/common/pojo/enums/response/CommonResponseEnum.java @@ -84,6 +84,8 @@ public enum CommonResponseEnum { FILE_EXIST("A0096", "文件已存在"), + FILE_NAME_ERROR("A0096", "文件格式有误"), + FILE_SIZE_ERROR("A0096", "文件过大"), FILE_XLSX_ERROR("A0096", "请上传excel文件"), diff --git a/pqs-common/common-core/src/main/java/com/njcn/common/utils/FileUtil.java b/pqs-common/common-core/src/main/java/com/njcn/common/utils/FileUtil.java index e67d17f16..9e5df71ed 100644 --- a/pqs-common/common-core/src/main/java/com/njcn/common/utils/FileUtil.java +++ b/pqs-common/common-core/src/main/java/com/njcn/common/utils/FileUtil.java @@ -1,6 +1,7 @@ package com.njcn.common.utils; import cn.hutool.core.text.StrPool; +import cn.hutool.core.util.StrUtil; import org.springframework.web.multipart.MultipartFile; import java.io.BufferedReader; @@ -56,5 +57,90 @@ public class FileUtil { } } + /** + * 判断文件是否为word格式 + * + * @param fileName 文件名 + */ + public static boolean judgeFileIsWord(String fileName) { + // 检查文件名是否为空 + if (StrUtil.isBlank(fileName)) { + return false; + } + // 获取文件扩展名 + String fileExtension = getFileExtension(fileName); + // 定义支持的文件类型 + String[] allowedExtensions = {"doc", "docx"}; + + // 检查扩展名是否在允许的列表中 + for (String ext : allowedExtensions) { + if (ext.equalsIgnoreCase(fileExtension)) { + return true; + } + } + return false; + } + + /** + * 判断文件是否为pdf格式 + * + * @param fileName 文件名 + */ + public static boolean judgeFileIsPdf(String fileName) { + // 检查文件名是否为空 + if (StrUtil.isBlank(fileName)) { + return false; + } + // 获取文件扩展名 + String fileExtension = getFileExtension(fileName); + // 定义支持的文件类型 + String[] allowedExtensions = {"pdf"}; + + // 检查扩展名是否在允许的列表中 + for (String ext : allowedExtensions) { + if (ext.equalsIgnoreCase(fileExtension)) { + return true; + } + } + return false; + } + + /** + * 判断文件是否为excel格式 + * + * @param fileName 文件名 + */ + public static boolean judgeFileIsExcel(String fileName) { + // 检查文件名是否为空 + if (StrUtil.isBlank(fileName)) { + return false; + } + // 获取文件扩展名 + String fileExtension = getFileExtension(fileName); + // 定义支持的文件类型 + String[] allowedExtensions = {"xls", "xlsx"}; + + // 检查扩展名是否在允许的列表中 + for (String ext : allowedExtensions) { + if (ext.equalsIgnoreCase(fileExtension)) { + return true; + } + } + return false; + } + + /** + * 从文件名中提取扩展名 + * + * @param fileName 文件名 + * @return 扩展名 + */ + private static String getFileExtension(String fileName) { + int dotIndex = fileName.lastIndexOf('.'); + if (dotIndex == -1) { + return ""; + } + return fileName.substring(dotIndex + 1); + } } diff --git a/pqs-process/process-boot/src/main/java/com/njcn/process/controller/SupvFileController.java b/pqs-process/process-boot/src/main/java/com/njcn/process/controller/SupvFileController.java index cf648eb01..808cdf2a9 100644 --- a/pqs-process/process-boot/src/main/java/com/njcn/process/controller/SupvFileController.java +++ b/pqs-process/process-boot/src/main/java/com/njcn/process/controller/SupvFileController.java @@ -9,6 +9,7 @@ import com.njcn.common.pojo.enums.common.LogEnum; import com.njcn.common.pojo.enums.response.CommonResponseEnum; import com.njcn.common.pojo.exception.BusinessException; import com.njcn.common.pojo.response.HttpResult; +import com.njcn.common.utils.FileUtil; import com.njcn.common.utils.HttpResultUtil; import com.njcn.process.pojo.param.SupvAlarmParam; import com.njcn.process.pojo.param.SupvFileParam; @@ -31,7 +32,7 @@ import java.util.List; /** *

- * 前端控制器 + * 前端控制器 *

* * @author hongawen @@ -47,40 +48,45 @@ public class SupvFileController extends BaseController { @PostMapping("planUpload") - @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.UPLOAD) + @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPLOAD) @ApiOperation("监督计划问题附件上传") public HttpResult planUpload(@ApiParam(value = "文件", required = true) @RequestPart("files") MultipartFile file, @RequestParam("planId") String planId, @RequestParam("type") Integer type, @RequestParam("uploaderId") String uploaderId, @RequestParam("uploaderName") String uploaderName, - @RequestParam("attachmentType")String attachmentType, - @RequestParam("uploadTime")String uploadTime - ){ + @RequestParam("attachmentType") String attachmentType, + @RequestParam("uploadTime") String uploadTime + ) { String methodDescribe = getMethodDescribe("planUpload"); - if(!StrUtil.isAllNotBlank(planId,uploaderId,uploaderName,attachmentType,uploadTime)||type==null){ - throw new BusinessException("必填字段不能为空"); + String originalFilename = file.getOriginalFilename(); + if (FileUtil.judgeFileIsWord(originalFilename) && FileUtil.judgeFileIsPdf(originalFilename) && FileUtil.judgeFileIsExcel(originalFilename)) { + if (!StrUtil.isAllNotBlank(planId, uploaderId, uploaderName, attachmentType, uploadTime) || type == null) { + throw new BusinessException("必填字段不能为空"); + } + iSupvFileService.planUpload(file, planId, type, uploaderId, uploaderName, attachmentType, uploadTime); + return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe); } - iSupvFileService.planUpload(file,planId,type,uploaderId,uploaderName,attachmentType,uploadTime); - return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe); + + return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FILE_NAME_ERROR, null, methodDescribe); } @PostMapping("list") @OperateInfo(info = LogEnum.BUSINESS_COMMON) @ApiOperation("查询附件信息集合") - @ApiImplicitParam(name = "param",value = "请求体",required = true) - public HttpResult> pageAlarm(@RequestBody SupvFileParam param){ + @ApiImplicitParam(name = "param", value = "请求体", required = true) + public HttpResult> pageAlarm(@RequestBody SupvFileParam param) { String methodDescribe = getMethodDescribe("pageAlarm"); List supvFiles = iSupvFileService.listFile(param); return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, supvFiles, methodDescribe); } @PostMapping("detail") - @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.DOWNLOAD) + @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DOWNLOAD) @ApiOperation("监督计划问题附件下载") - public HttpResult detail(HttpServletResponse response, @RequestParam("busId") String busId, @RequestParam("type") Integer type,@RequestParam("attachmentType") String attachmentType){ + public HttpResult detail(HttpServletResponse response, @RequestParam("busId") String busId, @RequestParam("type") Integer type, @RequestParam("attachmentType") String attachmentType) { String methodDescribe = getMethodDescribe("detail"); - iSupvFileService.detail(response,busId,type,attachmentType); + iSupvFileService.detail(response, busId, type, attachmentType); return null; } }