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 924d47156..7635ee91e 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 @@ -105,15 +105,21 @@ public class UploadFileController { public void getRenameFile(@RequestParam("fileUrl") String fileUrl, @RequestParam("fileName") String fileName, HttpServletResponse response) { try { URL url = new URL(fileUrl); - int index = fileUrl.indexOf("?");//第一个问号的位置 + // 清理URL参数 + int index = fileUrl.indexOf("?"); if (index > -1) { fileUrl = fileUrl.substring(0, index); } - String[] split = fileUrl.split("\\."); - // 不同文件的MimeType参考后续链接 + // 从URL获取文件扩展名 + String fileExtension = getFileExtension(fileUrl); + // 检查fileName是否已包含扩展名 + String finalFileName = fileName; + if (!fileName.toLowerCase().endsWith("." + fileExtension.toLowerCase())) { + finalFileName = fileName + "." + fileExtension; + } response.setContentType("application/x-download");//下面三行是关键代码,处理乱码问题 response.setCharacterEncoding("utf-8"); - response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "iso8859-1") + "." + split[split.length - 1]); + response.setHeader("Content-Disposition", "attachment;filename=" + new String(finalFileName.getBytes("utf-8"), "iso8859-1")); URLConnection conn = url.openConnection(); InputStream inStream = conn.getInputStream(); OutputStream fos = response.getOutputStream(); @@ -123,10 +129,25 @@ public class UploadFileController { response.getOutputStream().close(); response.flushBuffer(); } catch (Exception e) { - log.error("err:", e); + log.error("下载文件失败:", e); } } + // 提取文件扩展名的辅助方法 + private String getFileExtension(String fileUrl) { + String cleanUrl = fileUrl; + int queryIndex = fileUrl.indexOf("?"); + if (queryIndex > -1) { + cleanUrl = fileUrl.substring(0, queryIndex); + } + + int lastDotIndex = cleanUrl.lastIndexOf("."); + if (lastDotIndex > -1 && lastDotIndex < cleanUrl.length() - 1) { + return cleanUrl.substring(lastDotIndex + 1); + } + return ""; // 如果没有扩展名,返回空字符串 + } + @OperLog(operModul = "转换图片成webp格式", operType = "", operDesc = "转换图片成webp格式") @PostMapping("/convertBase642webp") public Result convertBase642webp(@RequestBody Map request,