5

In javax.ws.rs.core.Response.ResponseBuilder, when I set filename, with polish (german, french, etc.) national characters, it changes file name before response is sent to client:

ResponseBuilder response = Response.ok(file);
response.header("Content-Disposition", "attachment; filename=żółty.txt");

After this, file is being downloaded as "-óBty.txt". How to fix this?

EDIT: Whole application works properly with UTF-8 (json content contains polish characters for example). Only http headers doesn't.

Mariusz Jaskółka
  • 4,137
  • 2
  • 21
  • 47

1 Answers1

5

Ok, I found a solution. According to RFC 6266 one should not use non-ASCII characters in the header, but use url-encoded values instead. There is special grammar for this case:

String encodedFileName = URLEncoder.encode(file.getName(), "UTF-8");
response.header("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFileName);
Community
  • 1
  • 1
Mariusz Jaskółka
  • 4,137
  • 2
  • 21
  • 47