diff --git a/src/main/java/com/zhgd/xmgl/util/Base64Util.java b/src/main/java/com/zhgd/xmgl/util/Base64Util.java index cc6759398..cf719be50 100644 --- a/src/main/java/com/zhgd/xmgl/util/Base64Util.java +++ b/src/main/java/com/zhgd/xmgl/util/Base64Util.java @@ -1,6 +1,7 @@ package com.zhgd.xmgl.util; import cn.hutool.core.io.FileUtil; +import cn.hutool.core.io.IORuntimeException; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.binary.Base64; @@ -72,8 +73,14 @@ public class Base64Util { * @param imgPath */ public static String convertFileToBase64(String imgPath) { - byte[] fileBytes = FileUtil.readBytes(imgPath); - return Base64.encodeBase64String(fileBytes); + try { + byte[] fileBytes = FileUtil.readBytes(imgPath); + return Base64.encodeBase64String(fileBytes); + } catch (IORuntimeException e) { + log.debug("",e); + log.error("本地文件(图片、excel等)转换成Base64字符串错误:{}",e.getMessage()); + } + return null; } /** diff --git a/src/test/java/com/zhgd/xmgl/util/Base64UtilTest.java b/src/test/java/com/zhgd/xmgl/util/Base64UtilTest.java new file mode 100644 index 000000000..e4e6a8b74 --- /dev/null +++ b/src/test/java/com/zhgd/xmgl/util/Base64UtilTest.java @@ -0,0 +1,36 @@ +package com.zhgd.xmgl.util; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mockStatic; + +@ExtendWith(MockitoExtension.class) +class Base64UtilTest { + + @Test + void convertFileToBase64_Success() { + String imgPath = TestResourceUtils.getTestImgPath(); + + // 执行测试 + String result = Base64Util.convertFileToBase64(imgPath); + + // 验证结果 + assertNotNull(result); + } + + + @Test + void convertFileToBase64_FileReadError() { + String imgPath = "不存在.jpg"; + + // 执行测试 + String result = Base64Util.convertFileToBase64(imgPath); + + // 验证结果 + assertNull(result); + } +} diff --git a/src/test/java/com/zhgd/xmgl/util/TestResourceUtils.java b/src/test/java/com/zhgd/xmgl/util/TestResourceUtils.java new file mode 100644 index 000000000..562582134 --- /dev/null +++ b/src/test/java/com/zhgd/xmgl/util/TestResourceUtils.java @@ -0,0 +1,44 @@ +package com.zhgd.xmgl.util; + +import org.jetbrains.annotations.NotNull; + +import java.io.File; +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.net.URLDecoder; +import java.nio.file.Paths; + +public class TestResourceUtils { + /** + * 获取测试图片路径 + * + * @return + */ + @NotNull + public static String getTestImgPath() { + // 从 classpath 获取测试文件路径 + ClassLoader classLoader = TestResourceUtils.class.getClassLoader(); + URL resourceUrl = classLoader.getResource("images/test.jpg"); + + if (resourceUrl == null) { + throw new RuntimeException("测试图片文件未找到: images/test.jpg"); + } + + try { + // 使用 URI 方式处理 URL 编码(如 %20) + URI uri = resourceUrl.toURI(); + return Paths.get(uri).toAbsolutePath().toString(); + } catch (URISyntaxException e) { + // 回退方案:使用 URLDecoder 解码 + try { + String decodedPath = URLDecoder.decode(resourceUrl.getFile(), "UTF-8"); + return new File(decodedPath).getAbsolutePath(); + } catch (UnsupportedEncodingException e2) { + throw new RuntimeException("路径解码失败", e2); + } + } + } + +} diff --git a/src/test/resources/images/test.jpg b/src/test/resources/images/test.jpg new file mode 100644 index 000000000..df8626e18 Binary files /dev/null and b/src/test/resources/images/test.jpg differ