Content-Disposition filename in Chinese not supported
Asked Answered
S

2

5

I have been trying to download attachment with Chinese filename but somehow their encoding changes while downloading and some gibberish filename is saved where there are Chinese chararchters.

Technology: Java Server: Apache Tomcat

This is what I've tried already

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

Output(Downloaded attachment name): "7_6_4_AM__2017_JS_003_W_äð"

I've also tried appending * to filename directive after referring to :

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"");

Output(Downloaded attachment name): "706.txt"

Also,

In my research I found that HTTP header messages cannot carry characters outside the ISO-8859-1 character set.

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

Thanks in Advance.

Shantay answered 18/5, 2018 at 9:59 Comment(0)
B
7

Try to set character encoding:

response.setCharacterEncoding("UTF-8");

You might also want to encode your filename first:

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

From the 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.

Brisesoleil answered 18/5, 2018 at 10:3 Comment(3)
Already tried response.setCharacterEncoding("UTF-8");Let me check with fileName encoding.Shantay
Did you encode your filename? Did you setCharacterEncoding before calling to getWriter()?Impact
Just remember to decode the filename after receiving the response. I forgot that.Sill
E
0

I came here from node.js and it doesn't work for me. Here is the way of fixing it in node.js

 const encodedFileName = encodeURIComponent(fileName)
 reply.header('Content-Disposition', `attachment; filename*=UTF-8''${encodedFileName}`)

By the way if you omit UTF-8'' part, some browsers might not handle non-ASCII characters correctly in the filename.

Emeritaemeritus answered 17/10 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.