zjk
2026-07-24 3ee306e4a8db75680960f5eb4eec9fb7d05b5f29
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
package com.ruoyi.web.controller.tool;
 
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
 
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
 
@Slf4j
@Component
public class ImageUtil {
 
    /**
     * 将网络图片URL转为Base64编码
     *
     * @param imageUrl 网络图片地址
     * @return Base64编码的图片字符串
     */
    public static String imageUrlToBase64(String imageUrl) {
        if (StringUtils.isEmpty(imageUrl)) {
            return null;
        }
 
        String imagePath = RuoYiConfig.getUploadPath() + imageUrl.replace("/profile/upload", "");
 
        InputStream inputStream = null;
        ByteArrayOutputStream outputStream = null;
 
        try {
            // 1. 获取本地文件
            File imageFile = new File(imagePath);
            if (!imageFile.exists()) {
                log.error("图片文件不存在:{}", imagePath);
                return null;
            }
 
            // 2. 读取图片数据
            inputStream = Files.newInputStream(imageFile.toPath());
            outputStream = new ByteArrayOutputStream();
 
            byte[] buffer = new byte[8192];
            int len;
            while ((len = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
 
            // 3. 转换为Base64
            byte[] imageBytes = outputStream.toByteArray();
            String base64 = Base64.encodeBase64String(imageBytes);
 
            // 4. 根据文件扩展名添加前缀
            String contentType = getContentType(imagePath);
            return "data:" + contentType + ";base64," + base64;
 
        } catch (Exception e) {
            log.error("本地图片转Base64失败,路径:{}", imagePath, e);
            return null;
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (Exception e) {
                log.error("关闭流失败", e);
            }
        }
    }
 
    /**
     * 根据文件扩展名获取Content-Type
     */
    private static String getContentType(String imagePath) {
        if (StringUtils.isEmpty(imagePath)) {
            return "image/jpeg";
        }
        String ext = imagePath.substring(imagePath.lastIndexOf(".") + 1).toLowerCase();
        switch (ext) {
            case "png":
                return "image/png";
            case "gif":
                return "image/gif";
            case "bmp":
                return "image/bmp";
            case "webp":
                return "image/webp";
            case "svg":
                return "image/svg+xml";
            case "jpg":
            case "jpeg":
            default:
                return "image/jpeg";
        }
    }
}