使用PrintWriter和OutputStream [英] Using PrintWriter and OutputStream
问题描述
我正在使用struts创建一个项目,我在使用Jasper IReports时遇到了问题。我想将一些信息导出到pdf文件中,并且我一直得到java.lang.IllegalStateException:getOutputStream()已被调用...由于在页面已打开PrintWriter时在我的代码中打开ServletOutputStream而导致异常。
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.
代码在模型中(所以它不在jsp中,它在java文件中),如下所示:
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();
}
我检查了连接,路径和HttpServletResponse并且都运行良好。
I have checked the connection, the path and the HttpServletResponse and are all working fine.
我是Jasper Reports的新手以及编写PDF格式的东西,所以你可以 - 正确地 - 我对我正在做的事情一无所知在这里和那个,显然我的代码是通过网络从某处复制/粘贴的。
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.
我试图使用PrintWriter而不是OutputStream,将字节转换为字符串并使用PrintWriter.append(String)方法(allthought不是String是CharSequence),但它不会将数据提取到PDF中。
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.
我也尝试过PrintWriter,关闭它以打开OutputStream(不起作用)或刷新它(两者都没有)。
I have also tried to get the PrintWriter, close it to open the OutputStream (didn't work) or flush it (neither).
任何有关使用任何可以显示数据的解决方案的帮助在pdf中会很棒。
非常感谢!
Any help with a solution to use any out that could show the data in a pdf would be great. Thanks a lot!
推荐答案
查看堆栈跟踪会很有用。
Would be useful to see the stack trace.
您可能首先尝试运行健全性检查:修改该代码以简单地将静态字符串(hello world)写入ServletOutputStream并将内容类型设置为text / html。因为它应该工作正常:
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:
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();
}
HTH
这篇关于使用PrintWriter和OutputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!