czt
2026-01-09 2c7f0f8bc83624fa5fc7092b7bdb361b867fbcf2
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package com.fzzy.igds.utils;
 
import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
 
import java.io.*;
import java.util.Base64;
 
/**
 * @Description 图片Base处理
 * @Author CZT
 * @Date 2026/1/7 11:07
 */
@Slf4j
public class Base64Util {
 
    public static String BASE_IMG_START = "data:image/jpg;base64,";
 
    /**
     * 根据地址,把图片转换为Base64字符串
     * <p>
     * data:image/jpg;base64,
     *
     * @param diskFile
     * @return
     */
    public static String getImageStr(String diskFile) {
        InputStream in = null;
        byte[] data = null;
        // 读取图片字节数组
        try {
            in = new FileInputStream(diskFile);
            data = new byte[in.available()];
            in.read(data);
            // in.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != in)
                    in.close();
            } catch (Exception e) {
            }
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return BASE_IMG_START + encoder.encode(data);
    }
 
    /**
     * 对字节数组字符串进行Base64解码并生成图片
     *
     * @param imgStr
     * @return
     */
    public static boolean generateImage(String imgStr, String diskFile) {
        if (imgStr == null)
            return false;
 
        if (imgStr.startsWith("data:")) {
            imgStr = imgStr.substring(BASE_IMG_START.length());
        }
        BASE64Decoder decoder = new BASE64Decoder();
        OutputStream out = null;
        try {
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            // 生成jpeg图片
            out = new FileOutputStream(diskFile);
            out.write(b);
            out.flush();
            // out.close();
            return true;
        } catch (Exception e) {
            return false;
        } finally {
            try {
                if (null != out)
                    out.close();
            } catch (Exception e) {
 
            }
        }
    }
 
 
    /**
     * base64转为图片
     *
     * @param path   文件路径:到文件夹即可,代码里会在文件夹里生成对应的jpg文件
     * @param base64
     * @return
     */
    public static String base64ToJpg(String path, String fileName, String base64) {
        // 判断文件路径是否存在
        File filePath = new File(path);
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        // 创建文件
        String jpgFile = path + "\\" + fileName + ".jpg";
        File file = new File(jpgFile);
        boolean jpgFileExist = false;
        try {
            jpgFileExist = file.createNewFile();
            log.info("jpg文件创建成功");
        } catch (IOException e) {
            log.info("jpg文件创建失败");
            e.printStackTrace();
        }
        if (jpgFileExist) {
            // 解密
            Base64.Decoder decoder = Base64.getDecoder();
            // 去掉base64前缀 data:image/jpeg;base64,
            base64 = base64.substring(base64.indexOf(",", 1) + 1, base64.length());
            byte[] b = decoder.decode(base64);
            // 处理数据
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            // 保存图片
            try {
                FileOutputStream out = new FileOutputStream(jpgFile);
                out.write(b);
                out.flush();
                out.close();
                // 写入成功返回文件路径
                return jpgFile;
            } catch (FileNotFoundException e) {
                log.info("文件未找到");
                e.printStackTrace();
            } catch (IOException e) {
                log.info("写入失败");
                e.printStackTrace();
            }
        }
        return "文件不存在";
    }
}