在liferay portlet中我想使用servlet并生成一个pdf文件。这是我在下面复制它们的代码。
view.jsp的:
<p style="color:black">
<a href="<%=request.getContextPath()%>/servlet/pdfServlet">Click here for DownLoad PDF Format!!!</a></p>
我的portlet正文
public class pdfFormat extends GenericPortlet {
/**
* Helper method to serve up the mandatory view mode.
*/
protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
response.setContentType("text/html");
PortletRequestDispatcher dispatcher = getPortletContext()
.getRequestDispatcher("/WEB-INF/jsp/view.jsp");
dispatcher.include(request, response);
}
}
和我的 servlet 代码:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Document document = new Document();
try{
response.setContentType("application/pdf");
response.setHeader ("Content-Disposition", "attachment; filename=Report.pdf");
// FontFactory.register("c:\\WINDOWS\\fonts\\DejaVuSans-Bold.ttf", "MY_FONT");
BaseFont baseFont = BaseFont.createFont("c:\\WINDOWS\\fonts\\DejaVuSans-Bold.ttf", BaseFont.IDENTITY_H,true);
Font font = new Font(baseFont);
PdfWriter.getInstance(document, response.getOutputStream());
ArrayList<reportModel> ShowResult = new ArrayList<reportModel>();
reportDAO dao = new reportDAO();
ShowResult = dao.ShowAllinfo();
document.open();
document.add(new Paragraph(new Date().toString()));
for(int i=0;i<10;i++)
{
String strTitle = ShowResult.get(i).getTitle();
String strSite = ShowResult.get(i).getSite();
String strNote = ShowResult.get(i).getNote();
document.add(new Paragraph(strTitle,font));
document.add(new Paragraph(strSite,font));
document.add(new Paragraph(strNote,font));
document.add(new Paragraph("****************************************"));
}
}catch(Exception e){
e.printStackTrace();
}
document.close();
}
之后我部署了我的portlet并获得了一个pdf文件,我在tomcat日志文件中看到了这个错误:
SEVERE: Servlet.service() for servlet [pdfServlet] in context with path [/pdfReport] threw exception [ExceptionConverter: ClientAbortException: java.net.So
cketException: Software caused connection abort: socket write error] with root cause
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at org.apache.coyote.http11.InternalOutputBuffer.realWriteBytes(InternalOutputBuffer.java:215)
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:462)
at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:366)
at org.apache.coyote.http11.InternalOutputBuffer$OutputStreamOutputBuffer.doWrite(InternalOutputBuffer.java:240)
at org.apache.coyote.http11.filters.ChunkedOutputFilter.doWrite(ChunkedOutputFilter.java:119)
at org.apache.coyote.http11.AbstractOutputBuffer.doWrite(AbstractOutputBuffer.java:192)
at org.apache.coyote.Response.doWrite(Response.java:504)
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:383)
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:462)
at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:366)
at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:413)
at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:401)
at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:91)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
at com.itextpdf.text.pdf.OutputStreamCounter.flush(OutputStreamCounter.java:89)
at com.itextpdf.text.DocWriter.close(DocWriter.java:233)
at com.itextpdf.text.pdf.PdfWriter.close(PdfWriter.java:1286)
at com.itextpdf.text.pdf.PdfDocument.close(PdfDocument.java:810)
at com.itextpdf.text.Document.close(Document.java:416)
at com.iran.servlet.pdfServlet.doGet(pdfServlet.java:86)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:72)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.liferay.portal.kernel.bean.ClassLoaderBeanHandler.invoke(ClassLoaderBeanHandler.java:67)
at $Proxy402.doFilter(Unknown Source)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:72)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDirectCallFilter(InvokerFilterChain.java:167)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:95)
at com.liferay.portal.kernel.servlet.PortalClassLoaderFilter.doFilter(PortalClassLoaderFilter.java:70)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:206)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:108)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter.doFilter(InvokerFilter.java:73)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
我该如何解决这个问题?
答案 0 :(得分:0)
我不熟悉那个pdf编写器,但您可能想尝试使用已创建的文档来提供它?例如。将PdfWriter.getInstance(document, response.getOutputStream());
移到方法的末尾。
背景:pdfwriter可能已经编写了您传入该调用的(空)文档。
但这只是猜测 - 正如我所说,我不熟悉那个组件