限制默认只能上传图片,可自行修改或删除,解决商用安全漏洞

pull/173/head
zhengyouxiancq 2025-03-04 15:42:07 +08:00
parent 407aa5908f
commit 2d81e405b8
1 changed files with 9 additions and 0 deletions

View File

@ -24,6 +24,7 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.module.infra.framework.file.core.utils.FileTypeUtils.writeAttachment;
@ -42,6 +43,14 @@ public class FileController {
public CommonResult<String> uploadFile(FileUploadReqVO uploadReqVO) throws Exception {
MultipartFile file = uploadReqVO.getFile();
String path = uploadReqVO.getPath();
// 校验文件类型
String extname = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")).toLowerCase();
if(StrUtil.isEmpty(extname)){
return error(3379,"只能上传图片文件!");
}
if(!".bmp,.jpg,.jpeg,.png".contains(extname)) {
return error(3379,"只能上传图片文件!");
}
return success(fileService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream())));
}