From 722d7be0eb89ceb33c2e8813d2d2b616ed68f04a Mon Sep 17 00:00:00 2001
From: wenminjie <1036699586@qq.com>
Date: 星期三, 22 七月 2026 15:17:45 +0800
Subject: [PATCH] 网络图片转base64 ->  本地图片转base64

---
 ruoyi-applet/src/main/java/com/ruoyi/web/controller/tool/ImageUtil.java                    |   62 ++++++++++++++++++++++--------
 ruoyi-applet/src/main/java/com/ruoyi/web/controller/api/SysApiVisitorRecordController.java |    2 
 2 files changed, 46 insertions(+), 18 deletions(-)

diff --git a/ruoyi-applet/src/main/java/com/ruoyi/web/controller/api/SysApiVisitorRecordController.java b/ruoyi-applet/src/main/java/com/ruoyi/web/controller/api/SysApiVisitorRecordController.java
index e5aa50b..88e5222 100644
--- a/ruoyi-applet/src/main/java/com/ruoyi/web/controller/api/SysApiVisitorRecordController.java
+++ b/ruoyi-applet/src/main/java/com/ruoyi/web/controller/api/SysApiVisitorRecordController.java
@@ -149,7 +149,7 @@
                     .map(id -> {
                         SysVisitArea area = sysVisitAreaMapper.selectById(Long.parseLong(id));
                         if (area == null) {
-                            return null;
+                            throw new ServiceException("访问区域不存在,请联系工作人员");
                         }
                         AreasList areas = new AreasList();
                         areas.setAreaCode(area.getAreaCode());
diff --git a/ruoyi-applet/src/main/java/com/ruoyi/web/controller/tool/ImageUtil.java b/ruoyi-applet/src/main/java/com/ruoyi/web/controller/tool/ImageUtil.java
index be6c283..b1c2d95 100644
--- a/ruoyi-applet/src/main/java/com/ruoyi/web/controller/tool/ImageUtil.java
+++ b/ruoyi-applet/src/main/java/com/ruoyi/web/controller/tool/ImageUtil.java
@@ -1,14 +1,19 @@
 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
@@ -25,20 +30,21 @@
             return null;
         }
 
+        String imagePath = RuoYiConfig.getUploadPath() + imageUrl.replace("/profile/upload", "");
+
         InputStream inputStream = null;
         ByteArrayOutputStream outputStream = null;
 
         try {
-            // 1. 创建URL连接
-            URL url = new URL(imageUrl);
-            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
-            connection.setRequestMethod("GET");
-            connection.setConnectTimeout(5000);
-            connection.setReadTimeout(10000);
-            connection.setDoInput(true);
+            // 1. 获取本地文件
+            File imageFile = new File(imagePath);
+            if (!imageFile.exists()) {
+                log.error("图片文件不存在:{}", imagePath);
+                return null;
+            }
 
-            // 2. 获取图片数据
-            inputStream = connection.getInputStream();
+            // 2. 读取图片数据
+            inputStream = Files.newInputStream(imageFile.toPath());
             outputStream = new ByteArrayOutputStream();
 
             byte[] buffer = new byte[8192];
@@ -51,16 +57,12 @@
             byte[] imageBytes = outputStream.toByteArray();
             String base64 = Base64.encodeBase64String(imageBytes);
 
-            // 4. 根据图片类型添加前缀
-            String contentType = connection.getContentType();
-            if (contentType != null && contentType.startsWith("image/")) {
-                return "data:" + contentType + ";base64," + base64;
-            }
-
-            return "data:image/jpeg;base64," + base64;
+            // 4. 根据文件扩展名添加前缀
+            String contentType = getContentType(imagePath);
+            return "data:" + contentType + ";base64," + base64;
 
         } catch (Exception e) {
-            log.error("图片转Base64失败,URL:{}", imageUrl, e);
+            log.error("本地图片转Base64失败,路径:{}", imagePath, e);
             return null;
         } finally {
             try {
@@ -75,4 +77,30 @@
             }
         }
     }
+
+    /**
+     * 根据文件扩展名获取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";
+        }
+    }
 }
\ No newline at end of file

--
Gitblit v1.7.1