Jersey ResponseBuilder file name encoding
In javax.ws.rs.core.Response.ResponseBuilder, when I set the filename, with Polish (German, French, etc.) national characters, it changes the filename before sending a response to the client:
ResponseBuilder response = Response.ok(file);
response.header("Content-Disposition", "attachment; filename=żółty.txt");
The file is then downloaded as "-óBty.txt". How to fix it?
EDIT: The whole application works correctly with UTF-8 (e.g. json content contains polish character). Only http headers don't work.
+3
source to share
2 answers
Ok I found a solution. According to RFC 6266 , you should not use non-ASCII characters in the header, but use URL encoded values instead. There is a special grammar for this case:
String encodedFileName = URLEncoder.encode(file.getName(), "UTF-8");
response.header("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFileName);
+3
source to share