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

@ -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;
@ -36,6 +37,9 @@ import java.util.regex.Pattern;
@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

@ -129,15 +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) catch (Exception e)
{ {
@ -152,11 +153,11 @@ public class FileUploadUtils
* @return 文件名称 * @return 文件名称
* @throws IOException 写入异常 * @throws IOException 写入异常
*/ */
public static String uploadMinio(MultipartFile file, String bucketName) throws IOException public static String uploadMinio(MultipartFile file, String bucketName,String dir) throws IOException
{ {
try try
{ {
return uploadMinio(bucketName, file); return uploadMinio(bucketName,dir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
} }
catch (Exception e) catch (Exception e)
{ {
@ -164,18 +165,18 @@ public class FileUploadUtils
} }
} }
private static String uploadMinio(String bucketName, MultipartFile file) throws Exception 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)
{ {

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;
@ -40,17 +37,21 @@ 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){
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('?')); 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;