重命名由Java Servlet流传输的PDF [英] Renaming a PDF streamed by Java Servlet
问题描述
使用XSLT,XSL-FO和Apache FOP的正确组合,我可以将PDF发送到某些浏览器窗口。
其实我发送的文件内容如下:
Using the right mix of XSLT, XSL-FO and Apache FOP, I am able to send a PDF into some browser window.
In fact, I am sending the file content as follows:
response.setContentType("application/pdf");
response.setContentLength( out.size());
response.getOutputStream().write( out.toByteArray());
response.getOutputStream().flush();
如预期的那样,浏览器会在名为pdf的选项卡中显示PDF内容,如果我在本地保存文件,默认为pdf.pdf
如何强制文件名?
As expected, the browser shows the PDF content in a tab named "pdf", and if I save the file locally, the name also defaults to pdf.pdf
How can I force the file name?
我尝试过以下其他方面没有工作:
I tried the following among other things that didn't work:
response.setHeader("Content-Disposition","inline; filename=" + filename + ".pdf" );
为了清楚起见,我想在浏览器中显示内容(而不是直接下载)
For clarity I want to display the content in the browser (as opposed to a straight download)
推荐答案
我发现解决方案这里
基本上,这是更改web.xml中的URL模式
浏览器将使用任何页面名称看起来像
我曾经有
Basically, it's a matter of changing the URL pattern in web.xml
The browser will use whatever the page name looks like
I used to have
<servlet-mapping>
<servlet-name>PDF</servlet-name>
<url-pattern>/pdf</url-pattern>
</servlet-mapping>
我替换为
<servlet-mapping>
<servlet-name>PDF</servlet-name>
<url-pattern>/pdf/*</url-pattern>
</servlet-mapping>
使用 http://wherever/pdf/filename.pdf调用servlet ?param1 = va1 ...
将文件名更改为filename.pdf
Invoking the servlet using http://wherever/pdf/filename.pdf?param1=va1...
changes the file name to "filename.pdf"
这篇关于重命名由Java Servlet流传输的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!