This commit is contained in:
xd 2024-03-24 08:58:04 +08:00
parent 2bce229a7b
commit 21c77f76db
9 changed files with 81 additions and 39 deletions

View File

@ -83,7 +83,7 @@ public class CommonController
// 上传文件路径 // 上传文件路径
String filePath = RuoYiConfig.getUploadPath(); String filePath = RuoYiConfig.getUploadPath();
// 上传并返回新文件名称 // 上传并返回新文件名称
String fileName = FileUploadUtils.upload(filePath, file); String fileName = FileUploadUtils.upload(filePath,"", file);
String url = serverConfig.getUrl() + fileName; String url = serverConfig.getUrl() + fileName;
AjaxResult ajax = AjaxResult.success(); AjaxResult ajax = AjaxResult.success();
ajax.put("url", url); ajax.put("url", url);
@ -115,7 +115,7 @@ public class CommonController
for (MultipartFile file : files) for (MultipartFile file : files)
{ {
// 上传并返回新文件名称 // 上传并返回新文件名称
String fileName = FileUploadUtils.upload(filePath, file); String fileName = FileUploadUtils.upload(filePath,"", file);
String url = serverConfig.getUrl() + fileName; String url = serverConfig.getUrl() + fileName;
urls.add(url); urls.add(url);
fileNames.add(fileName); fileNames.add(fileName);
@ -168,7 +168,7 @@ public class CommonController
* Minio 服务器上传请求单文件上传 * Minio 服务器上传请求单文件上传
*/ */
@PostMapping("/minio-upload") @PostMapping("/minio-upload")
public AjaxResult uploadFileMinio(String dir,MultipartFile file) throws Exception public AjaxResult uploadFileMinio(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(dir,file); String fileName = FileUploadUtils.uploadMinio(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

@ -127,7 +127,7 @@ public class SysProfileController extends BaseController
} }
/** /**
* 头像上传 * 头像上传SysNoticeMapper
*/ */
@Log(title = "用户头像", businessType = BusinessType.UPDATE) @Log(title = "用户头像", businessType = BusinessType.UPDATE)
@PostMapping("/avatar") @PostMapping("/avatar")

View File

@ -201,7 +201,7 @@ magic-api:
# Minio配置-分布式存储 # Minio配置-分布式存储
minio: minio:
useEnable: true useEnable: true
url: http://192.168.9.134:3336 url: http://localhost:3336
accessKey: minio-admin accessKey: minio-admin
secretKey: minio-jndlitzx secretKey: minio-jndlitzx
bucketName: system bucketName: system

View File

@ -43,6 +43,11 @@ public class FileUploadUtils
*/ */
private static final String BUCKET_NAME = MinioConfig.getBucketName(); private static final String BUCKET_NAME = MinioConfig.getBucketName();
/**
* 本地默认分类目录
*/
private static String DIR = "";
public static void setDefaultBaseDir(String defaultBaseDir) public static void setDefaultBaseDir(String defaultBaseDir)
{ {
FileUploadUtils.defaultBaseDir = defaultBaseDir; FileUploadUtils.defaultBaseDir = defaultBaseDir;
@ -60,16 +65,26 @@ public class FileUploadUtils
/** /**
* 以默认配置进行文件上传 * 以默认配置进行文件上传
*
* @param file 上传的文件 * @param file 上传的文件
* @return 文件名称 * @return 文件名称
* @throws IOException 写入异常 * @throws IOException 写入异常
*/ */
public static String upload(MultipartFile file) throws IOException public static String upload(MultipartFile file) throws IOException {
return upload(file,DIR);
}
/**
* 以默认配置进行文件上传
* @param dir 自定义分类目录
* @param file 上传的文件
* @return 文件名称
* @throws IOException 写入异常
*/
public static String upload(MultipartFile file,String dir) throws IOException
{ {
try try
{ {
return upload(getDefaultBaseDir(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); return upload(getDefaultBaseDir(), dir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
} }
catch (Exception e) catch (Exception e)
{ {
@ -81,15 +96,16 @@ public class FileUploadUtils
* 根据文件路径上传 * 根据文件路径上传
* *
* @param baseDir 相对应用的基目录 * @param baseDir 相对应用的基目录
* @param dir 自定义分类目录
* @param file 上传的文件 * @param file 上传的文件
* @return 文件名称 * @return 文件名称
* @throws IOException 写入异常 * @throws IOException 写入异常
*/ */
public static String upload(String baseDir, MultipartFile file) throws IOException public static String upload(String baseDir, String dir, MultipartFile file) throws IOException
{ {
try try
{ {
return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); return upload(baseDir,dir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
} }
catch (Exception e) catch (Exception e)
{ {
@ -101,6 +117,7 @@ public class FileUploadUtils
* 文件上传 * 文件上传
* *
* @param baseDir 相对应用的基目录 * @param baseDir 相对应用的基目录
* @param dir 自定义分类目录
* @param file 上传的文件 * @param file 上传的文件
* @param allowedExtension 上传文件类型 * @param allowedExtension 上传文件类型
* @return 返回上传成功的文件名 * @return 返回上传成功的文件名
@ -109,7 +126,7 @@ public class FileUploadUtils
* @throws IOException 比如读写文件出错时 * @throws IOException 比如读写文件出错时
* @throws InvalidExtensionException 文件校验异常 * @throws InvalidExtensionException 文件校验异常
*/ */
public static String upload(String baseDir, MultipartFile file, String[] allowedExtension) throws Exception public static String upload(String baseDir, String dir, MultipartFile file, String[] allowedExtension) throws Exception
{ {
int fileNameLength = Objects.requireNonNull(file.getOriginalFilename()).length(); int fileNameLength = Objects.requireNonNull(file.getOriginalFilename()).length();
if (fileNameLength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) if (fileNameLength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
@ -119,13 +136,22 @@ public class FileUploadUtils
assertAllowed(file, allowedExtension); assertAllowed(file, allowedExtension);
String fileName = extractFilename(file); String fileName = extractFilename(dir,file);
String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath(); String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath();
file.transferTo(Paths.get(absPath)); file.transferTo(Paths.get(absPath));
return getPathFileName(baseDir, fileName); return getPathFileName(baseDir, fileName);
} }
/**
* 以默认BucketName配置上传到Minio服务器
* @param file 上传的文件
* @return 文件名称
* @throws IOException 写入异常
*/
public static String uploadMinio(MultipartFile file) throws IOException {
return uploadMinio(DIR,file);
}
/** /**
* 以默认BucketName配置上传到Minio服务器 * 以默认BucketName配置上传到Minio服务器
* *
@ -175,8 +201,8 @@ public class FileUploadUtils
assertAllowed(file, allowedExtension); assertAllowed(file, allowedExtension);
try try
{ {
String fileName = extractFilename(file); String fileName = extractFilename(dir,file);
return MinioUtil.uploadFile(bucketName,dir, fileName, file); return MinioUtil.uploadFile(bucketName,fileName, file);
} }
catch (Exception e) catch (Exception e)
{ {
@ -187,9 +213,15 @@ public class FileUploadUtils
/** /**
* 编码文件名 * 编码文件名
*/ */
public static String extractFilename(MultipartFile file) public static String extractFilename(String dir,MultipartFile file)
{ {
return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(), String template = "{}/{}_{}.{}";
if(StringUtils.isNotEmpty(dir)){
template = "{}/{}/{}_{}.{}";
return StringUtils.format(template, dir,DateUtils.datePath(),
FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file));
}
return StringUtils.format(template, DateUtils.datePath(),
FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file)); FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file));
} }

View File

@ -37,19 +37,18 @@ public class MinioUtil
* 上传文件 * 上传文件
* *
* @param bucketName 桶名称 * @param bucketName 桶名称
* @param dir 分类目录
* @param fileName 文件名 * @param fileName 文件名
* @throws IOException 写入异常 * @throws IOException 写入异常
*/ */
public static String uploadFile(String bucketName,String dir, String fileName, MultipartFile multipartFile) throws IOException public static String uploadFile(String bucketName,String fileName, MultipartFile multipartFile) throws IOException
{ {
String url = ""; String url = "";
try (InputStream inputStream = multipartFile.getInputStream()) try (InputStream inputStream = multipartFile.getInputStream())
{ {
boolean exist = existBucket(bucketName); boolean exist = existBucket(bucketName);
if(exist){ if(exist){
MINIO_CLIENT.putObject(PutObjectArgs.builder().bucket(bucketName).object(dir+"/"+fileName).stream(inputStream, multipartFile.getSize(), -1).contentType(multipartFile.getContentType()).build()); MINIO_CLIENT.putObject(PutObjectArgs.builder().bucket(bucketName).object(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 = MINIO_CLIENT.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(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);
@ -112,12 +111,22 @@ public class MinioUtil
return true; return true;
} }
/** /**
* @description 上传文件 * @description 上传文件
* @param multipartFile 文件资源 * @param multipartFile 文件资源
* @return java.lang.String * @return java.lang.String
*/ */
public static List<Map<String, Object>> upload(MultipartFile[] multipartFile) { public static List<Map<String, Object>> upload(MultipartFile[] multipartFile) {
return upload(BUCKET_NAME,multipartFile);
}
/**
* @description 上传文件
* @param multipartFile 文件资源
* @return java.lang.String
*/
public static List<Map<String, Object>> upload(String bucketName,MultipartFile[] multipartFile) {
List<Map<String, Object>> res = new ArrayList<>(multipartFile.length); List<Map<String, Object>> res = new ArrayList<>(multipartFile.length);
for (MultipartFile file : multipartFile) { for (MultipartFile file : multipartFile) {
String fileName = file.getOriginalFilename(); String fileName = file.getOriginalFilename();
@ -132,7 +141,7 @@ public class MinioUtil
in = file.getInputStream(); in = file.getInputStream();
MINIO_CLIENT.putObject( MINIO_CLIENT.putObject(
PutObjectArgs.builder() PutObjectArgs.builder()
.bucket(BUCKET_NAME).object(fileName).stream(in, in.available(), -1) .bucket(bucketName).object(fileName).stream(in, in.available(), -1)
.contentType(file.getContentType()) .contentType(file.getContentType())
.build()); .build());
} catch (Exception e) { } catch (Exception e) {

View File

@ -54,7 +54,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
)values( )values(
<if test="noticeTitle != null and noticeTitle != ''">#{noticeTitle}, </if> <if test="noticeTitle != null and noticeTitle != ''">#{noticeTitle}, </if>
<if test="noticeType != null and noticeType != ''">#{noticeType}, </if> <if test="noticeType != null and noticeType != ''">#{noticeType}, </if>
<if test="noticeContentBit != null and noticeContentBit.length != 0">notice_content = #{noticeContentBit}, </if> <if test="noticeContentBit != null and noticeContentBit.length != 0">#{noticeContentBit}, </if>
<if test="status != null and status != ''">#{status}, </if> <if test="status != null and status != ''">#{status}, </if>
<if test="remark != null and remark != ''">#{remark},</if> <if test="remark != null and remark != ''">#{remark},</if>
<if test="createBy != null and createBy != ''">#{createBy},</if> <if test="createBy != null and createBy != ''">#{createBy},</if>

View File

@ -60,7 +60,7 @@ export default {
}, },
data() { data() {
return { return {
uploadUrl: process.env.VUE_APP_BASE_API + "/common/upload", // uploadUrl: process.env.VUE_APP_BASE_API + "/common/minio-upload", //
headers: { headers: {
Authorization: "Bearer " + getToken() Authorization: "Bearer " + getToken()
}, },
@ -182,7 +182,8 @@ export default {
// //
let length = quill.getSelection().index; let length = quill.getSelection().index;
// res.url // res.url
quill.insertEmbed(length, "image", process.env.VUE_APP_BASE_API + res.fileName); //quill.insertEmbed(length, "image", process.env.VUE_APP_BASE_API + res.fileName);
quill.insertEmbed(length, "image", res.fileName);
// //
quill.setSelection(length + 1); quill.setSelection(length + 1);
} else { } else {

View File

@ -73,7 +73,7 @@ export default {
number: 0, number: 0,
uploadList: [], uploadList: [],
baseUrl: process.env.VUE_APP_BASE_API, baseUrl: process.env.VUE_APP_BASE_API,
uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/minio-upload", //
headers: { headers: {
Authorization: "Bearer " + getToken(), Authorization: "Bearer " + getToken(),
}, },

View File

@ -77,7 +77,7 @@ export default {
dialogVisible: false, dialogVisible: false,
hideUpload: false, hideUpload: false,
baseUrl: process.env.VUE_APP_BASE_API, baseUrl: process.env.VUE_APP_BASE_API,
uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/minio-upload", //
headers: { headers: {
Authorization: "Bearer " + getToken(), Authorization: "Bearer " + getToken(),
}, },