5

我正在使用 struts 创建一个项目,但在使用 Jasper IReports 时遇到问题。我想将一些信息导出到 pdf 文件中,并且不断收到 java.lang.IllegalStateException:getOutputStream() 已被调用...当页面已经打开 PrintWriter 时在我的代码中打开 ServletOutputStream 导致的异常。

代码在模型中(所以它不在jsp中,它在java文件中),如下所示:

    public void handle(HttpServletResponse res, Connection connection, String path)throws Exception{
    ServletOutputStream out = null;
    try {

        JasperDesign jasperDesign = JRXmlLoader.load(path);
        JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
        byte[] bytes = JasperRunManager.runReportToPdf(jasperReport, null, connection);
        res.setContentType("application/pdf");
        res.setContentLength(bytes.length);
        out = res.getOutputStream();
        out.write(bytes, 0, bytes.length);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        out.flush();
        out.close();
    }

我检查了连接、路径和 HttpServletResponse 并且一切正常。

我是 Jasper Reports 以及将内容编码为 PDF 的新手,因此您可以正确地假设我对我在这里所做的事情有一个最小的了解,显然我的代码是通过网络从某个地方复制/粘贴的.

我尝试使用 PrintWriter 而不是 OutputStream,将字节转换为字符串并使用 PrintWriter.append(String) 方法(allthought is not String is CharSequence),但它不会将数据提取到 PDF 中。

我还尝试获取 PrintWriter,关闭它以打开 OutputStream(不起作用)或刷新它(两者都没有)。

任何关于使用任何可以在 pdf 中显示数据的解决方案的帮助都会很棒。非常感谢!

4

4 回答 4

5

查看堆栈跟踪会很有用。

不过,您可以先尝试运行健全性检查:修改该代码以简单地将静态字符串(hello world)写入 ServletOutputStream 并将内容类型设置为 text/html。因为那应该可以正常工作:

public void handle(HttpServletResponse res, Connection connection, String path)throws Exception{
ServletOutputStream out = null;
try {
    byte[] bytes = "hello world".getBytes();
    res.setContentType("text/html");
    res.setContentLength(bytes.length);
    out = res.getOutputStream();
    out.write(bytes, 0, bytes.length);
} catch (Exception e) {
    e.printStackTrace();
} finally {
    out.flush();
    out.close();
}

高温高压

于 2010-01-15T11:49:15.800 回答
3

一些想法:

  • 如果您使用的是二进制流(PDF 是二进制),PrintWriter 将无法工作
  • 如果客户端超时或断开连接,可能会出现非法状态。该过程的数据包跟踪会告诉您很多信息(即使您无法很好地阅读它们)。查看 WireShark 或可用于您的平台的内容。值得您花时间至少了解一下线路级别发生的事情。
  • 确保您从生成的报告中获取的数据实际上是 pdf。将其写入文件并尝试打开。
  • 某些情况需要您在写入流之前设置 http 长度标头,否则当数据开始显示时它们会放弃。这里可能需要。
于 2010-01-15T11:53:24.417 回答
1

代码究竟是如何调用的?从堆栈跟踪来看,您似乎正在使用JSP 文件中使用scriptlethandle的方法运行 Java 类(准确地说)。在 Java 类将报告写入 之后,JSP 文件将继续输出文件本身的剩余部分(包括空格!),这将隐式调用 将其写入响应。当之前已经在 J​​ava 类中调用过时,这将导致您现在面临的问题。inicio2.jspOutputStreamgetWriter()IllegalStateExceptiongetOutputStream()

将 Java 代码放在 Java 类中很好,但这并不意味着您仍然可以使用 JSP 来调用它。JSP 不应包含任何单行 Java 代码。JSP 本身就是作为视图技术输出的一部分。要解决这一切,只需要有一个HttpServlet可以通过 HTML<form><a>.

于 2010-01-15T13:36:52.493 回答
0

自己的答案:

我已经在路径中放入了一个 jrxml,它实际上连接到数据库并获取了一些真实数据,突然它起作用了,PDF 在 mozilla 中打开,但异常不断发生。我不知道如何抛出异常(我已经调试并且异常发生在同一个地方)并且能够看到 PDF。

所以我只能继续寻找这里实际发生的事情。我会回答我找到的任何东西(可能在星期一)。

感谢 simonlord 和 Jim Rush 的帮助!:)

编辑:顺便说一句,这里是堆栈跟踪(它是西班牙语,如果需要,我会翻译任何你不明白的东西):再次编辑:似乎我没有在 2010 年 1 月 15 日复制“1”。 . 可能会对 1 月 5 日 insetad 的踪迹造成混淆。已更正。

15-ene-2010 13:47:03 org.apache.catalina.core.ApplicationDispatcher invoke
GRAVE: El Servlet.service() para servlet jsp lanzó una excepción
java.lang.IllegalStateException: getOutputStream() ya ha sido llamado para esta respuesta
    at org.apache.catalina.connector.Response.getWriter(Response.java:607)
    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:196)
    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
    at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
    at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
    at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:171)
    at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:116)
    at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:76)
    at org.apache.jsp.pages.inicio2_jsp._jspService(inicio2_jsp.java:300)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:679)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:584)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:497)
    at org.apache.struts.chain.commands.servlet.PerformForward.handleAsInclude(PerformForward.java:141)
    at org.apache.struts.chain.commands.servlet.PerformForward.perform(PerformForward.java:92)
    at org.apache.struts.chain.commands.AbstractPerformForward.execute(AbstractPerformForward.java:54)
    at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
    at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
    at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
    at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
    at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:873)
    at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
    at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
    at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
    at java.lang.Thread.run(Unknown Source)
15-ene-2010 13:47:03 org.apache.struts.chain.commands.AbstractExceptionHandler execute
ADVERTENCIA: Unhandled exception
org.apache.jasper.JasperException: getOutputStream() ya ha sido llamado para esta respuesta
    at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:460)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:367)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:679)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:584)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:497)
    at org.apache.struts.chain.commands.servlet.PerformForward.handleAsInclude(PerformForward.java:141)
    at org.apache.struts.chain.commands.servlet.PerformForward.perform(PerformForward.java:92)
    at org.apache.struts.chain.commands.AbstractPerformForward.execute(AbstractPerformForward.java:54)
    at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
    at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
    at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
    at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
    at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:873)
    at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
    at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
    at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
    at java.lang.Thread.run(Unknown Source)
15-ene-2010 13:47:03 org.apache.struts.chain.commands.ExceptionCatcher postprocess
ADVERTENCIA: Exception from exceptionCommand 'servlet-exception'
org.apache.jasper.JasperException: getOutputStream() ya ha sido llamado para esta respuesta
    at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:460)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:367)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:679)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:584)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:497)
    at org.apache.struts.chain.commands.servlet.PerformForward.handleAsInclude(PerformForward.java:141)
    at org.apache.struts.chain.commands.servlet.PerformForward.perform(PerformForward.java:92)
    at org.apache.struts.chain.commands.AbstractPerformForward.execute(AbstractPerformForward.java:54)
    at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
    at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
    at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
    at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
    at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:873)
    at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
    at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
    at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
    at java.lang.Thread.run(Unknown Source)
15-ene-2010 13:47:03 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() para servlet action lanzó excepción
java.lang.IllegalStateException: getOutputStream() ya ha sido llamado para esta respuesta
    at org.apache.catalina.connector.Response.getWriter(Response.java:607)
    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:196)
    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
    at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
    at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
    at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:171)
    at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:116)
    at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:76)
    at org.apache.jsp.pages.inicio2_jsp._jspService(inicio2_jsp.java:300)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:679)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:584)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:497)
    at org.apache.struts.chain.commands.servlet.PerformForward.handleAsInclude(PerformForward.java:141)
    at org.apache.struts.chain.commands.servlet.PerformForward.perform(PerformForward.java:92)
    at org.apache.struts.chain.commands.AbstractPerformForward.execute(AbstractPerformForward.java:54)
    at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
    at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
    at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
    at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
    at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:873)
    at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
    at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
    at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
    at java.lang.Thread.run(Unknown Source)
于 2010-01-15T12:38:00.427 回答