1
私はPDFを生成して送信しようとしています。これは私が作成したメソッドです。IO例外ストリームがスローされたのはなぜですか?
public String exportReportToPDF() throws IOException {
FacesContext faces = FacesContext.getCurrentInstance();
ExternalContext externalContext = faces.getExternalContext();
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
HttpSession session = (HttpSession) externalContext.getSession(true);
String url = request.getRequestURL().append(";jsessionid=").append(session.getId()).toString();
Document doc = Jsoup.connect(url).get();
String html = doc.html();
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
renderer.layout();
HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
response.setContentType("application/pdf");
SimpleDateFormat dateFormater = new SimpleDateFormat("ddMMyyyyHHmm");
String fileName = "Something" + dateFormater.format(new Date())+".pdf";
response.setHeader("Content-disposition", "inline; filename=" + fileName);
try {
response.getOutputStream().write(html.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
faces.responseComplete();
faces.renderResponse();
return "success";
}
私はPDFを生成しようとすると、私はこの例外を取得:
java.io.IOException: Stream closed
私はサーブレットとiTextのに新たなんだので、おそらくこのコードは実際にありますうんざりするいつ私はストリームを閉じますか?それを修正するために私は何をすべきですか?どんなアイデアも大歓迎です。
UPDATE:ここ
が要求されたスタックトレースです:
10:33:20,586 ERROR [stderr] (http-/127.0.0.1:8080-2) java.io.IOException: Stream closed 10:33:20,588 ERROR [stderr] (http-/127.0.0.1:8080-2) at java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:151) 10:33:20,588 ERROR [stderr] (http-/127.0.0.1:8080-2) at java.io.BufferedInputStream.read1(BufferedInputStream.java:273) 10:33:20,589 ERROR [stderr] (http-/127.0.0.1:8080-2) at java.io.BufferedInputStream.read(BufferedInputStream.java:334) 10:33:20,589 ERROR [stderr] (http-/127.0.0.1:8080-2) at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283) 10:33:20,589 ERROR [stderr] (http-/127.0.0.1:8080-2) at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325) 10:33:20,590 ERROR [stderr] (http-/127.0.0.1:8080-2) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177) 10:33:20,590 ERROR [stderr] (http-/127.0.0.1:8080-2) at java.io.InputStreamReader.read(InputStreamReader.java:184) 10:33:20,591 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.xhtmlrenderer.css.parser.Lexer.zzRefill(Lexer.java:1634) 10:33:20,591 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.xhtmlrenderer.css.parser.Lexer.yylex(Lexer.java:1865) 10:33:20,592 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.xhtmlrenderer.css.parser.CSSParser.next(CSSParser.java:1798) 10:33:20,592 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.xhtmlrenderer.css.parser.CSSParser.la(CSSParser.java:1810) 10:33:20,593 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.xhtmlrenderer.css.parser.CSSParser.stylesheet(CSSParser.java:159) 10:33:20,593 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.xhtmlrenderer.css.parser.CSSParser.parseStylesheet(CSSParser.java:89) 10:33:20,594 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.xhtmlrenderer.context.StylesheetFactoryImpl.parse(StylesheetFactoryImpl.java:78) 10:33:20,595 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.xhtmlrenderer.context.StylesheetFactoryImpl.parse(StylesheetFactoryImpl.java:95) 10:33:20,596 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.xhtmlrenderer.context.StylesheetFactoryImpl.getStylesheet(StylesheetFactoryImpl.java:174) 10:33:20,597 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.xhtmlrenderer.context.StyleReference.readAndParseAll(StyleReference.java:123) 10:33:20,598 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.xhtmlrenderer.context.StyleReference.setDocumentContext(StyleReference.java:107) 10:33:20,598 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.xhtmlrenderer.pdf.ITextRenderer.setDocument(ITextRenderer.java:175) 10:33:20,599 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.xhtmlrenderer.pdf.ITextRenderer.setDocument(ITextRenderer.java:142) 10:33:20,599 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.xhtmlrenderer.pdf.ITextRenderer.setDocumentFromString(ITextRenderer.java:159) 10:33:20,600 ERROR [stderr] (http-/127.0.0.1:8080-2) at es.xpt.htravel.reports.view.ReportGeneratorController.exportReportToPDF(ReportGeneratorController.java:394) 10:33:20,601 ERROR [stderr] (http-/127.0.0.1:8080-2) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 10:33:20,601 ERROR [stderr] (http-/127.0.0.1:8080-2) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 10:33:20,601 ERROR [stderr] (http-/127.0.0.1:8080-2) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 10:33:20,602 ERROR [stderr] (http-/127.0.0.1:8080-2) at java.lang.reflect.Method.invoke(Method.java:606) 10:33:20,602 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.apache.el.parser.AstValue.invoke(AstValue.java:258) 10:33:20,602 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) 10:33:20,602 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40) 10:33:20,603 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50) 10:33:20,604 ERROR [stderr] (http-/127.0.0.1:8080-2) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) 10:33:20,604 ERROR [stderr] (http-/127.0.0.1:8080-2) at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87) 10:33:20,605 ERROR [stderr] (http-/127.0.0.1:8080-2) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101) 10:33:20,612 ERROR [stderr] (http-/127.0.0.1:8080-2) at javax.faces.component.UICommand.broadcast(UICommand.java:315) 10:33:20,617 ERROR [stderr] (http-/127.0.0.1:8080-2) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:786) 10:33:20,618 ERROR [stderr] (http-/127.0.0.1:8080-2) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1251) 10:33:20,618 ERROR [stderr] (http-/127.0.0.1:8080-2) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 10:33:20,619 ERROR [stderr] (http-/127.0.0.1:8080-2) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 10:33:20,619 ERROR [stderr] (http-/127.0.0.1:8080-2) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 10:33:20,619 ERROR [stderr] (http-/127.0.0.1:8080-2) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) 10:33:20,619 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295) 10:33:20,620 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) 10:33:20,620 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79) 10:33:20,637 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:246) 10:33:20,639 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) 10:33:20,652 ERROR [stderr] (http-/127.0.0.1:8080-2) at es.xpt.htravel.booking.view.PdfFilter.doFilter(PdfFilter.java:220) 10:33:20,652 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:246) 10:33:20,655 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) 10:33:20,698 ERROR [stderr] (http-/127.0.0.1:8080-2) at es.xpt.htravel.booking.view.LoginFilter.doFilter(LoginFilter.java:180) 10:33:20,754 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:246) 10:33:20,755 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) 10:33:20,765 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) 10:33:20,771 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) 10:33:20,772 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) 10:33:20,776 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145) 10:33:20,779 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) 10:33:20,783 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) 10:33:20,785 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:336) 10:33:20,786 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) 10:33:20,786 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653) 10:33:20,787 ERROR [stderr] (http-/127.0.0.1:8080-2) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:920) 10:33:20,787 ERROR [stderr] (http-/127.0.0.1:8080-2) at java.lang.Thread.run(Thread.java:745)
スタックトレースは、例外へのパスを理解するために重要である。このような何か素人の言葉に読んで翻訳することができない限り、答えを本質的に含んでいるので、確かに私たちから隠すべきではありません。これまでに提供されている情報でスタックトレースがなくても、なぜこの例外がスローされるのかという野生の推測しかできません。これはあなた(または私たち)が望むものではありません。 – BalusC
コメントありがとうございます。スタックトレースを含めるように編集しました。 –
JSoupのバグのようです。回避策についてはhttps://stackoverflow.com/questions/16550571/jsoup-randomly-throws-java-io-ioexception-stream-is-closed-when-running-from-brを確認してください – user158037