修复编码转换问题

This commit is contained in:
高雄
2025-12-29 10:20:59 +08:00
parent 4afe1caa33
commit 38a9c142e2
2 changed files with 14 additions and 13 deletions

View File

@@ -199,14 +199,16 @@ public class FileHandlerService implements InitializingBean {
String baseUrl = BaseUrlFilter.getBaseUrl(); String baseUrl = BaseUrlFilter.getBaseUrl();
pdfFilePath = pdfFilePath.replace(fileDir, ""); pdfFilePath = pdfFilePath.replace(fileDir, "");
String pdfFolder = pdfFilePath.substring(0, pdfFilePath.length() - 4); String pdfFolder = pdfFilePath.substring(0, pdfFilePath.length() - 4);
String urlPrefix; // 对整个路径进行编码,包括特殊字符
try { String encodedPath = URLEncoder.encode(pdfFolder, StandardCharsets.UTF_8);
urlPrefix = baseUrl + URLEncoder.encode(pdfFolder, uriEncoding).replaceAll("\\+", "%20"); encodedPath = encodedPath
} catch (UnsupportedEncodingException e) { .replaceAll("%2F", "/") // 恢复斜杠
logger.error("UnsupportedEncodingException", e); .replaceAll("%5C", "/") // 恢复反斜杠
urlPrefix = baseUrl + pdfFolder; .replaceAll("\\+", "%20"); // 空格处理
} // 构建URL使用_作为分隔符这是kkFileView压缩包预览的常见格式
return urlPrefix + "/" + index + PDF2JPG_IMAGE_FORMAT; String url = baseUrl + encodedPath + "/" + index + PDF2JPG_IMAGE_FORMAT;
return url;
} }
/** /**

View File

@@ -61,11 +61,10 @@ public class WebUtils {
* *
*/ */
public static String encodeFileName(String name) { public static String encodeFileName(String name) {
try { name = URLEncoder.encode(name, StandardCharsets.UTF_8)
name = URLEncoder.encode(name, "UTF-8").replaceAll("\\+", "%20"); .replaceAll("%2F", "/") // 恢复斜杠
} catch (UnsupportedEncodingException e) { .replaceAll("%5C", "/") // 恢复反斜杠
return null; .replaceAll("\\+", "%20"); // 空格处理
}
return name; return name;
} }