2016-08-01 119 views
0

如何Resove以下錯誤:產生java.io.IOException:UT010029:流關閉

ERROR [com.test.filter.AuthenticationFilter] (default task-6) displayUpdateUser:Error occurred while displaying the Update User page.: java.io.IOException: UT010029: Stream is closed 
    at io.undertow.servlet.spec.ServletOutputStreamImpl.write(ServletOutputStreamImpl.java:136) 
    at java.io.ByteArrayOutputStream.writeTo(ByteArrayOutputStream.java:167) 
    at org.springframework.web.servlet.view.AbstractView.writeToResponse(AbstractView.java:468) 
    at org.springframework.web.servlet.view.document.AbstractPdfView.renderMergedOutputModel(AbstractPdfView.java:82) 
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303) 
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1243) 
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:968) 
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:870) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) 
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) 
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86) 
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:130) 
    at com.test.filter.AuthenticationFilter.doFilter(AuthenticationFilter.java:66) 
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60) 
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132) 
    at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:85) 
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62) 
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) 
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78) 
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) 
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131) 
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57) 
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) 
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46) 
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64) 
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58) 
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:72) 
    at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50) 
    at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) 
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) 
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61) 
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) 
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) 
    at io.undertow.servlet.handlers.ServletInitialHandler.jrHandle(ServletInitialHandler.java) 
    at org.zeroturnaround.javarebel.integration.servlet.undertow.cbp.ServletInitialHandlerCBP.handleRequest(ServletInitialHandlerCBP.java:101) 
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:282) 
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:261) 
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:80) 
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:172) 
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:199) 
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:774) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at java.lang.Thread.run(Thread.java:745) 

代碼:

document.open(); 
    PDFGenerator.createSummaryTable(document, table, dataList, relPath); 
    document.close(); 
    pdfWriter.close(); 
    os.flush(); 
    os.close(); 

    byte b[] = os.toByteArray(); 
    ByteArrayOutputStream os1 = new ByteArrayOutputStream(); 
    PdfReader pdfReader = new PdfReader(b); 
    PdfStamper pdfStamper = new PdfStamper(pdfReader, os1); 
    pdfStamper.setViewerPreferences(PdfWriter.PDFXNONE); 
    pdfStamper.setViewerPreferences(PdfWriter.HideMenubar); 
    pdfStamper.setViewerPreferences(PdfWriter.HideToolbar); 
    pdfStamper.setViewerPreferences(PdfWriter.HideWindowUI); 
    PdfWriter pdfWriter1 = pdfStamper.getWriter(); 

    pdfWriter1.addJavaScript(getPrintJS()); 
    pdfReader.close(); 
    pdfStamper.close(); 
    os1.flush(); 
    os1.close(); 

    OutputStream os2 = httpServletResponse.getOutputStream(); 
    os2.write(os1.toByteArray()); 
    os2.flush(); 
    os2.close(); 
+0

沒有人在'createSummaryTable()'方法中看到你在做什麼。很奇怪你需要兩次創建這個PDF。設置查看器首選項並在'createSummaryTable()'方法中添加JavaScript會很容易。無論如何,你正試圖寫一些已經關閉的'ServletOutputStream'。這不是iText問題。這是一個基本的servlet問題。 –

+0

即使你沒有明確關閉ServletOutputStream,它也可能被服務器關閉。由於超時。此外,它看起來像直接寫入'HttpServletResponse.getOutputStream()'是在你的框架中的一個錯誤的模式:框架似乎最終嘗試和寫一個響應。因此,請驗證您對您的框架的使用,特別是Spring Web框架。 – mkl

+0

堆棧跟蹤中的「由...引起」行在哪裏? – kolossus

回答

1

您關閉O1之後,你再次讀取使用它:

os1.close(); //<--- close 

OutputStream os2 = httpServletResponse.getOutputStream(); 
os2.write(os1.toByteArray()); //<--- use os1 
+0

儘管這不是很好的風格,但它並不是引起問題的原因,因爲'os1'是一個'ByteArrayOutputStream','ByteArrayOutputStream.close()'是一個無操作方法。參看它的JavaDocs:*「關閉一個'ByteArrayOutputStream'沒有任何作用,這個類中的方法可以在流關閉後調用,而不會產生'IOException'。」* – mkl

+0

謝謝你的答覆..我也嘗試過這個解決方案,但是我得到相同的錯誤.. – Thamaraikkannan