Commit 93dd97afb79157cf81027cc4a628f8cfff197167
1 parent
472993e2
fix(infra): 修复文件 URL 特殊字符编码问题
- 新增 URL Path 编解码工具,保留 + 字符语义 - 文件访问基于原始 URI 解码 path,避免中文、空格、% 等字符下载异常 - 文件客户端生成 URL 时按路径段编码,S3 兼容完整 URL 和裸 path - 优化 Content-Disposition 文件名,支持 filename 和 filename* - 下载文件名优先使用文件记录的原始名称 - 补充 HTTP、Local、S3、文件服务相关单测
Showing
1 changed file
with
32 additions
and
0 deletions
yudao-module-infra/src/test/java/cn/iocoder/yudao/module/infra/framework/file/core/utils/FileTypeUtilsTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.infra.framework.file.core.utils; | ||
| 2 | + | ||
| 3 | +import org.junit.jupiter.api.Test; | ||
| 4 | +import org.springframework.mock.web.MockHttpServletResponse; | ||
| 5 | + | ||
| 6 | +import java.nio.charset.StandardCharsets; | ||
| 7 | + | ||
| 8 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| 9 | +import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| 10 | + | ||
| 11 | +/** | ||
| 12 | + * {@link FileTypeUtils} 的单元测试 | ||
| 13 | + */ | ||
| 14 | +public class FileTypeUtilsTest { | ||
| 15 | + | ||
| 16 | + @Test | ||
| 17 | + public void testWriteAttachment_contentDispositionEncodeFilename() throws Exception { | ||
| 18 | + // 准备参数 | ||
| 19 | + MockHttpServletResponse response = new MockHttpServletResponse(); | ||
| 20 | + byte[] content = "test".getBytes(StandardCharsets.UTF_8); | ||
| 21 | + | ||
| 22 | + // 调用 | ||
| 23 | + FileTypeUtils.writeAttachment(response, "中文 100%+文件.txt", content); | ||
| 24 | + | ||
| 25 | + // 断言 | ||
| 26 | + assertEquals("attachment;filename=\"__ 100%+__.txt\";" | ||
| 27 | + + "filename*=UTF-8''%E4%B8%AD%E6%96%87%20100%25+%E6%96%87%E4%BB%B6.txt", | ||
| 28 | + response.getHeader("Content-Disposition")); | ||
| 29 | + assertArrayEquals(content, response.getContentAsByteArray()); | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | +} |