Following steps are required:
- URI-encode the filename
- Replace the spaces in the encoded filename (we're using an URL encoder instead of URI encoder, but URL encoding uses
+
as encoded space instead of %20
, so we need to manually replace them with %20
).
- Set the encoded file name in the header. Here we have two variants: one which specifices the encoding, and one that doesn't. For maximal compatibility we can specify both.
Code:
String fileName = ...;
String encodedFileName = URLEncoder.encode(fileName,
StandardCharsets.UTF_8.name()).replace("+", "%20");
response.setHeader("Content-Disposition",
String.format("inline; filename*=UTF-8''%1$s; filename=%1$s", encodedFileName));
Example header:
inline; filename*=UTF-8''Hello%20World.doc; filename=Hello%20World.doc
Successfully tested with
- Firefox ✔
- Chrome ✔
- Edge ✔
- Internet Explorer ✔