我正在寻找一种通过Liferay Portal向浏览器发送PDF(直接显示)文件的方式。找到了很多解决方案 - 最受欢迎的解决方案是编写可以完成这项工作的Servlet。我已经阅读了JSR 286规范中的Portlet资源服务,有人可以详细说明Spring 3.0 Portlet MVC吗?在Spring Portlet MVC架构服务PDF - Liferay 6.0.6
<servlet>
<display-name>DownloadServlet</display-name>
<servlet-name>DownloadServlet</servlet-name>
<servlet-class>com.liferay.portal.pdf.DownloadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DownloadServlet</servlet-name>
<url-pattern>/DownloadServlet/*</url-pattern>
</servlet-mapping>
和Servlet的组成:
private void downloadServlet(HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOException {
logger.debug(" downloadServlet :: ");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
ServletOutputStream op = null;
try {
//Something
pdfContentVO=//getpdf VO here
String filename = "PDFFILE_"+pdfNumber+".pdf";
op = resp.getOutputStream();
resp.setContentType("application/pdf");
resp.setHeader("Content-Disposition", "attachment; filename="
+ filename);
resp.setContentLength(pdfContentVO.getPdfData().length);
System.out.println("pdfcontent"+pdfContentVO.getPdfData());
op.write(pdfContentVO.getPdfData());
op.flush();
op.close();
} catch(final IOException e) {
System.out.println ("IOException.");
throw e;
} finally {
if (bis != null)
{
bis.close();
}
if (bos != null)
{
bos.flush();
bos.close();
}
}
}
在Spring Controller中映射请求映射URL的实际方法是使用portlet:resourceURL标记中的'id'属性,然后将其映射到控制器的@ResourceMapping属性中。这应该将URL映射到方法。但如果你不介意自己比较和映射,上面的方法工作正常。 – 2012-04-21 11:45:18