update 用户头像上传限制只能为图片格式

feature/model
疯狂的狮子Li 3 years ago
parent 666c80877c
commit 1dceadba4c

@ -1,6 +1,7 @@
package com.ruoyi.web.controller.system; package com.ruoyi.web.controller.system;
import cn.dev33.satoken.secure.BCrypt; import cn.dev33.satoken.secure.BCrypt;
import cn.hutool.core.io.FileUtil;
import com.ruoyi.common.annotation.Log; import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants; import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.controller.BaseController;
@ -9,6 +10,7 @@ import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.helper.LoginHelper; import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.system.domain.SysOss; import com.ruoyi.system.domain.SysOss;
import com.ruoyi.system.service.ISysOssService; import com.ruoyi.system.service.ISysOssService;
import com.ruoyi.system.service.ISysUserService; import com.ruoyi.system.service.ISysUserService;
@ -22,6 +24,7 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.File;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -117,6 +120,10 @@ public class SysProfileController extends BaseController {
public R<Map<String, Object>> avatar(@RequestPart("avatarfile") MultipartFile file) { public R<Map<String, Object>> avatar(@RequestPart("avatarfile") MultipartFile file) {
Map<String, Object> ajax = new HashMap<>(); Map<String, Object> ajax = new HashMap<>();
if (!file.isEmpty()) { if (!file.isEmpty()) {
String extension = FileUtil.extName(file.getOriginalFilename());
if (!StringUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.IMAGE_EXTENSION)) {
return R.fail("文件格式不正确,请上传" + Arrays.toString(MimeTypeUtils.IMAGE_EXTENSION) + "格式");
}
SysOss oss = iSysOssService.upload(file); SysOss oss = iSysOssService.upload(file);
String avatar = oss.getUrl(); String avatar = oss.getUrl();
if (userService.updateUserAvatar(getUsername(), avatar)) { if (userService.updateUserAvatar(getUsername(), avatar)) {

@ -0,0 +1,40 @@
package com.ruoyi.common.utils.file;
/**
*
*
* @author ruoyi
*/
public class MimeTypeUtils {
public static final String IMAGE_PNG = "image/png";
public static final String IMAGE_JPG = "image/jpg";
public static final String IMAGE_JPEG = "image/jpeg";
public static final String IMAGE_BMP = "image/bmp";
public static final String IMAGE_GIF = "image/gif";
public static final String[] IMAGE_EXTENSION = {"bmp", "gif", "jpg", "jpeg", "png"};
public static final String[] FLASH_EXTENSION = {"swf", "flv"};
public static final String[] MEDIA_EXTENSION = {"swf", "flv", "mp3", "wav", "wma", "wmv", "mid", "avi", "mpg",
"asf", "rm", "rmvb"};
public static final String[] VIDEO_EXTENSION = {"mp4", "avi", "rmvb"};
public static final String[] DEFAULT_ALLOWED_EXTENSION = {
// 图片
"bmp", "gif", "jpg", "jpeg", "png",
// word excel powerpoint
"doc", "docx", "xls", "xlsx", "ppt", "pptx", "html", "htm", "txt",
// 压缩文件
"rar", "zip", "gz", "bz2",
// 视频格式
"mp4", "avi", "rmvb",
// pdf
"pdf"};
}
Loading…
Cancel
Save