Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Content-Disposition filename in Chinese not supported

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.

like image 874
Rhythm Singh Avatar asked May 18 '18 09:05

Rhythm Singh


1 Answers

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.

like image 174
Mạnh Quyết Nguyễn Avatar answered Oct 19 '22 21:10

Mạnh Quyết Nguyễn