Browse Source

文件名长度限制128字符

master
xsx 5 months ago
parent
commit
8f355d5a73
  1. 5
      im-platform/src/main/java/com/bx/implatform/contant/Constant.java
  2. 10
      im-platform/src/main/java/com/bx/implatform/service/impl/FileServiceImpl.java

5
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_SIZE = 20 * 1024 * 1024L;
/**
* 最大文件名长度
*/
public static final Long MAX_FILE_NAME_LENGTH = 128L;
/** /**
* 大群人数上限 * 大群人数上限
*/ */

10
im-platform/src/main/java/com/bx/implatform/service/impl/FileServiceImpl.java

@ -60,6 +60,8 @@ public class FileServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> imple
public String uploadFile(MultipartFile file) { public String uploadFile(MultipartFile file) {
try { try {
Long userId = SessionContext.getSession().getUserId(); Long userId = SessionContext.getSession().getUserId();
// 文件名长度校验
checkFileNameLength(file);
// 大小校验 // 大小校验
if (file.getSize() > Constant.MAX_FILE_SIZE) { if (file.getSize() > Constant.MAX_FILE_SIZE) {
throw new GlobalException(ResultCode.PROGRAM_ERROR, "文件大小不能超过20M"); throw new GlobalException(ResultCode.PROGRAM_ERROR, "文件大小不能超过20M");
@ -95,6 +97,8 @@ public class FileServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> imple
public UploadImageVO uploadImage(MultipartFile file, Boolean isPermanent,Long thumbSize) { public UploadImageVO uploadImage(MultipartFile file, Boolean isPermanent,Long thumbSize) {
try { try {
Long userId = SessionContext.getSession().getUserId(); Long userId = SessionContext.getSession().getUserId();
// 文件名长度校验
checkFileNameLength(file);
// 大小校验 // 大小校验
if (file.getSize() > Constant.MAX_IMAGE_SIZE) { if (file.getSize() > Constant.MAX_IMAGE_SIZE) {
throw new GlobalException(ResultCode.PROGRAM_ERROR, "图片大小不能超过20M"); throw new GlobalException(ResultCode.PROGRAM_ERROR, "图片大小不能超过20M");
@ -170,6 +174,7 @@ public class FileServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> imple
LambdaQueryWrapper<FileInfo> wrapper = Wrappers.lambdaQuery(); LambdaQueryWrapper<FileInfo> wrapper = Wrappers.lambdaQuery();
wrapper.eq(FileInfo::getMd5, md5); wrapper.eq(FileInfo::getMd5, md5);
wrapper.eq(FileInfo::getFileType, fileType); wrapper.eq(FileInfo::getFileType, fileType);
wrapper.last("limit 1");
return getOne(wrapper); return getOne(wrapper);
} }
@ -199,4 +204,9 @@ public class FileServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> imple
this.save(fileInfo); 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);
}
}
} }

Loading…
Cancel
Save