java spring-mvc (portlet):如何在打开的文件对话框中返回一个 pdf 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4592709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
spring-mvc (portlet): how to return a pdf file in open file dialog?
提问by Hyman
In my @ActionMapping
I create a PDF file for the user.
Now I was wondering how I can return this pdf to the user in the form of a save/open file dialog box?
I'd prefer this over showing a download link if the generation was succesful.
在我的@ActionMapping
我为用户创建了一个 PDF 文件。现在我想知道如何以保存/打开文件对话框的形式将这个 pdf 返回给用户?如果生成成功,我更喜欢显示下载链接。
I'm using spring-mvc 3.0.5 in combination with portlets. But if anyone has some pointers for a normal application then I can probably figure it out from there. For 2.0 I read something about extending a pdfgenerator class and twidling in the web.xml but since nowadays we just need POJO's....
我将 spring-mvc 3.0.5 与 portlet 结合使用。但是如果有人对普通应用程序有一些指示,那么我可能可以从那里弄清楚。对于 2.0,我读了一些关于扩展 pdfgenerator 类和在 web.xml 中的 twidling 但因为现在我们只需要 POJO ......
Edit: Code after Adeel's suggestion:
编辑:Adeel 建议后的代码:
File file = new File("C:\test.pdf");
response.setContentType("application/pdf");
try {
byte[] b = new byte[(int) file.length()];
OutputStream out = response.getPortletOutputStream();
out.write(new FileInputStream(file).read(b));
out.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "users/main";
回答by Adeel Ansari
You can write that file directly to your response writer
, and don't forget to change the contentType
. For example,
您可以将该文件直接写入您response writer
的contentType
. 例如,
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment;filename=something.pdf");
OutputStream out = response.getOutputStream();
out.write(pdfFileContentInBytes);
out.flush();
Well, I thought its a HttpServletResponse
what you have, but its not the case. As you are working with Portlet, its a RenderResponse
object. After searching over the Internet, I found few links which might be helpful to you in this regard.
好吧,我认为这HttpServletResponse
是你所拥有的,但事实并非如此。当您使用 Portlet 时,它是一个RenderResponse
对象。在互联网上搜索后,我发现了几个可能对您有所帮助的链接。
First take an example of Lotus Form Server Portlet, its showing the way how to allow multiple mime-type while configuring the portlet using
portlet.xml
.Here is Spring Portlet docs, its showing how we configure portlet using portlet.xml. It has an XML element about mime-type, see if you can give the value,
application/pdf
, there.
首先以Lotus Form Server Portlet为例,它展示了如何在使用
portlet.xml
.这是Spring Portlet 文档,它展示了我们如何使用 portlet.xml 配置 portlet。它有一个关于 mime-type 的 XML 元素,看看你能不能给出值,
application/pdf
,那里。
Another idea is to change your parameter to ActionResponse response
, instead of RenderResponse response
. I am a bit blur here, not sure what is your super class? what method is it? etc....
另一个想法是将您的参数更改为ActionResponse response
, 而不是RenderResponse response
。我这里有点模糊,不知道你的超级班是什么?是什么方法?等等....
To me, it seems that the problem is allowed/not-allowed mime-types for the portlet response.
对我来说,问题似乎是允许/不允许 portlet 响应的 MIME 类型。
回答by PeterR
in spring mvc, ResourceResponse response
在 spring mvc 中,ResourceResponse 响应
response.reset();
response.setContentType("application/pdf");
response.setProperty("Content-disposition", "attachment; filename=\"" +"example.pdf" +"\"");
InputStream fontInputStream = request.getPortletSession()
.getPortletContext()
.getResourceAsStream("/WEB-INF/classes/arial.ttf");
Document document = new Document(PageSize.A4, 40, 40, 40, 50);
PdfWriter writer = PdfWriter.getInstance(document,
response.getPortletOutputStream());
document.addAuthor("XYZ");
document.addTitle("ASDF");
document.open();
回答by Ashok Goli
Here's the answer after I worked it out for a little while: Serve PDF in Spring Portlet MVC Architecture - Liferay 6.0.6
这是我解决了一段时间后的答案: 在 Spring Portlet MVC 架构中提供 PDF - Liferay 6.0.6
The solution is to use Resource Serving Mechanism from JSR 286. The ResourceResponse res
has a res.setContentType("application/pdf");
method so you can use it to serve any type of resources. If you need it to be downloaded as an attachment, then use this:
解决方案是使用 JSR 286 中的资源服务机制。它ResourceResponse res
有一个res.setContentType("application/pdf");
方法,因此您可以使用它来服务任何类型的资源。如果您需要将其作为附件下载,请使用以下命令:
res.addProperty(HttpHeaders.CONTENT_DISPOSITION,"attachment");
res.addProperty(HttpHeaders.CONTENT_DISPOSITION,"attachment");
回答by user2009177
My code:
我的代码:
ResourceMapping("getPDF")
资源映射(“getPDF”)
public void descargarRecibo(ResourceRequest request,
ResourceResponse response, PortletSession session,
ModelMap modelMap) {
FileInputStream fileInputStream = null;
BufferedInputStream bufferedInputStream = null;
String fileURL = "c:/intranetdoc/PDetalleLlamadas/file.pdf";
try {
fileInputStream = new java.io.FileInputStream(fileURL);
OutputStream outputStream = response.getPortletOutputStream();
response.setContentType("application/pdf");
response.addProperty("Content-Disposition", "attachment; filename="
+ fileName);
bufferedInputStream = new java.io.BufferedInputStream(
fileInputStream);
byte[] bytes = new byte[bufferedInputStream.available()];
response.setContentLength(bytes.length);
int aByte = 0;
while ((aByte = bufferedInputStream.read()) != -1) {
outputStream.write(aByte);
}
outputStream.flush();
bufferedInputStream.close();
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
}
}