Set filename of the Pdf that is streamed back to t

2020-05-27 10:33发布

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

 byte[] pdf = report.exportPdfToArray(user);
response.setContentType("application/pdf");
response.setHeader("content-disposition", "inline; filename=\"My.pdf\"");
outStream = response.getOutputStream();
outStream.write(pdf);
outStream.flush();
outStream.close();

The report is executed and it is sent back to the browser, but I can not control the name of the file even though I set the content-disposition. I am using Jboss 4.2.1. Do you know what am I missing?

EDIT: So is there any way to set the filename when the content-disposition is inline?

6条回答
混吃等死
2楼-- · 2020-05-27 11:07

I can't detect a flaw. Did you check the behavior with other browsers/readers?

As of RFC, it is not defined what the client has to do do with the filename information if displayed inline...

查看更多
孤傲高冷的网名
3楼-- · 2020-05-27 11:09

I have tried a solution in java and it worked.

response.setHeader("Content-Disposition","inline; filename=\"MyFile.pdf\"");
response.setContentType("application/pdf; name=\"MyFile.pdf\"");
response.getOutputStream().write(pdfAsBytesArray);
查看更多
虎瘦雄心在
4楼-- · 2020-05-27 11:12

content-disposition: attachment ....

查看更多
戒情不戒烟
5楼-- · 2020-05-27 11:21

MSIE will use the last part of the path info of the request URL (the part after the last /) as the default filename of the Save As action. It ignores the filename attribute of the Content-Disposition header altogether. All other browsers treat that header properly.

You need to change the URL pattern of your PDF servlet to a path mapping. I.e. do not use /pdf with http://example.com/context/pdf, but rather use /pdf/* with http://example.com/context/pdf/report.pdf. This way MSIE will use "report.pdf" instead of "pdf" as the default filename for the Save As action.

查看更多
倾城 Initia
6楼-- · 2020-05-27 11:23

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()));
查看更多
爱情/是我丢掉的垃圾
7楼-- · 2020-05-27 11:26

There is workaround to do so. We can use iframe where iframe will open in html page, iframe will hold the pdf report whereas the html page is independent of iframe. We can edit the title of html page that holds iframe.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
    <head>
        <title>${reportName}</title>
    </head>
    <body>
        <iframe src="/fcWeb/ReportPDFServlet" width="100%" height="100%"></iframe> 
    </body>
</html>
查看更多
登录 后发表回答