gpt4 book ai didi

spring-mvc - 在 Spring Portlet MVC 架构中提供 PDF - Liferay 6.0.6

转载 作者:行者123 更新时间:2023-12-01 09:35:35 26 4
gpt4 key购买 nike

我正在寻找一种通过 Liferay Portal 将 PDF(直接显示)文件发送到浏览器的方法。找到了许多解决方案——最流行的一个是编写一个可以完成这项工作的 Servlet。我已经阅读了 JSR 286 规范中的 Portlet Resource Serving,有人可以详细说明一下 Spring 3.0 Portlet MVC 的内容吗?

<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();
}
}

}

最佳答案

我在 Servlet 上找不到任何东西<->Portlet 映射的东西。因此,我使用资源映射使用注释发送带有 Spring Portlet MVC 的 pdf。引用:http://developers.sun.com/portalserver/reference/techart/jsr286/jsr286_2.html

在 JSP 中:

    <portlet:resourceURL var="PDFActionURL">
<portlet:param name="reportType" value="pdf" />
<portlet:param name="pdfNumber" value="${pdfNumber}" />
</portlet:resourceURL>
<input type="button" name="viewPDFButton" value="View PDF" onClick="self.location = '${PDFActionURL}';" />

在 portlet 的 Spring ApplicationContext.xml 中,包括这些:

<context:annotation-config />
<context:component-scan base-package="com.xxx" />

定义一个新的 Controller :

import java.io.IOException;
import java.io.OutputStream;

import javax.portlet.PortletException;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.ResourceMapping;

import com.xxx.pdf.PDFBO;
import com.xxx.PDFDTO;
import com.liferay.portal.kernel.servlet.HttpHeaders;
import com.liferay.portal.kernel.util.ParamUtil;

@Controller("pdfController")
@RequestMapping(value = "view")
public class PDFController {
private final static Logger LOG = LoggerFactory.getLogger(PDFController.class);

//This is using Spring 3.0 Annotation Mapping (Spring Portlet MVC Architecture)
@ResourceMapping
public void serveResource(ResourceRequest resourceRequest, ResourceResponse res) throws PortletException, IOException {
LOG.info("In serveResource: ResourceURL");
String returnType = ParamUtil.getString(resourceRequest, "reportType");
String pdfNumber = ParamUtil.getString(resourceRequest, "pdfNumber");
LOG.info("returnType:" + returnType + " pdfNumber:" + pdfNumber);
String filename = "FILENAME_"+pdfNumber+".pdf";
// HttpServletRequest request =
// PortalUtil.getHttpServletRequest(resourceRequest);

if (returnType != null && returnType.equals("pdf")) {
try {
//GET YOUR PDF HERE
//PDFBO pdfBO = new PDFBO();
//PDFDTO pdfContentVO = null;
//pdfContentVO = pdfBO.getPDF(pdfNumber);
res.setContentType("application/pdf");
res.addProperty(HttpHeaders.CACHE_CONTROL, "max-age=3600, must-revalidate");
res.addProperty(HttpHeaders.CONTENT_DISPOSITION,"filename="+ filename);
//Use this to directly download the file
//res.addProperty(HttpHeaders.CONTENT_DISPOSITION,"attachment");
OutputStream out = res.getPortletOutputStream();
//out.write(pdfContentVO.getPdfData());
out.write(/*get pdf byte[] Array Here */);
out.flush();
out.close();
} catch (Exception e) {
LOG.info("Error in " + getClass().getName() + "\n" + e);
}
}
}
}

编辑:如果您使用的是以前版本的 Liferay,这是一篇通过 Liferay 实现文件下载/服务的好文章 - https://www.liferay.com/web/raymond.auge/blog/-/blogs/801426

关于spring-mvc - 在 Spring Portlet MVC 架构中提供 PDF - Liferay 6.0.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8654568/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com