Search code examples
javaspring-boothttputf-8content-disposition

Content-Disposition filename not being set correctly in Spring Boot 2.7.11


I'm using Spring Boot 2.7.11 and I'm having an issue with the Content-Disposition header not being set correctly when downloading a file with a Japanese filename. Specifically, when I download the file, the filename is being changed to "files.zip" instead of the correct filename.

I've confirmed that the filename is being passed correctly to the writeContentDisposition() method, and I'm setting the Content-Disposition header correctly with the filename parameter encoded as UTF-8.

Here's an example of the header value that I'm passing:

attachment; filename="日本語頑張る.zip"; filename*=UTF-8''%E6%97%A5%E6%9C%AC%E8%AA%9E%E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%83%86%E3%82%B9%E3%83%88.zip

This works correctly in Spring Boot 2.6.11, but in Spring Boot 2.7.11, the downloaded file is named "files.zip" instead of the correct filename.

Here's the code that I'm using to set the Content-Disposition header:

public static void setContentDisposition(
    HttpServletResponse response, String filename, boolean download) {
        String encodedFilename = UriUtils.encode(filename, StandardCharsets.UTF_8).replaceAll("\\+",
   "%20");
      String disposition = (download ? "attachment" : "inline") +
        "; filename=\"" + filename + "\"" +
        "; filename*=UTF-8''" + encodedFilename;
      response.setHeader(HttpHeaders.CONTENT_DISPOSITION, disposition);
}

I'm not sure why this isn't working in Spring Boot 2.7.11. Can anyone help me figure out what might be causing this issue?


Solution

  • The problem likely is caused by literal non-ASCII characters in the field value, which might cause Spring not to set the value at all.

    Remove the "filename" (not "filename*") parameter and try again.