Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
有来开源组织
youlai-mall
提交
f58ac515
Y
youlai-mall
项目概览
有来开源组织
/
youlai-mall
通知
8
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Y
youlai-mall
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
f58ac515
编写于
4月 13, 2023
作者:
郝
郝先瑞
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: 文件上传未关闭流问题修复
上级
74040479
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
37 addition
and
29 deletion
+37
-29
youlai-common/common-file/src/main/java/com/youlai/common/file/service/impl/MinioServiceImpl.java
...com/youlai/common/file/service/impl/MinioServiceImpl.java
+37
-29
未找到文件。
youlai-common/common-file/src/main/java/com/youlai/common/file/service/impl/MinioServiceImpl.java
浏览文件 @
f58ac515
...
...
@@ -7,6 +7,7 @@ import cn.hutool.core.util.IdUtil;
import
cn.hutool.core.util.StrUtil
;
import
com.youlai.common.file.service.FileService
;
import
com.youlai.common.file.vo.FileInfo
;
import
com.youlai.common.web.exception.BizException
;
import
io.minio.*
;
import
io.minio.http.Method
;
import
lombok.Setter
;
...
...
@@ -21,6 +22,12 @@ import java.io.InputStream;
import
java.time.LocalDateTime
;
/**
* MinIO 文件服务类
*
* @author haoxr
* @date 2023/4/13
*/
@Component
@ConfigurationProperties
(
prefix
=
"minio"
)
@Slf4j
...
...
@@ -80,7 +87,6 @@ public class MinioServiceImpl implements FileService, InitializingBean {
* @return
*/
@Override
@SneakyThrows
public
FileInfo
uploadFile
(
MultipartFile
file
)
{
// 存储桶不存在则创建
createBucketIfAbsent
(
bucketName
);
...
...
@@ -89,36 +95,38 @@ public class MinioServiceImpl implements FileService, InitializingBean {
String
suffix
=
FileUtil
.
getSuffix
(
file
.
getOriginalFilename
());
String
uuid
=
IdUtil
.
simpleUUID
();
String
fileName
=
DateUtil
.
format
(
LocalDateTime
.
now
(),
"yyyy/MM/dd"
)
+
"/"
+
uuid
+
"."
+
suffix
;
InputStream
inputStream
=
file
.
getInputStream
();
// 文件上传
PutObjectArgs
putObjectArgs
=
PutObjectArgs
.
builder
()
.
bucket
(
bucketName
)
.
object
(
fileName
)
.
contentType
(
file
.
getContentType
())
.
stream
(
inputStream
,
inputStream
.
available
(),
-
1
)
.
build
();
minioClient
.
putObject
(
putObjectArgs
);
// 返回文件路径
String
fileUrl
;
if
(
StrUtil
.
isBlank
(
customDomain
))
{
// 未配置自定义域名
GetPresignedObjectUrlArgs
getPresignedObjectUrlArgs
=
GetPresignedObjectUrlArgs
.
builder
()
.
bucket
(
bucketName
).
object
(
fileName
)
.
method
(
Method
.
GET
)
// try-with-resource 语法糖自动释放流
try
(
InputStream
inputStream
=
file
.
getInputStream
())
{
// 文件上传
PutObjectArgs
putObjectArgs
=
PutObjectArgs
.
builder
()
.
bucket
(
bucketName
)
.
object
(
fileName
)
.
contentType
(
file
.
getContentType
())
.
stream
(
inputStream
,
inputStream
.
available
(),
-
1
)
.
build
();
fileUrl
=
minioClient
.
getPresignedObjectUrl
(
getPresignedObjectUrlArgs
);
fileUrl
=
fileUrl
.
substring
(
0
,
fileUrl
.
indexOf
(
"?"
));
}
else
{
// 配置自定义文件路径域名
fileUrl
=
customDomain
+
'/'
+
bucketName
+
"/"
+
fileName
;
minioClient
.
putObject
(
putObjectArgs
);
// 返回文件路径
String
fileUrl
;
if
(
StrUtil
.
isBlank
(
customDomain
))
{
// 未配置自定义域名
GetPresignedObjectUrlArgs
getPresignedObjectUrlArgs
=
GetPresignedObjectUrlArgs
.
builder
()
.
bucket
(
bucketName
).
object
(
fileName
)
.
method
(
Method
.
GET
)
.
build
();
fileUrl
=
minioClient
.
getPresignedObjectUrl
(
getPresignedObjectUrlArgs
);
fileUrl
=
fileUrl
.
substring
(
0
,
fileUrl
.
indexOf
(
"?"
));
}
else
{
// 配置自定义文件路径域名
fileUrl
=
customDomain
+
'/'
+
bucketName
+
"/"
+
fileName
;
}
FileInfo
fileInfo
=
new
FileInfo
();
fileInfo
.
setName
(
fileName
);
fileInfo
.
setUrl
(
fileUrl
);
return
fileInfo
;
}
catch
(
Exception
e
)
{
throw
new
BizException
(
"文件上传失败"
);
}
FileInfo
fileInfo
=
new
FileInfo
();
fileInfo
.
setName
(
fileName
);
fileInfo
.
setUrl
(
fileUrl
);
return
fileInfo
;
}
/**
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录