From 2ac4a5aae69928fa4be547452f660f0ac918e283 Mon Sep 17 00:00:00 2001 From: "liang.chao" <1360241448@qq.com> Date: Thu, 10 Oct 2024 10:04:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=97=E7=BD=91=E6=BC=8F=E6=B4=9E=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/controller/SysFileController.java | 2 +- .../service/impl/SysFileServiceImpl.java | 22 +++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/controller/SysFileController.java b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/controller/SysFileController.java index 2010c23..7d5ada4 100644 --- a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/controller/SysFileController.java +++ b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/controller/SysFileController.java @@ -60,7 +60,7 @@ public class SysFileController { ErrorCode.ATTACHMENT_UPLOAD_FAILED.getMessage()); } }catch (Exception e){ - log.error(e.getMessage()); + return AjaxResult.error(e.getMessage()); } if (file != null && file.getId() != 0){ return AjaxResult.success(file); diff --git a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/impl/SysFileServiceImpl.java b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/impl/SysFileServiceImpl.java index 454da5b..ea4a6db 100644 --- a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/impl/SysFileServiceImpl.java +++ b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/impl/SysFileServiceImpl.java @@ -53,6 +53,8 @@ public class SysFileServiceImpl implements SysFileService { */ @Value("${file.path}") private String localFilePath; + // 允许的文件格式 + private static final List ALLOWED_EXTENSIONS = Arrays.asList("jpg", "jpeg", "txt", "png", "pdf", "docx", "doc", "xlsx", "xls"); @Resource private FileClient fileClient; @@ -68,11 +70,16 @@ public class SysFileServiceImpl implements SysFileService { public FileInfo uploadFile(HttpServletRequest request) throws Exception { FileInfo file = new FileInfo(); StandardMultipartHttpServletRequest req = (StandardMultipartHttpServletRequest) request; - // String photoType = req.getParameter("fileType"); + // String photoType = req.getParameter("fileType"); Long userId = SecurityUtils.getLoginUser().getUserid(); HashMap map = getFile(req); List items = (List) map.get("filePath"); MultipartFile item = items.get(0); + // 获取文件后缀名 + String fileExtension = item.getOriginalFilename().substring(item.getOriginalFilename().lastIndexOf(".") + 1); + if (!ALLOWED_EXTENSIONS.contains(fileExtension.toLowerCase())) { + throw new Exception("不支持该文件格式"); + } try { //String url = saveFile(request, item, photoType); /*AjaxResult res = fileClient.uploadFile(item); @@ -84,7 +91,7 @@ public class SysFileServiceImpl implements SysFileService { String type = fileName.substring(fileName.lastIndexOf(".") + 1); long size = item.getSize() / 1024 / 1024; file.setFileName(fileName); - log.info("文件名为:{}",fileName); + log.info("文件名为:{}", fileName); file.setFileUrl(url); file.setCreator(userId.toString()); file.setType(type); @@ -102,6 +109,7 @@ public class SysFileServiceImpl implements SysFileService { /** * 腾讯云文件上传 + * * @param file * @return */ @@ -126,7 +134,7 @@ public class SysFileServiceImpl implements SysFileService { String key = IdUtils.fastSimpleUuid() + file.getOriginalFilename(); //对上传文件分组,根据当前日期 /2024/04/02 String dateTime = new DateTime().toString("yyyy/MM/dd"); - key = dateTime+"/"+key; + key = dateTime + "/" + key; try { //获取上传文件输入流 InputStream inputStream = file.getInputStream(); @@ -139,9 +147,9 @@ public class SysFileServiceImpl implements SysFileService { // 高级接口会返回一个异步结果Upload PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest); // 返回上传文件路径:https://ggkt-atguigu-1310644373.cos.ap-beijing.myqcloud.com/01.jpg - return "https://" + bucketName + "." + "cos" + "." + ConstantPropertiesUtil.END_POINT+".myqcloud.com" + "/" + key; + return "https://" + bucketName + "." + "cos" + "." + ConstantPropertiesUtil.END_POINT + ".myqcloud.com" + "/" + key; } catch (Exception e) { - log.error(e.getMessage()); + log.error(e.getMessage()); } return null; } @@ -153,7 +161,7 @@ public class SysFileServiceImpl implements SysFileService { * @throws Exception */ @Override - public FileInfo uploadHeadPic(MultipartFile item,String fileType) { + public FileInfo uploadHeadPic(MultipartFile item, String fileType) { FileInfo file = new FileInfo(); Long userId = SecurityUtils.getLoginUser().getUserid(); try { @@ -205,7 +213,7 @@ public class SysFileServiceImpl implements SysFileService { String tmpName = multipartFile.getOriginalFilename(); tmpName = tmpName.substring(tmpName.lastIndexOf("\\") + 1); tmpName = IdUtil.fastSimpleUUID() + System.currentTimeMillis() + tmpName.substring(tmpName.lastIndexOf("."), tmpName.length()); - String imageFiles = localFilePath+ "/" + fileType + "/"; + String imageFiles = localFilePath + "/" + fileType + "/"; String os = System.getProperty("os.name"); if (os.toLowerCase().startsWith(GlobalConstants.STRING_WIN)) { imageFiles = "D://files/" + fileType + "/";