Home » excel » java – GWT/Apache POI Download Servlet: Incorrect filename on download

java – GWT/Apache POI Download Servlet: Incorrect filename on download

Posted by: admin May 14, 2020 Leave a comment


I have a servlet that creates a dynamic Excel spreadsheet using Apache POI. I can create the spreadsheet just fine. However, when I use Google Chrome and a Tomcat server, the downloaded file does not reflect the correct filename. Instead, it replaces the filename with the name of the servlet. So, for example, I want to download a file “dr_3.xlsx”, it would instead download a file called “excelService” (the name of the servlet). All the contents are correct, just the file name is wrong.

This bug only occurs when using Google Chrome on a Tomcat Server. It performs as expected when using Firefox + Tomcat, Chrome + Jetty, Firefox + Jetty, and IE(eww) + Tomcat.

This is the code for the doGet() method:

protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException
        int id = Integer.parseInt(req.getParameter("id"));
        int BUFFER = 1024 * 100;
        resp.setContentType( "application/octet-stream" );
        resp.setHeader( "Content-Disposition:", String.format("attachment; filename=\"%s\"", "dr_" + id + ".xlsx"));
        OutputStream outputStream = resp.getOutputStream();
        byte[] bytes = buildFile(id);
        resp.setBufferSize( BUFFER );

Just to reiterate: The Excel file is fine. The name of the file is incorrect.

Thanks ahead of time for your help.

How to&Answers:

Thanks to Fedy2, removing the colon from "Content-Disposition" worked.


Try without the quotes:

resp.setHeader( "Content-Disposition:", String.format("attachment; filename=%s", "dr_" + id + ".xlsx"));

Also, check out this SO discussion.