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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With