-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathImageUtil.java
94 lines (78 loc) · 2.65 KB
/
ImageUtil.java
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
package site.yuyanjia.template.common.util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* 图片工具类
*
* @author seer
* @date 2018/6/13 9:11
*/
@Component
public class ImageUtil {
/**
* 图片base64前缀
*/
private static final String IMAGE_BASE64_DATA = "data";
/**
* 静态资源目录前缀·
*/
private static final String STATIC_LOCATION_PREFIX = "file:";
/**
* 静态资源路径
*/
private static String staticLocation;
public static void setStaticLocation(String staticLocation) {
if (staticLocation.startsWith(ImageUtil.STATIC_LOCATION_PREFIX)) {
staticLocation = staticLocation.substring(ImageUtil.STATIC_LOCATION_PREFIX.length());
}
ImageUtil.staticLocation = staticLocation;
}
/**
* 构建图片
*
* @param imgBase64 base64字符串
* @param relativeDirectoryPath 相对目录路径
* @param fileName 保存文件名
* @return 相对文件路径
* @throws IOException
*/
public static synchronized String buildImage(String imgBase64, String relativeDirectoryPath, String fileName, PrefixEnum prefixEnum) throws IOException {
if (StringUtils.startsWith(imgBase64, IMAGE_BASE64_DATA)) {
int inx = StringUtils.indexOf(imgBase64, ",");
imgBase64 = StringUtils.substring(imgBase64, inx + 1);
}
byte[] bytes = Base64Utils.decodeFromString(imgBase64);
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {
bytes[i] += 256;
}
}
String relativeFilePath = prefixEnum.toString() + "-" + fileName + ".jpg";
if (StringUtils.isNotEmpty(relativeDirectoryPath)) {
if (!relativeDirectoryPath.endsWith(File.separator)) {
relativeDirectoryPath += File.separator;
}
relativeFilePath = relativeDirectoryPath + relativeFilePath;
}
String absoluteFilePath = staticLocation + relativeFilePath;
File file = new File(absoluteFilePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdir();
}
try (OutputStream outputStream = new FileOutputStream(absoluteFilePath)) {
outputStream.write(bytes);
outputStream.flush();
}
return relativeFilePath;
}
/**
* 前缀枚举
*/
public enum PrefixEnum {
}
}