fix: 文件上传后缀名限制

dev
huangfeng 3 months ago
parent 09b0adabc0
commit 2b3ca992af

@ -8,6 +8,7 @@ import com.huatek.busi.entity.GlobalSettingItem;
import com.huatek.busi.entity.WiringDiagram; import com.huatek.busi.entity.WiringDiagram;
import com.huatek.busi.service.*; import com.huatek.busi.service.*;
import com.huatek.busi.where.*; import com.huatek.busi.where.*;
import com.huatek.torch.frame.exception.OwlException;
import com.huatek.torch.frame.tools.ConstantUtil; import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil; import com.huatek.torch.frame.tools.ResultUtil;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
@ -18,10 +19,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Base64; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@Api(tags={"一次接线图管理接口"}) @Api(tags={"一次接线图管理接口"})
@RestController @RestController
@ -61,7 +59,28 @@ public class WiringDiagramController {
if(globalSettingItem == null || (globalSettingItem.getFileUploadNumber() == null && globalSettingItem.getFileUploadNumber() <= 0)){ if(globalSettingItem == null || (globalSettingItem.getFileUploadNumber() == null && globalSettingItem.getFileUploadNumber() <= 0)){
return ResultUtil.put(ConstantUtil.REQUEST_FAIL, "BUSINESS.UPLOAD.ANNEX.NUM", ""); return ResultUtil.put(ConstantUtil.REQUEST_FAIL, "BUSINESS.UPLOAD.ANNEX.NUM", "");
} }
// 检查文件是否为空
if (file.isEmpty()) {
throw new OwlException("文件不能为空");
}
// 获取原始文件名并校验
String fileNamenew = file.getOriginalFilename(); String fileNamenew = file.getOriginalFilename();
if (fileNamenew == null || fileNamenew.isEmpty()) {
throw new OwlException("无效的文件名");
}
// 提取文件后缀(处理包含多个"."的情况)
int lastDotIndex = fileNamenew.lastIndexOf('.');
if (lastDotIndex == -1) {
throw new OwlException("文件缺少扩展名");
}
String suffix = fileNamenew.substring(lastDotIndex + 1).toLowerCase();
// 定义允许的后缀列表(可从配置读取)
List<String> allowedSuffixes = Arrays.asList("jpg", "png", "pdf", "docx", "doc", "xlsx", "xls");
// 校验后缀合法性
if (!allowedSuffixes.contains(suffix)) {
throw new OwlException("不支持的文件类型");
}
// 上传根目录 // 上传根目录
String rootPath = globalSettingItem.getRootPath(); String rootPath = globalSettingItem.getRootPath();
return iWiringDiagramService.importFile(file.getBytes(), token, "0", fileNamenew, rootPath,type,fileName); return iWiringDiagramService.importFile(file.getBytes(), token, "0", fileNamenew, rootPath,type,fileName);

Loading…
Cancel
Save