Java SpringMVC(portlet):如何在“打开文件”对话框中返回pdf文件?
在我的Java SpringMVC(portlet):如何在“打开文件”对话框中返回pdf文件?,java,spring,spring-mvc,portlet,spring-portlet-mvc,Java,Spring,Spring Mvc,Portlet,Spring Portlet Mvc,在我的@ActionMapping中,我为用户创建了一个PDF文件。 现在我想知道如何以保存/打开文件对话框的形式将此pdf返回给用户? 如果生成成功的话,我更喜欢这个而不是显示下载链接 我将SpringMVC3.0.5与Portlet结合使用。但是,如果有人对一个普通的应用程序有一些指针,那么我可能可以从那里找到它。 对于2.0,我读了一些关于扩展pdfgenerator类和在web.xml中玩弄的内容,但是现在我们只需要POJO的 编辑: 阿德尔建议后的代码: File file = ne
@ActionMapping
中,我为用户创建了一个PDF文件。
现在我想知道如何以保存/打开文件对话框的形式将此pdf返回给用户?
如果生成成功的话,我更喜欢这个而不是显示下载链接
我将SpringMVC3.0.5与Portlet结合使用。但是,如果有人对一个普通的应用程序有一些指针,那么我可能可以从那里找到它。
对于2.0,我读了一些关于扩展pdfgenerator类和在web.xml中玩弄的内容,但是现在我们只需要POJO的
编辑: 阿德尔建议后的代码:
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";
您可以将该文件直接写入
响应编写器
,并且不要忘记更改内容类型
。比如说,
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment;filename=something.pdf");
OutputStream out = response.getOutputStream();
out.write(pdfFileContentInBytes);
out.flush();
嗯,我认为这是一个HttpServletResponse
你所拥有的,但事实并非如此。当您使用Portlet时,它是一个RenderResponse
对象。在网上搜索之后,我发现了一些可能对你有帮助的链接
- 首先举一个例子,它展示了如何在使用
配置portlet时允许多个mime类型portlet.xml
- 下面,它展示了我们如何使用portlet.xml配置portlet。它有一个关于mime类型的XML元素,请查看是否可以在那里给出值,
application/pdf
ActionResponse-response
,而不是RenderResponse-response
。我这里有点模糊,不知道你们的超级班是什么?这是什么方法?等等
对我来说,问题似乎是允许/不允许portlet响应的mime类型。在SpringMVC中,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();
经过一段时间的研究,我得出了以下答案: 解决方案是使用JSR286中的资源服务机制。
resourcesponse res
有一个res.setContentType(“application/pdf”)方法,以便您可以使用它来服务任何类型的资源。如果需要将其作为附件下载,请使用以下方法:
res.addProperty(HttpHeaders.CONTENT_处置,“附件”)代码>我的代码:
资源映射(“getPDF”)
添加到Adeel的答案中..内容类型应为应用程序/pdf@Mahesh:我刚才正在做:)。不管怎样,谢谢。我刚试过,我得到:application/pdf不是受支持的mime/type。但这似乎是正确的方向。@Jack:令人惊讶!它是一个有效的mime类型。看看这个。但是,尝试使用应用程序/octet流
应该可以。但我仍然建议您解决为什么前者会给您带来错误。如果您共享实际代码和错误消息,我们可能会说些什么。添加了一些代码。我使用的是SpringPortletMVC,因此我无法直接访问HttpServletResponse(setHeader需要它,因为RenderResponse缺少该功能(可能是出于某种原因)。这就是实际的错误。application/pdf不是受支持的mime类型。lol-抱歉-认为这是一个问题-但是很少有介绍会是nicesetProperty()作为Portlet中setHeader()的替代品-很高兴知道这会起作用
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();
}
}