Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
V
vehicle-quality-review
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
王飞
vehicle-quality-review
Commits
1b00dc52
Commit
1b00dc52
authored
Dec 19, 2023
by
王飞
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Feat
1、整合minio。 Reference N/A
parent
8c83550d
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
414 additions
and
101 deletions
+414
-101
pom.xml
quality-review/pom.xml
+7
-0
CommonController.java
...ava/com/ruoyi/web/controller/common/CommonController.java
+146
-97
application.yml
ruoyi-admin/src/main/resources/application.yml
+6
-0
SystemReviewTaskTest.java
...i-admin/src/test/java/com/ruoyi/SystemReviewTaskTest.java
+7
-0
pom.xml
ruoyi-common/pom.xml
+8
-0
MinioConfig.java
...on/src/main/java/com/ruoyi/common/config/MinioConfig.java
+82
-0
FileUploadUtils.java
...ain/java/com/ruoyi/common/utils/file/FileUploadUtils.java
+114
-4
MinioUtil.java
.../src/main/java/com/ruoyi/common/utils/file/MinioUtil.java
+44
-0
No files found.
quality-review/pom.xml
View file @
1b00dc52
...
...
@@ -37,12 +37,19 @@
<scope>
provided
</scope>
</dependency>
<!-- skywalking 链路追踪 -->
<dependency>
<groupId>
org.apache.skywalking
</groupId>
<artifactId>
apm-toolkit-trace
</artifactId>
<version>
9.0.0
</version>
</dependency>
<!-- 用于将 java.io.File 转换为 MultipartFile -->
<dependency>
<groupId>
org.springframework
</groupId>
<artifactId>
spring-test
</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java
View file @
1b00dc52
...
...
@@ -37,127 +37,176 @@ public class CommonController
private
static
final
String
FILE_DELIMETER
=
","
;
/**
* 通用下载请求
*
* @param fileName 文件名称
* @param delete 是否删除
*/
@GetMapping
(
"/download"
)
public
void
fileDownload
(
String
fileName
,
Boolean
delete
,
HttpServletResponse
response
,
HttpServletRequest
request
)
{
try
{
if
(!
FileUtils
.
checkAllowDownload
(
fileName
))
{
throw
new
Exception
(
StringUtils
.
format
(
"文件名称({})非法,不允许下载。 "
,
fileName
));
}
String
realFileName
=
System
.
currentTimeMillis
()
+
fileName
.
substring
(
fileName
.
indexOf
(
"_"
)
+
1
);
String
filePath
=
RuoYiConfig
.
getDownloadPath
()
+
fileName
;
response
.
setContentType
(
MediaType
.
APPLICATION_OCTET_STREAM_VALUE
);
FileUtils
.
setAttachmentResponseHeader
(
response
,
realFileName
);
FileUtils
.
writeBytes
(
filePath
,
response
.
getOutputStream
());
if
(
delete
)
{
FileUtils
.
deleteFile
(
filePath
);
}
}
catch
(
Exception
e
)
{
log
.
error
(
"下载文件失败"
,
e
);
}
}
// /**
// * 通用下载请求
// *
// * @param fileName 文件名称
// * @param delete 是否删除
// */
// @GetMapping("/download")
// public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request)
// {
// try
// {
// if (!FileUtils.checkAllowDownload(fileName))
// {
// throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName));
// }
// String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
// String filePath = RuoYiConfig.getDownloadPath() + fileName;
//
// response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
// FileUtils.setAttachmentResponseHeader(response, realFileName);
// FileUtils.writeBytes(filePath, response.getOutputStream());
// if (delete)
// {
// FileUtils.deleteFile(filePath);
// }
// }
// catch (Exception e)
// {
// log.error("下载文件失败", e);
// }
// }
// /**
// * 通用上传请求(单个)
// */
// @PostMapping("/upload")
// public AjaxResult uploadFile(MultipartFile file) throws Exception
// {
// try
// {
// // 上传文件路径
// String filePath = RuoYiConfig.getUploadPath();
// // 上传并返回新文件名称
// String fileName = FileUploadUtils.upload(filePath, file);
// String url = serverConfig.getUrl() + fileName;
// AjaxResult ajax = AjaxResult.success();
// ajax.put("url", url);
// ajax.put("fileName", fileName);
// ajax.put("newFileName", FileUtils.getName(fileName));
// ajax.put("originalFilename", file.getOriginalFilename());
// return ajax;
// }
// catch (Exception e)
// {
// return AjaxResult.error(e.getMessage());
// }
// }
/**
* 通用上传请求(单个)
*/
@PostMapping
(
"/upload"
)
public
AjaxResult
uploadFile
(
MultipartFile
file
)
throws
Exception
{
try
{
// 上传文件路径
String
filePath
=
RuoYiConfig
.
getUploadPath
();
// 上传并返回新文件名称
String
fileName
=
FileUploadUtils
.
upload
(
filePath
,
file
);
String
url
=
serverConfig
.
getUrl
()
+
fileName
;
@PostMapping
(
"/uploadMinio"
)
public
AjaxResult
uploadFileMinio
(
MultipartFile
file
)
throws
Exception
{
try
{
String
url
=
FileUploadUtils
.
uploadMinio
(
file
);
AjaxResult
ajax
=
AjaxResult
.
success
();
ajax
.
put
(
"url"
,
url
);
ajax
.
put
(
"fileName"
,
fileName
);
ajax
.
put
(
"newFileName"
,
FileUtils
.
getName
(
fileName
));
ajax
.
put
(
"originalFilename"
,
file
.
getOriginalFilename
());
return
ajax
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
return
AjaxResult
.
error
(
e
.
getMessage
());
}
}
// /**
// * 通用上传请求(多个)
// */
// @PostMapping("/uploads")
// public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception
// {
// try
// {
// // 上传文件路径
// String filePath = RuoYiConfig.getUploadPath();
// List<String> urls = new ArrayList<String>();
// List<String> fileNames = new ArrayList<String>();
// List<String> newFileNames = new ArrayList<String>();
// List<String> originalFilenames = new ArrayList<String>();
// for (MultipartFile file : files)
// {
// // 上传并返回新文件名称
// String fileName = FileUploadUtils.upload(filePath, file);
// String url = serverConfig.getUrl() + fileName;
// urls.add(url);
// fileNames.add(fileName);
// newFileNames.add(FileUtils.getName(fileName));
// originalFilenames.add(file.getOriginalFilename());
// }
// AjaxResult ajax = AjaxResult.success();
// ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
// ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER));
// ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER));
// ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER));
// return ajax;
// }
// catch (Exception e)
// {
// return AjaxResult.error(e.getMessage());
// }
// }
/**
* 通用上传请求(多个)
*/
@PostMapping
(
"/uploads"
)
public
AjaxResult
uploadFiles
(
List
<
MultipartFile
>
files
)
throws
Exception
{
try
{
// 上传文件路径
String
filePath
=
RuoYiConfig
.
getUploadPath
();
List
<
String
>
urls
=
new
ArrayList
<
String
>();
List
<
String
>
fileNames
=
new
ArrayList
<
String
>();
List
<
String
>
newFileNames
=
new
ArrayList
<
String
>();
List
<
String
>
originalFilenames
=
new
ArrayList
<
String
>();
for
(
MultipartFile
file
:
files
)
{
// 上传并返回新文件名称
String
fileName
=
FileUploadUtils
.
upload
(
filePath
,
file
);
String
url
=
serverConfig
.
getUrl
()
+
fileName
;
@PostMapping
(
"/uploadsMinio"
)
public
AjaxResult
uploadFilesMinio
(
List
<
MultipartFile
>
files
)
throws
Exception
{
try
{
List
<
String
>
urls
=
new
ArrayList
();
List
<
String
>
originalFilenames
=
new
ArrayList
();
for
(
MultipartFile
file
:
files
)
{
String
url
=
FileUploadUtils
.
uploadMinio
(
file
);
urls
.
add
(
url
);
fileNames
.
add
(
fileName
);
newFileNames
.
add
(
FileUtils
.
getName
(
fileName
));
originalFilenames
.
add
(
file
.
getOriginalFilename
());
}
AjaxResult
ajax
=
AjaxResult
.
success
();
ajax
.
put
(
"urls"
,
StringUtils
.
join
(
urls
,
FILE_DELIMETER
));
ajax
.
put
(
"fileNames"
,
StringUtils
.
join
(
fileNames
,
FILE_DELIMETER
));
ajax
.
put
(
"newFileNames"
,
StringUtils
.
join
(
newFileNames
,
FILE_DELIMETER
));
ajax
.
put
(
"originalFilenames"
,
StringUtils
.
join
(
originalFilenames
,
FILE_DELIMETER
));
return
ajax
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
return
AjaxResult
.
error
(
e
.
getMessage
());
}
}
/**
* 本地资源通用下载
*/
@GetMapping
(
"/download/resource"
)
public
void
resourceDownload
(
String
resource
,
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
Exception
{
try
{
if
(!
FileUtils
.
checkAllowDownload
(
resource
))
{
throw
new
Exception
(
StringUtils
.
format
(
"资源文件({})非法,不允许下载。 "
,
resource
));
}
// 本地资源路径
String
localPath
=
RuoYiConfig
.
getProfile
();
// 数据库资源地址
String
downloadPath
=
localPath
+
StringUtils
.
substringAfter
(
resource
,
Constants
.
RESOURCE_PREFIX
);
// 下载名称
String
downloadName
=
StringUtils
.
substringAfterLast
(
downloadPath
,
"/"
);
response
.
setContentType
(
MediaType
.
APPLICATION_OCTET_STREAM_VALUE
);
FileUtils
.
setAttachmentResponseHeader
(
response
,
downloadName
);
FileUtils
.
writeBytes
(
downloadPath
,
response
.
getOutputStream
());
}
catch
(
Exception
e
)
{
log
.
error
(
"下载文件失败"
,
e
);
}
}
//
/**
//
* 本地资源通用下载
//
*/
//
@GetMapping("/download/resource")
//
public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
//
throws Exception
//
{
//
try
//
{
//
if (!FileUtils.checkAllowDownload(resource))
//
{
//
throw new Exception(StringUtils.format("资源文件({})非法,不允许下载。 ", resource));
//
}
//
// 本地资源路径
//
String localPath = RuoYiConfig.getProfile();
//
// 数据库资源地址
//
String downloadPath = localPath + StringUtils.substringAfter(resource, Constants.RESOURCE_PREFIX);
//
// 下载名称
//
String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
//
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
//
FileUtils.setAttachmentResponseHeader(response, downloadName);
//
FileUtils.writeBytes(downloadPath, response.getOutputStream());
//
}
//
catch (Exception e)
//
{
//
log.error("下载文件失败", e);
//
}
//
}
}
ruoyi-admin/src/main/resources/application.yml
View file @
1b00dc52
...
...
@@ -144,3 +144,9 @@ xss:
excludes
:
/system/notice
# 匹配链接
urlPatterns
:
/system/*,/monitor/*,/tool/*
minio
:
url
:
http://49.232.167.247:22038
accessKey
:
lB7WhZYiQwLzhHPutRGn
secretKey
:
7XMWpLm6p4d20OFe9uXKifEyhF3cp4sTCowI2fhJ
bucketName
:
vehicle-quality-review-oss
\ No newline at end of file
ruoyi-admin/src/test/java/com/ruoyi/SystemReviewTaskTest.java
View file @
1b00dc52
...
...
@@ -20,4 +20,11 @@ public class SystemReviewTaskTest {
System
.
out
.
println
(
JSONUtil
.
toJsonPrettyStr
(
task
));
}
@Test
public
void
testOSSUrl
()
{
String
bucket
=
"vehicle-quality-review-oss"
;
String
url
=
"http://49.232.167.247:22038/vehicle-quality-review-oss/2023/12/19/1702891760057_20231219102010A002.jpg"
;
System
.
out
.
println
();
}
}
ruoyi-common/pom.xml
View file @
1b00dc52
...
...
@@ -138,12 +138,20 @@
<artifactId>
spring-boot-starter-test
</artifactId>
</dependency>
<!-- hutool -->
<dependency>
<groupId>
cn.hutool
</groupId>
<artifactId>
hutool-all
</artifactId>
<version>
5.8.23
</version>
</dependency>
<!-- minio -->
<dependency>
<groupId>
io.minio
</groupId>
<artifactId>
minio
</artifactId>
<version>
8.2.1
</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
ruoyi-common/src/main/java/com/ruoyi/common/config/MinioConfig.java
0 → 100644
View file @
1b00dc52
package
com
.
ruoyi
.
common
.
config
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
io.minio.MinioClient
;
/**
* Minio 配置信息
*
* @author ruoyi
*/
@Configuration
@ConfigurationProperties
(
prefix
=
"minio"
)
public
class
MinioConfig
{
/**
* 服务地址
*/
private
static
String
url
;
/**
* 用户名
*/
private
static
String
accessKey
;
/**
* 密码
*/
private
static
String
secretKey
;
/**
* 存储桶名称
*/
private
static
String
bucketName
;
public
static
String
getUrl
()
{
return
url
;
}
public
void
setUrl
(
String
url
)
{
MinioConfig
.
url
=
url
;
}
public
static
String
getAccessKey
()
{
return
accessKey
;
}
public
void
setAccessKey
(
String
accessKey
)
{
MinioConfig
.
accessKey
=
accessKey
;
}
public
static
String
getSecretKey
()
{
return
secretKey
;
}
public
void
setSecretKey
(
String
secretKey
)
{
MinioConfig
.
secretKey
=
secretKey
;
}
public
static
String
getBucketName
()
{
return
bucketName
;
}
public
void
setBucketName
(
String
bucketName
)
{
MinioConfig
.
bucketName
=
bucketName
;
}
@Bean
public
MinioClient
getMinioClient
()
{
return
MinioClient
.
builder
().
endpoint
(
url
).
credentials
(
accessKey
,
secretKey
).
build
();
}
}
ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java
View file @
1b00dc52
package
com
.
ruoyi
.
common
.
utils
.
file
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.nio.file.Paths
;
import
java.util.Objects
;
import
org.apache.commons.io.FilenameUtils
;
import
org.springframework.mock.web.MockMultipartFile
;
import
org.springframework.web.multipart.MultipartFile
;
import
com.ruoyi.common.config.MinioConfig
;
import
com.ruoyi.common.config.RuoYiConfig
;
import
com.ruoyi.common.constant.Constants
;
import
com.ruoyi.common.exception.file.FileNameLengthLimitExceededException
;
...
...
@@ -33,10 +37,15 @@ public class FileUploadUtils
public
static
final
int
DEFAULT_FILE_NAME_LENGTH
=
100
;
/**
* 默认上传的地址
*
本地
默认上传的地址
*/
private
static
String
defaultBaseDir
=
RuoYiConfig
.
getProfile
();
/**
* Minio默认上传的地址
*/
private
static
String
bucketName
=
MinioConfig
.
getBucketName
();
public
static
void
setDefaultBaseDir
(
String
defaultBaseDir
)
{
FileUploadUtils
.
defaultBaseDir
=
defaultBaseDir
;
...
...
@@ -47,6 +56,11 @@ public class FileUploadUtils
return
defaultBaseDir
;
}
public
static
String
getBucketName
()
{
return
bucketName
;
}
/**
* 以默认配置进行文件上传
*
...
...
@@ -117,6 +131,90 @@ public class FileUploadUtils
return
getPathFileName
(
baseDir
,
fileName
);
}
/**
* 以默认BucketName配置上传到Minio服务器
*
* @param file 上传的文件
* @return 文件名称
* @throws Exception
*/
public
static
final
String
uploadMinio
(
MultipartFile
file
)
throws
IOException
{
try
{
return
uploadMinino
(
getBucketName
(),
file
,
MimeTypeUtils
.
DEFAULT_ALLOWED_EXTENSION
);
}
catch
(
Exception
e
)
{
throw
new
IOException
(
e
.
getMessage
(),
e
);
}
}
/**
* 以默认BucketName配置上传到Minio服务器
*
* @param file 上传的文件
* @param contentType 文件类型
* @return 文件名称
* @throws Exception
*/
public
static
final
String
uploadMinio
(
File
file
,
String
contentType
)
throws
IOException
{
return
uploadMinio
(
file2MultipartFile
(
file
,
contentType
));
}
/**
* 自定义bucketName配置上传到Minio服务器
*
* @param file 上传的文件
* @return 文件名称
* @throws Exception
*/
public
static
final
String
uploadMinio
(
MultipartFile
file
,
String
bucketName
)
throws
IOException
{
try
{
return
uploadMinino
(
bucketName
,
file
,
MimeTypeUtils
.
DEFAULT_ALLOWED_EXTENSION
);
}
catch
(
Exception
e
)
{
throw
new
IOException
(
e
.
getMessage
(),
e
);
}
}
/**
* 自定义bucketName配置上传到Minio服务器
*
* @param file 上传的文件
* @param contentType 文件类型
* @return 文件名称
* @throws Exception
*/
public
static
final
String
uploadMinio
(
File
file
,
String
contentType
,
String
bucketName
)
throws
IOException
{
return
uploadMinio
(
file2MultipartFile
(
file
,
contentType
),
bucketName
);
}
private
static
final
String
uploadMinino
(
String
bucketName
,
MultipartFile
file
,
String
[]
allowedExtension
)
throws
FileSizeLimitExceededException
,
IOException
,
FileNameLengthLimitExceededException
,
InvalidExtensionException
{
int
fileNamelength
=
file
.
getOriginalFilename
().
length
();
if
(
fileNamelength
>
FileUploadUtils
.
DEFAULT_FILE_NAME_LENGTH
)
{
throw
new
FileNameLengthLimitExceededException
(
FileUploadUtils
.
DEFAULT_FILE_NAME_LENGTH
);
}
assertAllowed
(
file
,
allowedExtension
);
try
{
String
fileName
=
extractFilename
(
file
);
String
pathFileName
=
MinioUtil
.
uploadFile
(
bucketName
,
fileName
,
file
);
return
pathFileName
;
}
catch
(
Exception
e
)
{
throw
new
IOException
(
e
.
getMessage
(),
e
);
}
}
/**
* 编码文件名
*/
...
...
@@ -229,4 +327,16 @@ public class FileUploadUtils
}
return
extension
;
}
/**
* java.io.File 转换 MultipartFile
* @param file java.io.File
* @param contentType 文件类型
* @return MultipartFile
* @throws IOException
*/
private
static
MultipartFile
file2MultipartFile
(
File
file
,
String
contentType
)
throws
IOException
{
return
new
MockMultipartFile
(
file
.
getName
(),
file
.
getName
(),
contentType
,
new
FileInputStream
(
file
));
}
}
\ No newline at end of file
ruoyi-common/src/main/java/com/ruoyi/common/utils/file/MinioUtil.java
0 → 100644
View file @
1b00dc52
package
com
.
ruoyi
.
common
.
utils
.
file
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
org.springframework.web.multipart.MultipartFile
;
import
com.ruoyi.common.utils.ServletUtils
;
import
com.ruoyi.common.utils.spring.SpringUtils
;
import
io.minio.GetPresignedObjectUrlArgs
;
import
io.minio.MinioClient
;
import
io.minio.PutObjectArgs
;
import
io.minio.http.Method
;
/**
* Minio 文件存储工具类
*
* @author ruoyi
*/
public
class
MinioUtil
{
/**
* 上传文件
*
* @param bucketName 桶名称
* @param fileName
* @throws IOException
*/
public
static
String
uploadFile
(
String
bucketName
,
String
fileName
,
MultipartFile
multipartFile
)
throws
IOException
{
String
url
=
""
;
MinioClient
minioClient
=
SpringUtils
.
getBean
(
MinioClient
.
class
);
try
(
InputStream
inputStream
=
multipartFile
.
getInputStream
())
{
minioClient
.
putObject
(
PutObjectArgs
.
builder
().
bucket
(
bucketName
).
object
(
fileName
).
stream
(
inputStream
,
multipartFile
.
getSize
(),
-
1
).
contentType
(
multipartFile
.
getContentType
()).
build
());
url
=
minioClient
.
getPresignedObjectUrl
(
GetPresignedObjectUrlArgs
.
builder
().
bucket
(
bucketName
).
object
(
fileName
).
method
(
Method
.
GET
).
build
());
url
=
url
.
substring
(
0
,
url
.
indexOf
(
'?'
));
url
=
url
.
substring
(
url
.
indexOf
(
bucketName
)
-
1
,
url
.
length
());
return
ServletUtils
.
urlDecode
(
url
);
}
catch
(
Exception
e
)
{
throw
new
IOException
(
e
.
getMessage
(),
e
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment