diff --git a/src/main/java/com/zhgd/xmgl/modules/basicdata/controller/UploadFileController.java b/src/main/java/com/zhgd/xmgl/modules/basicdata/controller/UploadFileController.java index 6b89defe5..398078dd6 100644 --- a/src/main/java/com/zhgd/xmgl/modules/basicdata/controller/UploadFileController.java +++ b/src/main/java/com/zhgd/xmgl/modules/basicdata/controller/UploadFileController.java @@ -1,15 +1,18 @@ package com.zhgd.xmgl.modules.basicdata.controller; +import cn.hutool.core.util.StrUtil; import com.luciad.imageio.webp.WebPWriteParam; import com.zhgd.annotation.OperLog; import com.zhgd.jeecg.common.api.vo.Result; import com.zhgd.jeecg.common.execption.OpenAlertException; import com.zhgd.xmgl.modules.basicdata.service.UploadFileService; +import com.zhgd.xmgl.util.UrlUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.util.FileCopyUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -45,6 +48,10 @@ public class UploadFileController { @Resource private UploadFileService uploadFileService; + @Value("${serverUrl}") + private String serverUrl; + @Value("${publicServerUrl:}") + private String publicServerUrl; @PostMapping(value = "/image") public Map uploadImage(@RequestParam("files") MultipartFile[] files) { @@ -105,6 +112,9 @@ public class UploadFileController { }) public void getRenameFile(@RequestParam("fileUrl") String fileUrl, @RequestParam("fileName") String fileName, HttpServletResponse response) { try { + if (StrUtil.isNotBlank(publicServerUrl) && fileUrl.contains(publicServerUrl)) { + fileUrl = UrlUtil.replaceUrl(fileUrl, serverUrl); + } URL url = new URL(fileUrl); int index = fileUrl.indexOf("?");//第一个问号的位置 if (index > -1) { diff --git a/src/main/java/com/zhgd/xmgl/util/UrlUtil.java b/src/main/java/com/zhgd/xmgl/util/UrlUtil.java index 26ccd430d..b7f48afcb 100644 --- a/src/main/java/com/zhgd/xmgl/util/UrlUtil.java +++ b/src/main/java/com/zhgd/xmgl/util/UrlUtil.java @@ -10,6 +10,7 @@ import org.springframework.web.multipart.MultipartFile; import java.io.*; import java.net.HttpURLConnection; +import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap; import java.util.Map; @@ -241,10 +242,105 @@ public class UrlUtil { return fileName; } + /** + * 替换URL的协议和域名部分 + * + * @param originalUrl 原始URL + * @param newBaseUrl 新的基础URL(如:http://192.168.1.100 或 https://192.168.1.100:8080) + * @return 替换后的完整URL + * @throws MalformedURLException 当原始URL或新基础URL格式不正确时抛出异常 + */ + public static String replaceUrl(String originalUrl, String newBaseUrl) throws MalformedURLException { + if (originalUrl == null || originalUrl.trim().isEmpty()) { + throw new IllegalArgumentException("原始URL不能为空"); + } + if (newBaseUrl == null || newBaseUrl.trim().isEmpty()) { + throw new IllegalArgumentException("新基础URL不能为空"); + } + + // 解析原始URL + URL url = new URL(originalUrl); + + // 解析新的基础URL + URL newBase = new URL(newBaseUrl); + + // 构建新的URL:使用新基础URL的协议、主机和端口,保留原始URL的路径和查询参数 + URL newUrl = new URL(newBase.getProtocol(), newBase.getHost(), newBase.getPort(), url.getFile()); + + return newUrl.toString(); + } + + /** + * 安全版本的URL替换方法,如果替换失败则返回原始URL + * + * @param originalUrl 原始URL + * @param newBaseUrl 新的基础URL + * @return 替换后的URL,如果替换失败则返回原始URL + */ + public static String replaceUrlSafely(String originalUrl, String newBaseUrl) { + try { + return replaceUrl(originalUrl, newBaseUrl); + } catch (Exception e) { + // 记录日志(实际项目中应该使用日志框架) + System.err.println("URL替换失败,返回原始URL: " + e.getMessage()); + return originalUrl; + } + } + + /** + * 批量替换URL数组 + * + * @param originalUrls 原始URL数组 + * @param newBaseUrl 新的基础URL + * @return 替换后的URL数组 + */ + public static String[] replaceUrls(String[] originalUrls, String newBaseUrl) { + if (originalUrls == null) { + return new String[0]; + } + + String[] result = new String[originalUrls.length]; + for (int i = 0; i < originalUrls.length; i++) { + result[i] = replaceUrlSafely(originalUrls[i], newBaseUrl); + } + return result; + } + + // 测试示例 public static void main(String[] args) { - HashMap map = new HashMap<>(16); - map.put("k1", "v1"); - map.put("k", "v"); - System.out.println(urlJoin("http://baidu.com", map)); + try { + // 测试用例 + String[] testUrls = { + "https://api.example.com/v1/users", + "http://www.example.com:8080/path?query=123", + "https://sub.domain.com/api/data", + "ftp://files.example.com/download" + }; + + String newBaseUrl = "http://192.168.1.100:8080"; + + System.out.println("原始URLs:"); + for (String url : testUrls) { + System.out.println(" " + url); + } + + System.out.println("\n替换为: " + newBaseUrl); + System.out.println("\n替换结果:"); + + String[] replacedUrls = replaceUrls(testUrls, newBaseUrl); + for (String url : replacedUrls) { + System.out.println(" " + url); + } + + // 单个URL替换测试 + System.out.println("\n单个URL替换测试:"); + String original = "https://api.example.com:1234/v1/users?page=1"; + String replaced = replaceUrl(original, "http://localhost:3000"); + System.out.println("原始: " + original); + System.out.println("替换: " + replaced); + + } catch (Exception e) { + e.printStackTrace(); + } } }