Content-Disposition 不支持中文文件名

Content-Disposition filename in Chinese not supported

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

技术:Java 服务器:阿帕奇 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.