This commit is contained in:
zwt13703
2026-05-14 09:17:19 +08:00
parent a62d2fd664
commit 9313898f6c
3 changed files with 12 additions and 6 deletions
+7 -4
View File
@@ -11,8 +11,11 @@
## 会话 ID: 2 ## 会话 ID: 2
- [2026-05-14 10:00:00] - [2026-05-14 10:00:00]
- **执行原因**: 本地 file:// 路径中包含中文文件名时,urlEncoderencode 会将中文重新编码为百分号编码(如 日程 → %E6%97%A5%E7%A8%8B,导致文件系统无法找到文件。 - **执行原因**: 本地 file:// 路径中包含中文文件名时,多个环节会对中文进行百分号编码,导致文件系统无法找到文件。
- **执行过程**: - **执行过程**:
1. 排查发现 urlEncoderencode 方法会调用 URLEncoder.encode 对整个 URL 编码,中文被转回百分号编码 1. 排查发现 urlEncoderencode 会编码中文,在 onlinePreview 中为 file:// URL 跳过该调用
2. 在 onlinePreview 方法中增加判断:当 fileUrl 以 file:// 开头时,跳过 urlEncoderencode 调用 2. 用户反馈仍有编码问题,进一步排查发现 FileHandlerService.getFileAttribute() 的 else 分支也会对非 file:// URL 调用 encodeUrlFileName 编码中文
- **执行结果**: 本地文件路径中的中文字符保持原样,文件系统能正确找到文件 3. 同时排查发现 DownloadUtils.normalizedURL() 使用 Galimatias 解析 URL 时也会对中文编码
4. 在 FileHandlerService 中增加判断:file:// 开头的 URL 跳过 encodeUrlFileName 编码。
5. 在 DownloadUtils.handleFileProtocol() 中对 url.getPath() 进行 URLDecoder.decode 解码,作为兜底。
- **执行结果**: 三个编码点均已处理,中文文件名能正确传递给文件系统。
@@ -256,7 +256,7 @@ public class FileHandlerService {
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
e.printStackTrace(); e.printStackTrace();
} }
}else { }else if (!url.startsWith("file://")) {
url = Objects.requireNonNull(WebUtils.encodeUrlFileName(url)) url = Objects.requireNonNull(WebUtils.encodeUrlFileName(url))
.replaceAll("\\+", "%20") .replaceAll("\\+", "%20")
.replaceAll("%3A", ":") .replaceAll("%3A", ":")
@@ -14,6 +14,8 @@ import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.UUID; import java.util.UUID;
@@ -153,7 +155,8 @@ public class DownloadUtils {
// 处理file协议的文件下载 // 处理file协议的文件下载
private static void handleFileProtocol(URL url, String targetPath) throws IOException { private static void handleFileProtocol(URL url, String targetPath) throws IOException {
File sourceFile = new File(url.getPath()); String path = URLDecoder.decode(url.getPath(), StandardCharsets.UTF_8);
File sourceFile = new File(path);
if (!sourceFile.exists()) { if (!sourceFile.exists()) {
throw new FileNotFoundException("本地文件不存在: " + url.getPath()); throw new FileNotFoundException("本地文件不存在: " + url.getPath());
} }