Set filename of the Pdf that is streamed back to the browser

后端 未结 6 1051
太阳男子
太阳男子 2021-02-06 03:01

I have a Java webapp creating a pdf and streaming it back to the browser.

 byte[] pdf = report.exportPdfToArray(user);
response.setContentType(\"application/pdf         


        
6条回答
  •  耶瑟儿~
    2021-02-06 03:09

    It's weird but it can be useful for someone(maybe someone can tell what is wrong with it):

    When I set two headers like:

    response.addHeader("content-length", String.valueOf(((FileInputStream) is).getChannel().size()));
    response.addHeader("Content-disposition", "attachment; filename=\"MyFileName.doc\"");
    

    It doesn't work. But when I change the order it works as expected:

    response.addHeader("Content-disposition", "attachment; filename=\"MyFileName.doc\"");
    response.addHeader("content-length", String.valueOf(((FileInputStream) is).getChannel().size()));
    

提交回复
热议问题