Merge remote-tracking branch 'origin/master'

This commit is contained in:
guofeihu
2024-08-23 11:16:25 +08:00
3 changed files with 108 additions and 14 deletions

View File

@@ -84,6 +84,8 @@ public enum CommonResponseEnum {
FILE_EXIST("A0096", "文件已存在"), FILE_EXIST("A0096", "文件已存在"),
FILE_NAME_ERROR("A0096", "文件格式有误"),
FILE_SIZE_ERROR("A0096", "文件过大"), FILE_SIZE_ERROR("A0096", "文件过大"),
FILE_XLSX_ERROR("A0096", "请上传excel文件"), FILE_XLSX_ERROR("A0096", "请上传excel文件"),

View File

@@ -1,6 +1,7 @@
package com.njcn.common.utils; package com.njcn.common.utils;
import cn.hutool.core.text.StrPool; import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.StrUtil;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.BufferedReader; 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);
}
} }

View File

@@ -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.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException; import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.pojo.response.HttpResult; import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.FileUtil;
import com.njcn.common.utils.HttpResultUtil; import com.njcn.common.utils.HttpResultUtil;
import com.njcn.process.pojo.param.SupvAlarmParam; import com.njcn.process.pojo.param.SupvAlarmParam;
import com.njcn.process.pojo.param.SupvFileParam; import com.njcn.process.pojo.param.SupvFileParam;
@@ -58,6 +59,8 @@ public class SupvFileController extends BaseController {
@RequestParam("uploadTime") String uploadTime @RequestParam("uploadTime") String uploadTime
) { ) {
String methodDescribe = getMethodDescribe("planUpload"); String methodDescribe = getMethodDescribe("planUpload");
String originalFilename = file.getOriginalFilename();
if (FileUtil.judgeFileIsWord(originalFilename) || FileUtil.judgeFileIsPdf(originalFilename) || FileUtil.judgeFileIsExcel(originalFilename)) {
if (!StrUtil.isAllNotBlank(planId, uploaderId, uploaderName, attachmentType, uploadTime) || type == null) { if (!StrUtil.isAllNotBlank(planId, uploaderId, uploaderName, attachmentType, uploadTime) || type == null) {
throw new BusinessException("必填字段不能为空"); throw new BusinessException("必填字段不能为空");
} }
@@ -65,6 +68,9 @@ public class SupvFileController extends BaseController {
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe); return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
} }
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FILE_NAME_ERROR, null, methodDescribe);
}
@PostMapping("list") @PostMapping("list")
@OperateInfo(info = LogEnum.BUSINESS_COMMON) @OperateInfo(info = LogEnum.BUSINESS_COMMON)
@ApiOperation("查询附件信息集合") @ApiOperation("查询附件信息集合")