This commit is contained in:
xd 2024-03-21 16:52:36 +08:00
parent ec396a9437
commit 2bce229a7b
7 changed files with 58 additions and 49 deletions

View File

@ -26,7 +26,7 @@ import com.ruoyi.framework.config.ServerConfig;
/** /**
* 通用请求处理 * 通用请求处理
* *
* @author ruoyi * @author ruoyi
*/ */
@RestController @RestController
@ -42,7 +42,7 @@ public class CommonController
/** /**
* 通用下载请求 * 通用下载请求
* *
* @param fileName 文件名称 * @param fileName 文件名称
* @param delete 是否删除 * @param delete 是否删除
*/ */
@ -168,7 +168,7 @@ public class CommonController
* Minio 服务器上传请求单文件上传 * Minio 服务器上传请求单文件上传
*/ */
@PostMapping("/minio-upload") @PostMapping("/minio-upload")
public AjaxResult uploadFileMinio(MultipartFile file) throws Exception public AjaxResult uploadFileMinio(String dir,MultipartFile file) throws Exception
{ {
if(file == null){ if(file == null){
return AjaxResult.error("不能上传空文件"); return AjaxResult.error("不能上传空文件");
@ -176,7 +176,7 @@ public class CommonController
try try
{ {
// 上传并返回新文件名称 // 上传并返回新文件名称
String fileName = FileUploadUtils.uploadMinio(file); String fileName = FileUploadUtils.uploadMinio(dir,file);
AjaxResult ajax = AjaxResult.success(); AjaxResult ajax = AjaxResult.success();
ajax.put("url", fileName); ajax.put("url", fileName);
ajax.put("fileName", fileName); ajax.put("fileName", fileName);

View File

@ -1,5 +1,6 @@
package com.ruoyi.web.controller.system; package com.ruoyi.web.controller.system;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
@ -29,13 +30,16 @@ import java.util.regex.Pattern;
/** /**
* 个人信息 业务处理 * 个人信息 业务处理
* *
* @author ruoyi * @author ruoyi
*/ */
@RestController @RestController
@RequestMapping("/system/user/profile") @RequestMapping("/system/user/profile")
public class SysProfileController extends BaseController public class SysProfileController extends BaseController
{ {
@Autowired
private ISysConfigService configService;
@Autowired @Autowired
private ISysUserService userService; private ISysUserService userService;
@ -132,7 +136,8 @@ public class SysProfileController extends BaseController
if (!file.isEmpty()) if (!file.isEmpty())
{ {
LoginUser loginUser = getLoginUser(); LoginUser loginUser = getLoginUser();
String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION); //String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
String avatar = FileUploadUtils.uploadMinio("user",file);
if (userService.updateUserAvatar(loginUser.getUsername(), avatar)) if (userService.updateUserAvatar(loginUser.getUsername(), avatar))
{ {
AjaxResult ajax = AjaxResult.success(); AjaxResult ajax = AjaxResult.success();

View File

@ -201,10 +201,10 @@ magic-api:
# Minio配置-分布式存储 # Minio配置-分布式存储
minio: minio:
useEnable: true useEnable: true
url: http://localhost:3336 url: http://192.168.9.134:3336
accessKey: minio-admin accessKey: minio-admin
secretKey: minio-jndlitzx secretKey: minio-jndlitzx
bucketName: jndl bucketName: system
# OA单点登录key # OA单点登录key
OA: OA:

View File

@ -37,7 +37,7 @@ public class FileUploadUtils
* 本地默认上传的地址 * 本地默认上传的地址
*/ */
private static String defaultBaseDir = RuoYiConfig.getProfile(); private static String defaultBaseDir = RuoYiConfig.getProfile();
/** /**
* Minio默认上传的地址 * Minio默认上传的地址
*/ */
@ -52,7 +52,7 @@ public class FileUploadUtils
{ {
return defaultBaseDir; return defaultBaseDir;
} }
public static String getBucketName() public static String getBucketName()
{ {
return BUCKET_NAME; return BUCKET_NAME;
@ -129,34 +129,16 @@ public class FileUploadUtils
/** /**
* 以默认BucketName配置上传到Minio服务器 * 以默认BucketName配置上传到Minio服务器
* *
* @param dir 分类目录
* @param file 上传的文件 * @param file 上传的文件
* @return 文件名称 * @return 文件名称
* @throws IOException 写入异常 * @throws IOException 写入异常
*/ */
public static String uploadMinio(MultipartFile file) throws IOException public static String uploadMinio(String dir,MultipartFile file) throws IOException
{ {
try try
{ {
return uploadMinio(getBucketName(), file); return uploadMinio(getBucketName(),dir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
}
catch (Exception e)
{
throw new IOException(e.getMessage(), e);
}
}
/**
* 自定义bucketName配置上传到Minio服务器
*
* @param file 上传的文件
* @return 文件名称
* @throws IOException 写入异常
*/
public static String uploadMinio(MultipartFile file, String bucketName) throws IOException
{
try
{
return uploadMinio(bucketName, file);
} }
catch (Exception e) catch (Exception e)
{ {
@ -164,18 +146,37 @@ public class FileUploadUtils
} }
} }
private static String uploadMinio(String bucketName, MultipartFile file) throws Exception /**
* 自定义bucketName配置上传到Minio服务器
*
* @param file 上传的文件
* @return 文件名称
* @throws IOException 写入异常
*/
public static String uploadMinio(MultipartFile file, String bucketName,String dir) throws IOException
{
try
{
return uploadMinio(bucketName,dir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
}
catch (Exception e)
{
throw new IOException(e.getMessage(), e);
}
}
private static String uploadMinio(String bucketName, String dir, MultipartFile file, String[] allowedExtension) throws Exception
{ {
int fileNameLength = file.getOriginalFilename().length(); int fileNameLength = file.getOriginalFilename().length();
if (fileNameLength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) if (fileNameLength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
{ {
throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH); throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
} }
assertAllowed(file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); assertAllowed(file, allowedExtension);
try try
{ {
String fileName = extractFilename(file); String fileName = extractFilename(file);
return MinioUtil.uploadFile(bucketName, fileName, file); return MinioUtil.uploadFile(bucketName,dir, fileName, file);
} }
catch (Exception e) catch (Exception e)
{ {
@ -276,7 +277,7 @@ public class FileUploadUtils
/** /**
* 获取文件名的后缀 * 获取文件名的后缀
* *
* @param file 表单文件 * @param file 表单文件
* @return 后缀名 * @return 后缀名
*/ */
@ -289,4 +290,4 @@ public class FileUploadUtils
} }
return extension; return extension;
} }
} }

View File

@ -1,9 +1,6 @@
package com.ruoyi.common.utils.file; package com.ruoyi.common.utils.file;
import java.io.ByteArrayOutputStream; import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -27,7 +24,7 @@ import io.minio.http.Method;
/** /**
* Minio 文件存储工具类 * Minio 文件存储工具类
* *
* @author ruoyi * @author ruoyi
*/ */
public class MinioUtil public class MinioUtil
@ -38,19 +35,23 @@ public class MinioUtil
/** /**
* 上传文件 * 上传文件
* *
* @param bucketName 桶名称 * @param bucketName 桶名称
* @param dir 分类目录
* @param fileName 文件名 * @param fileName 文件名
* @throws IOException 写入异常 * @throws IOException 写入异常
*/ */
public static String uploadFile(String bucketName, String fileName, MultipartFile multipartFile) throws IOException public static String uploadFile(String bucketName,String dir, String fileName, MultipartFile multipartFile) throws IOException
{ {
String url = "";
try (InputStream inputStream = multipartFile.getInputStream()) try (InputStream inputStream = multipartFile.getInputStream())
{ {
MINIO_CLIENT.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileName).stream(inputStream, multipartFile.getSize(), -1).contentType(multipartFile.getContentType()).build()); boolean exist = existBucket(bucketName);
String url = MINIO_CLIENT.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(fileName).method(Method.GET).build()); if(exist){
url = url.substring(0, url.indexOf('?')); MINIO_CLIENT.putObject(PutObjectArgs.builder().bucket(bucketName).object(dir+"/"+fileName).stream(inputStream, multipartFile.getSize(), -1).contentType(multipartFile.getContentType()).build());
url = MINIO_CLIENT.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(dir+"/"+fileName).method(Method.GET).build());
url = url.substring(0, url.indexOf('?'));
}
return ServletUtils.urlDecode(url); return ServletUtils.urlDecode(url);
} }
catch (Exception e) catch (Exception e)

View File

@ -71,7 +71,8 @@ const user = {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
getInfo().then(res => { getInfo().then(res => {
const user = res.user const user = res.user
const avatar = (user.avatar == "" || user.avatar == null) ? require("@/assets/images/profile.jpg") : process.env.VUE_APP_BASE_API + user.avatar; //const avatar = (user.avatar == "" || user.avatar == null) ? require("@/assets/images/profile.jpg") : process.env.VUE_APP_BASE_API + user.avatar;
const avatar = (user.avatar == "" || user.avatar == null) ? require("@/assets/images/profile.jpg") : user.avatar;
if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组 if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
commit('SET_ROLES', res.roles) commit('SET_ROLES', res.roles)
commit('SET_PERMISSIONS', res.permissions) commit('SET_PERMISSIONS', res.permissions)

View File

@ -137,7 +137,8 @@ export default {
formData.append("avatarfile", data, this.options.filename); formData.append("avatarfile", data, this.options.filename);
uploadAvatar(formData).then(response => { uploadAvatar(formData).then(response => {
this.open = false; this.open = false;
this.options.img = process.env.VUE_APP_BASE_API + response.imgUrl; //this.options.img = process.env.VUE_APP_BASE_API + response.imgUrl;
this.options.img = response.imgUrl;
store.commit('SET_AVATAR', this.options.img); store.commit('SET_AVATAR', this.options.img);
this.$modal.msgSuccess("修改成功"); this.$modal.msgSuccess("修改成功");
this.visible = false; this.visible = false;