2017-12-28 35 views
1

PDF를 생성하여 보내려고합니다. 나는 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"; 
} 

, 나는이 예외 얻을 : 이것은 내가 만든 방법이다

java.io.IOException: Stream closed 

내가 서블릿과이 iText에 새로 온 사람을, 그래서 아마도이 코드는 정말 엉망이되다. 스트림을 언제 닫을까요? 문제를 해결하려면 어떻게해야합니까? 어떤 아이디어라도 대단히 환영합니다.

UPDATE : 여기

요청 스택 추적 경우 : XHTML이 렌더러가 찾을 수없는 파일을 참조하는 경우

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)
+1

스택 트레이스는 예외에 대한 경로를 이해하기 위해 매우 중요하다. 당신이 평신도의 용어로 그것을 읽고 번역 할 수 없다면, 본질적으로 대답을 포함하고 있기 때문에 분명히 우리에게서 그것을 숨기지 않아야합니다. 지금까지 제공된 정보를 사용하여 스택 추적 없이도이 예외가 왜 발생하는지 정확히 알 수 있습니다. 이것은 우리 (또는 우리)가 원하는 것이 아닙니다. – BalusC

+0

의견을 주셔서 감사합니다. 스택 추적을 포함하도록 편집했습니다. –

+0

JSoup의 버그 인 것 같습니다. 해결 방법은 https://stackoverflow.com/questions/16550571/jsoup-randomly-throws-java-io-ioexception-stream-is-closed-when-running-from-br을 확인하십시오. – user158037

답변

0

는 "renderer.setDocumentFromString을 (..)"는을 던질 수 "java.io.IOException : 스트림 닫음"예외. XHTML의 모든 참조를 확인할 수 있는지 확인하십시오. XHTML 참조 파일에 구성표를 추가 할 수 있습니다. 이런 식으로 뭔가 : =

href가 "파일 : // {path_to_the_file}/{FILE_NAME}는"