From c90848855e6d92e3cf7c8214bc2214d74f988866 Mon Sep 17 00:00:00 2001 From: fl <3098731433@qq.com> Date: Fri, 4 Jul 2025 13:14:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=BA=E5=91=98=E4=B8=8B=E5=8F=91=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../robot/util/IntelligentLibraryUtil.java | 37 ++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/bonus-business/src/main/java/com/bonus/business/robot/util/IntelligentLibraryUtil.java b/bonus-business/src/main/java/com/bonus/business/robot/util/IntelligentLibraryUtil.java index 2a8e282..4b7b017 100644 --- a/bonus-business/src/main/java/com/bonus/business/robot/util/IntelligentLibraryUtil.java +++ b/bonus-business/src/main/java/com/bonus/business/robot/util/IntelligentLibraryUtil.java @@ -175,11 +175,7 @@ public class IntelligentLibraryUtil { public static String addPersonImage(PersonImageVo param){ try { - // 假设我们有一个URL - URL url = new URL(param.getImagePath()); - // 将URL转换为File - File file = new File(url.getFile()); - File[] imageData = {file}; // + File[] imageData = convertUrlToFile(param.getImagePath()); // DigestAuthClient client = new DigestAuthClient(DIGEST_USER, md5(DIGEST_PWD)); // 定义需要添加的表单数据 Map formData = new HashMap<>(); @@ -192,6 +188,37 @@ public class IntelligentLibraryUtil { } } + public static File[] convertUrlToFile(String imageUrl) throws Exception { + // 打开URL输入流 + try (InputStream in = new URL(imageUrl).openStream()) { + // 创建临时文件(保留原始扩展名) + String suffix = getExtensionFromUrl(imageUrl); + File tempFile = File.createTempFile("image-", suffix); + tempFile.deleteOnExit(); // 程序退出时自动删除 + + // 写入到临时文件 + try (FileOutputStream out = new FileOutputStream(tempFile)) { + byte[] buffer = new byte[1024]; + int bytesRead; + while ((bytesRead = in.read(buffer)) != -1) { + out.write(buffer, 0, bytesRead); + } + } + + return new File[]{tempFile}; + } + } + + // 从URL中提取后缀名,比如 ".jpg", ".png" + private static String getExtensionFromUrl(String url) { + String lowerCaseUrl = url.toLowerCase(); + if (lowerCaseUrl.endsWith(".jpg") || lowerCaseUrl.endsWith(".jpeg")) return ".jpg"; + if (lowerCaseUrl.endsWith(".png")) return ".png"; + if (lowerCaseUrl.endsWith(".gif")) return ".gif"; + if (lowerCaseUrl.endsWith(".bmp")) return ".bmp"; + return ".tmp"; // 默认后缀 + } + public static File[] convertImageUrlToFile(String imageUrl, int width, int height) throws IOException { // 从给定的 URL 获取图像数据 try (InputStream inputStream = new URL(imageUrl).openStream()) {