使用 PrintWriter 和 OutputStream
我正在使用 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 不是 String 是 CharSequence),但它不会将数据提取到 PDF 中。
我还尝试获取 PrintWriter,关闭它以打开 OutputStream (不起作用)或刷新它(两者都不是)。
任何有关使用任何可以在 pdf 中显示数据的输出的解决方案的帮助都会很棒。 多谢!
I am creating a project with struts and I have a problem using Jasper IReports. I want to export some info into a pdf file and I keep getting the java.lang.IllegalStateException: getOutputStream() has already been call... Exception due to openning a ServletOutputStream in my code when the page already opens a PrintWriter.
The code is in the model (so it is not in the jsp, it's in a java file), as it follows:
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();
}
I have checked the connection, the path and the HttpServletResponse and are all working fine.
I'm very newbie with Jasper Reports as well as with coding stuff into PDF so you can -correctly- suposse that I have a minimal idea of what I am doing here and that, obviously my code is copy/pasted from somewhere through the net.
I have tried to use PrintWriter instead of OutputStream, transforming bytes into a String and using the PrintWriter.append(String) method (allthought is not String is CharSequence), but it doesn't extract the data into the PDF.
I have also tried to get the PrintWriter, close it to open the OutputStream (didn't work) or flush it (neither).
Any help with a solution to use any out that could show the data in a pdf would be great.
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看堆栈跟踪很有用。
不过,您可以先尝试运行完整性检查:修改该代码以简单地将静态字符串 (hello world) 写入 ServletOutputStream 并将内容类型设置为 text/html。因为这应该可以正常工作:
HTH
Would be useful to see the stack trace.
You might try running a sanity check first though: Modify that code to simply write a static string (hello world) to the ServletOutputStream and set content type to text/html. As that should work fine:
HTH
一些想法:
Some ideas:
代码具体是如何调用的呢?从堆栈跟踪来看,您似乎正在使用 JSP 文件(
inicio2.jspscriptlet 来通过
handle
方法运行 Java 类> 准确地说)。在 Java 类将报告写入OutputStream
后,JSP 文件将继续输出文件本身的剩余部分(包括空格!),这将隐式调用getWriter()
将其写入响应。确切地说,当之前在 Java 类中调用了 getOutputStream() 时,这会导致您现在所面临的 IllegalStateException 异常。将 Java 代码放在 Java 类中固然很好,但这并不意味着您仍然可以使用 JSP 来调用它。 JSP 不应包含任何一行 Java 代码。 JSP 本身就是作为视图技术输出的一部分。要解决这一切,只需拥有一个 Struts 操作类(或
HttpServlet
),您可以通过 HTMLHow exactly is the code invoked? Judging from the stacktrace it look like that you're running the Java class with the
handle
method using scriptlets inside a JSP file (theinicio2.jsp
to be precise). After that the Java class has written the report to theOutputStream
, the JSP file would continue with outputting the remnant of the file itself (including whitespace!), which would implicitly invoke thegetWriter()
to write it to response. Exactly that would cause anIllegalStateException
as you're facing now when thegetOutputStream()
is already been called before in the Java class.It's good that Java code is been placed in a Java class, but that doesn't mean that you may still use JSP to invoke it. The JSP should not contain any single line of Java code. JSP itself is as being a view technology part of the output. To fix this all, just have a Struts action class (or a
HttpServlet
) which you can invoke by a HTML<form>
or<a>
.自己的答案:
我在路径中放入了一个jrxml,它实际上连接到数据库并获取一些真实数据,突然它工作了,PDF在mozilla中打开,但异常不断发生。我不知道如何抛出异常(我已经调试并且异常发生在同一个地方)并能够看到 PDF。
所以我只能继续寻找这里究竟发生了什么。我会回答我发现的任何问题(可能在周一)。
感谢 simonlord 和 Jim Rush 的帮助! :)
编辑:顺便说一句,这是堆栈跟踪(它是西班牙语,如果需要,我会翻译您不明白的任何内容):
再次编辑:似乎我没有复制 2010 年 1 月 15 日的“1”...可能会对 1 月 5 日 insetad 的跟踪造成混淆。已更正。
Own answer:
I have put in the path a jrxml that actually connects to the database and gets some real data out and suddendly it worked, the PDF opened in mozilla, but the Exception keeps happening. I do not know how I can throw and exception (I have debugged and the Exception happens in the same place) and be able to see the PDF.
So I can only keep searching for what actually happens here. I will answer with anything I find (probably on monday).
Thanks simonlord and Jim Rush for your help! :)
Edit: By the way, here is the stacktrace (it's in Spanish, if needed I will translate anything you don't understand):
Edit again: Seems I didn't copy the '1' in the 15th-Jan-2010... could create confusion about a trace from the 5th-Jan insetad. Corrected.