spring-mvc(portlet):如何在打开文件对话框中返回pdf文件?
在我的 @ActionMapping
中,我为用户创建了一个 PDF 文件。 现在我想知道如何以保存/打开文件对话框的形式将此pdf返回给用户? 如果生成成功,我更喜欢显示下载链接。
我将 spring-mvc 3.0.5 与 portlet 结合使用。但如果有人对正常应用程序有一些指导,那么我可能可以从那里弄清楚。 对于 2.0,我读到了一些关于扩展 pdfgenerator 类并在 web.xml 中进行操作的内容,但现在我们只需要 POJO 的......
编辑: 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";
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.
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....
Edit:
Code after Adeel's suggestion:
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";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将该文件直接写入您的
响应编写器
,并且不要忘记更改contentType
。例如,嗯,我认为它是您所拥有的
HttpServletResponse
,但事实并非如此。当您使用 Portlet 时,它是一个 RenderResponse 对象。在互联网上搜索后,我发现了一些在这方面可能对您有帮助的链接。首先举一个例子 Lotus Form Server Portlet,它展示了如何在使用
portlet.xml
配置 portlet 时允许多个 mime 类型。这里是 Spring Portlet 文档,它显示我们如何使用 portlet.xml 配置 portlet。它有一个关于 mime-type 的 XML 元素,看看你是否可以给出值,
application/pdf
,在那里。另一个想法是将参数更改为
ActionResponse 响应
,而不是RenderResponse 响应
。我在这里有点模糊,不确定你的超级类是什么?它是什么方法?等等......对我来说,问题似乎是 portlet 响应允许/不允许的 mime 类型。
You can write that file directly to your
response writer
, and don't forget to change thecontentType
. For example,Well, I thought its a
HttpServletResponse
what you have, but its not the case. As you are working with Portlet, its aRenderResponse
object. After searching over the Internet, I found few links which might be helpful to you in this regard.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.Another idea is to change your parameter to
ActionResponse response
, instead ofRenderResponse response
. I am a bit blur here, not sure what is your super class? what method is it? etc....To me, it seems that the problem is allowed/not-allowed mime-types for the portlet response.
在spring mvc中,ResourceResponse响应
in spring mvc, ResourceResponse response
这是我研究了一段时间后的答案:
在 Spring Portlet MVC 架构中提供 PDF - Liferay 6.0 .6
解决方案是使用 JSR 286 中的资源服务机制。
ResourceResponse res
有一个res.setContentType("application/pdf");
方法,因此您可以使用它来提供任何类型的资源。如果您需要将其作为附件下载,请使用:res.addProperty(HttpHeaders.CONTENT_DISPOSITION,"attachment");
Here's the answer after I worked it out for a little while:
Serve PDF in Spring Portlet MVC Architecture - Liferay 6.0.6
The solution is to use Resource Serving Mechanism from JSR 286. The
ResourceResponse res
has ares.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:res.addProperty(HttpHeaders.CONTENT_DISPOSITION,"attachment");
我的代码:
My code: