UploadUtil.java
3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.ruoyi.utils;
import cn.hutool.core.codec.Base64;
import com.ruoyi.common.core.domain.ResponseResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MinioConfig;
import com.ruoyi.config.BsthSystemConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.util.Objects;
/**
* @author liujun
* @date 2024年07月25日 14:13
*/
@Slf4j
@Component
public class UploadUtil {
@Autowired
private BsthSystemConfig bsthSystemConfig;
@Autowired
private MinioConfig minioConfig;
/**
* 上传图片接口
*
* @param content 图片内容
* @param path 图片路径(相对路径)
* @param type 图片类型
* @return com.ruoyi.common.core.domain.ResponseResult<java.lang.Boolean>
* @author liujun
* @date 2024/7/25 14:31
*/
public ResponseResult<Boolean> uploadImageOfBase64(String content, String path, String type) {
if (StringUtils.isEmpty(content)) {
return ResponseResult.error("上传的图片内容为空");
}
ByteArrayInputStream inputStream = null;
try {
content = FileUploadUtils.choosePrictureContent(content);
byte[] bytes = Base64.decode(content);
inputStream = new ByteArrayInputStream(bytes);
minioConfig.upload(inputStream, path, "application/" + type);
return ResponseResult.success(Boolean.TRUE);
} catch (Exception e) {
log.error("上传图片异常:[{}]", path, e);
return ResponseResult.error("上传图片异常");
} finally {
if (Objects.nonNull(inputStream)) {
IOUtils.closeQuietly(inputStream);
}
}
}
/**
* 上传视频文件
*
* @param content
* @param path
* @return com.ruoyi.common.core.domain.ResponseResult<java.lang.Boolean>
* @author liujun
* @date 2024/7/25 14:37
*/
public ResponseResult<Boolean> uploadVideoOfBase64(String content, String path) {
if (StringUtils.isEmpty(content)) {
return ResponseResult.error("上传的视频内容为空");
}
FileOutputStream outputStream = null;
ByteArrayInputStream inputStream = null;
try {
content = FileUploadUtils.choosePrictureContent(content);
byte[] bytes = Base64.decode(content);
inputStream = new ByteArrayInputStream(bytes);
minioConfig.upload(inputStream, path, "application/mp4");
return ResponseResult.success(Boolean.TRUE);
} catch (Exception e) {
log.error("上传视频异常:[{}]", path, e);
return ResponseResult.error("上传视频异常");
} finally {
if (Objects.nonNull(outputStream)) {
IOUtils.closeQuietly(outputStream);
}
}
}
}