wenminjie
2026-07-24 6f75f9dd08088b3d2a6ff341fff32f28009a6967
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package com.ruoyi.web.controller.tool;
 
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.file.FileUploadUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.*;
 
@Slf4j
public class FaceBase64ToUrlUtil {
    /**
     * 处理访客人像(Base64转URL)
     * 使用若依的 FileUploadUtils,返回完整URL
     *
     * @param faceImageData Base64图片数据
     * @param idCard 身份证号(用于生成文件名)
     * @return 图片完整URL
     */
    public static String processFaceImage(String faceImageData, String idCard) {
        if (!StringUtils.hasText(faceImageData)) {
            return null;
        }
 
        try {
            // 判断是否为Base64格式
            if (isBase64Image(faceImageData)) {
                // 1. Base64转MultipartFile
                MultipartFile file = base64ToMultipartFile(faceImageData, "face_" + idCard);
                if (file == null) {
                    log.error("Base64转文件失败");
                    return null;
                }
 
                // 2. 使用若依的FileUploadUtils上传,返回完整URL
                String imageUrl = FileUploadUtils.upload(file);
                log.info("访客人像上传成功,URL: {}", imageUrl);
                return imageUrl;
            } else {
                // 如果已经是URL,直接返回
                log.info("访客人像已是URL格式,直接使用: {}", faceImageData);
                return faceImageData;
            }
        } catch (Exception e) {
            log.error("处理访客人像失败,身份证号: {}", idCard, e);
            return null;
        }
    }
 
    /**
     * 判断是否为Base64图片格式
     */
    private static boolean isBase64Image(String data) {
        if (!StringUtils.hasText(data)) {
            return false;
        }
        return data.contains("data:image/") ||
                data.contains("base64") ||
                (data.length() > 100 && !data.startsWith("http"));
    }
 
    /**
     * Base64转MultipartFile
     */
    private static MultipartFile base64ToMultipartFile(String base64Data, String fileName) {
        try {
            // 去除Base64前缀
            String[] base64Array = base64Data.split(",");
            String base64Str = base64Array.length > 1 ? base64Array[1] : base64Array[0];
 
            // 获取图片格式
            String imageType = getImageType(base64Data);
            if (StrUtil.isBlank(imageType)) {
                imageType = "jpg";
            }
 
            // 解码Base64
            byte[] imageBytes = cn.hutool.core.codec.Base64.decode(base64Str);
 
            // 创建临时文件
            String fullFileName = fileName + "." + imageType;
            File tempFile = File.createTempFile("upload_", "_" + fullFileName);
            tempFile.deleteOnExit();
 
            // 写入数据
            try (FileOutputStream fos = new FileOutputStream(tempFile)) {
                fos.write(imageBytes);
                fos.flush();
            }
 
            // 转换为MultipartFile
            return new CustomMultipartFile(tempFile, fullFileName);
 
        } catch (Exception e) {
            log.error("Base64转MultipartFile失败", e);
            return null;
        }
    }
 
    /**
     * 获取图片格式
     */
    private static String getImageType(String base64Data) {
        if (StrUtil.isBlank(base64Data)) {
            return "jpg";
        }
 
        if (base64Data.contains("data:image/")) {
            String prefix = base64Data.substring(0, base64Data.indexOf(","));
            if (prefix.contains("png")) {
                return "png";
            } else if (prefix.contains("jpg") || prefix.contains("jpeg")) {
                return "jpg";
            } else if (prefix.contains("gif")) {
                return "gif";
            } else if (prefix.contains("bmp")) {
                return "bmp";
            } else if (prefix.contains("webp")) {
                return "webp";
            }
        }
 
        return "jpg";
    }
 
    /**
     * 自定义MultipartFile实现
     */
    public static class CustomMultipartFile implements MultipartFile {
        private final File file;
        private final String fileName;
 
        public CustomMultipartFile(File file, String fileName) {
            this.file = file;
            this.fileName = fileName;
        }
 
        @Override
        public String getName() {
            return fileName;
        }
 
        @Override
        public String getOriginalFilename() {
            return fileName;
        }
 
        @Override
        public String getContentType() {
            String extension = getFileExtension(fileName);
            switch (extension) {
                case "png":
                    return "image/png";
                case "jpg":
                case "jpeg":
                    return "image/jpeg";
                case "gif":
                    return "image/gif";
                case "bmp":
                    return "image/bmp";
                case "webp":
                    return "image/webp";
                default:
                    return "image/jpeg";
            }
        }
 
        @Override
        public boolean isEmpty() {
            return file == null || file.length() == 0;
        }
 
        @Override
        public long getSize() {
            return file.length();
        }
 
        @Override
        public byte[] getBytes() throws IOException {
            byte[] bytes = new byte[(int) file.length()];
            try (FileInputStream fis = new FileInputStream(file)) {
                fis.read(bytes);
            }
            return bytes;
        }
 
        @Override
        public InputStream getInputStream() throws IOException {
            return new FileInputStream(file);
        }
 
        @Override
        public void transferTo(File dest) throws IOException, IllegalStateException {
            try (FileInputStream fis = new FileInputStream(file);
                 FileOutputStream fos = new FileOutputStream(dest)) {
                byte[] buffer = new byte[1024];
                int length;
                while ((length = fis.read(buffer)) > 0) {
                    fos.write(buffer, 0, length);
                }
                fos.flush();
            }
        }
 
        private String getFileExtension(String fileName) {
            if (fileName == null || fileName.isEmpty()) {
                return "";
            }
            int lastDotIndex = fileName.lastIndexOf(".");
            if (lastDotIndex > 0 && lastDotIndex < fileName.length() - 1) {
                return fileName.substring(lastDotIndex + 1).toLowerCase();
            }
            return "";
        }
    }
}