From 8f355d5a7388f0606098f8deb1df398c2e228606 Mon Sep 17 00:00:00 2001 From: xsx <825657193@qq.com> Date: Mon, 20 Oct 2025 16:05:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=90=8D=E9=95=BF=E5=BA=A6?= =?UTF-8?q?=E9=99=90=E5=88=B6128=E5=AD=97=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/bx/implatform/contant/Constant.java | 5 +++++ .../bx/implatform/service/impl/FileServiceImpl.java | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/im-platform/src/main/java/com/bx/implatform/contant/Constant.java b/im-platform/src/main/java/com/bx/implatform/contant/Constant.java index 3884cd7..d255ba3 100644 --- a/im-platform/src/main/java/com/bx/implatform/contant/Constant.java +++ b/im-platform/src/main/java/com/bx/implatform/contant/Constant.java @@ -15,6 +15,11 @@ public final class Constant { */ public static final Long MAX_FILE_SIZE = 20 * 1024 * 1024L; + /** + * 最大文件名长度 + */ + public static final Long MAX_FILE_NAME_LENGTH = 128L; + /** * 大群人数上限 */ diff --git a/im-platform/src/main/java/com/bx/implatform/service/impl/FileServiceImpl.java b/im-platform/src/main/java/com/bx/implatform/service/impl/FileServiceImpl.java index 761f2dc..9eb4ce5 100644 --- a/im-platform/src/main/java/com/bx/implatform/service/impl/FileServiceImpl.java +++ b/im-platform/src/main/java/com/bx/implatform/service/impl/FileServiceImpl.java @@ -60,6 +60,8 @@ public class FileServiceImpl extends ServiceImpl imple public String uploadFile(MultipartFile file) { try { Long userId = SessionContext.getSession().getUserId(); + // 文件名长度校验 + checkFileNameLength(file); // 大小校验 if (file.getSize() > Constant.MAX_FILE_SIZE) { throw new GlobalException(ResultCode.PROGRAM_ERROR, "文件大小不能超过20M"); @@ -95,6 +97,8 @@ public class FileServiceImpl extends ServiceImpl imple public UploadImageVO uploadImage(MultipartFile file, Boolean isPermanent,Long thumbSize) { try { Long userId = SessionContext.getSession().getUserId(); + // 文件名长度校验 + checkFileNameLength(file); // 大小校验 if (file.getSize() > Constant.MAX_IMAGE_SIZE) { throw new GlobalException(ResultCode.PROGRAM_ERROR, "图片大小不能超过20M"); @@ -170,6 +174,7 @@ public class FileServiceImpl extends ServiceImpl imple LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); wrapper.eq(FileInfo::getMd5, md5); wrapper.eq(FileInfo::getFileType, fileType); + wrapper.last("limit 1"); return getOne(wrapper); } @@ -199,4 +204,9 @@ public class FileServiceImpl extends ServiceImpl imple this.save(fileInfo); } + private void checkFileNameLength(MultipartFile file){ + if(file.getOriginalFilename().length() > Constant.MAX_FILE_NAME_LENGTH){ + throw new GlobalException("文件名长度不能超过" + Constant.MAX_FILE_NAME_LENGTH); + } + } }