UploadUtil.java
4.1 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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.config.BsthSystemConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
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;
/**
* 上传图片接口
*
* @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;
BufferedImage bufferedImage = null;
try {
content = FileUploadUtils.choosePrictureContent(content);
byte[] bytes = Base64.decode(content);
inputStream = new ByteArrayInputStream(bytes);
bufferedImage = ImageIO.read(inputStream);
if (bufferedImage.getWidth() < 0 || bufferedImage.getHeight() < 0) {
return ResponseResult.error("请上传正确的图片");
}
StringBuilder filePath = new StringBuilder();
filePath.append(bsthSystemConfig.getImageBasePath());
filePath.append(File.separator);
filePath.append(path);
File file = new File(filePath.toString());
if(!file.exists() || !file.isDirectory()){
FileUtils.forceMkdirParent(file);
}
ImageIO.write(bufferedImage, type, file);
bufferedImage.flush();
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;
try {
content = FileUploadUtils.choosePrictureContent(content);
byte[] bytes = Base64.decode(content);
StringBuilder filePath = new StringBuilder();
filePath.append(bsthSystemConfig.getImageBasePath());
filePath.append(File.separator);
filePath.append(path);
File file = new File(filePath.toString());
if(!file.exists() || !file.isDirectory()){
FileUtils.forceMkdirParent(file);
}
outputStream = new FileOutputStream(file);
outputStream.write(bytes, 0, bytes.length);
outputStream.flush();
return ResponseResult.success(Boolean.TRUE);
} catch (Exception e) {
log.error("上传视频异常:[{}]", path, e);
return ResponseResult.error("上传视频异常");
} finally {
if (Objects.nonNull(outputStream)) {
IOUtils.closeQuietly(outputStream);
}
}
}
}