gpt4 book ai didi

java - 不支持中文的 Content-Disposition 文件名

转载 作者:行者123 更新时间:2023-11-29 04:19:02 28 4
gpt4 key购买 nike

我一直在尝试下载带有中文文件名的附件,但不知何故它们的编码在下载时发生了变化,并且在有中文字符的地方保存了一些乱码文件名。

技术:Java服务器:Apache Tomcat

这是我已经尝试过的

response.setHeader("Content-Disposition", "attachment; filename="7_6_4_AM__2017_JS_003_南通凤凰服_B1_108"");

输出(下载的附件名称):“7_6_4_AM__2017_JS_003_W_äð”

我还尝试在引用之后将 * 附加到文件名指令:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
response.setHeader("Content-Disposition", "attachment; filename*="7_6_4_AM__2017_JS_003_南通凤凰服_B1_108"");

输出(下载的附件名称):“706.txt”

此外,

在我的研究中,我发现 HTTP header 消息不能携带 ISO-8859-1 字符集之外的字符。

https://www.rfc-editor.org/rfc/rfc5987

提前致谢。

最佳答案

尝试设置字符编码:

response.setCharacterEncoding("UTF-8");

您可能还想先对文件名进行编码:

filename= URLEncoder.encode(fileName, "UTF-8");

来自document

Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8. If the character encoding has already been set by setContentType(java.lang.String) or setLocale(java.util.Locale), this method overrides it. Calling setContentType(java.lang.String) with the String of text/html and calling this method with the String of UTF-8 is equivalent with calling setContentType with the String of text/html; charset=UTF-8. This method can be called repeatedly to change the character encoding. This method has no effect if it is called after getWriter has been called or after the response has been committed.

关于java - 不支持中文的 Content-Disposition 文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50408723/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com