修复编码转换问题

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();
pdfFilePath = pdfFilePath.replace(fileDir, "");
String pdfFolder = pdfFilePath.substring(0, pdfFilePath.length() - 4);
String urlPrefix;
try {
urlPrefix = baseUrl + URLEncoder.encode(pdfFolder, uriEncoding).replaceAll("\\+", "%20");
} catch (UnsupportedEncodingException e) {
logger.error("UnsupportedEncodingException", e);
urlPrefix = baseUrl + pdfFolder;
}
return urlPrefix + "/" + index + PDF2JPG_IMAGE_FORMAT;
// 对整个路径进行编码,包括特殊字符
String encodedPath = URLEncoder.encode(pdfFolder, StandardCharsets.UTF_8);
encodedPath = encodedPath
.replaceAll("%2F", "/") // 恢复斜杠
.replaceAll("%5C", "/") // 恢复反斜杠
.replaceAll("\\+", "%20"); // 空格处理
// 构建URL使用_作为分隔符这是kkFileView压缩包预览的常见格式
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) {
try {
name = URLEncoder.encode(name, "UTF-8").replaceAll("\\+", "%20");
} catch (UnsupportedEncodingException e) {
return null;
}
name = URLEncoder.encode(name, StandardCharsets.UTF_8)
.replaceAll("%2F", "/") // 恢复斜杠
.replaceAll("%5C", "/") // 恢复反斜杠
.replaceAll("\\+", "%20"); // 空格处理
return name;
}