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