今天用jsp显示jasperReport时报如下错误:
[14:48:13.906] java.lang.IllegalStateException: http://127.0.0.1:8083/test.jsp: tried to write 1544 bytes with content-length 1439
[14:48:13.906] at com.caucho.server.connection.ResponseStream.lengthException(ResponseStream.java:548)
[14:48:13.906] at com.caucho.server.connection.ResponseStream.writeNext(ResponseStream.java:416)
[14:48:13.906] at com.caucho.server.connection.ToByteResponseStream.flushByteBuffer(ToByteResponseStream.java:518)
[14:48:13.906] at com.caucho.server.connection.ToByteResponseStream.flushBuffer(ToByteResponseStream.java:424)
[14:48:13.906] at com.caucho.server.connection.ResponseStream.flush(ResponseStream.java:579)
[14:48:13.906] at com.caucho.server.connection.ServletOutputStreamImpl.flush(ServletOutputStreamImpl.java:73)
[14:48:13.906] at _jsp._test__jsp._jspService(_test__jsp.java:77)
[14:48:13.906] at com.caucho.jsp.JavaPage.service(JavaPage.java:60)
[14:48:13.906] at com.caucho.jsp.Page.pageservice(Page.java:570)
jsp如下:
<%@ page language="java" pageEncoding="GBK"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.util.Map"%>
<%@page import="java.io.File"%>
<%@page import="java.util.HashMap"%>
<%@page import="net.sf.jasperreports.engine.JasperCompileManager"%>
<%@page import="net.sf.jasperreports.engine.JasperRunManager"%>
<%@page import="net.sf.jasperreports.engine.JasperPrint"%>
<%@page import="net.sf.jasperreports.engine.JasperFillManager"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Enumeration"%>
<%@page import="net.sf.jasperreports.engine.export.JRXlsExporter"%>
<%@page import="net.sf.jasperreports.engine.JRExporterParameter"%>
<%@page import="net.sf.jasperreports.engine.export.JRXlsExporterParameter"%>
<%@page import="java.io.ByteArrayOutputStream"%>
<%@page import="net.sf.jasperreports.engine.export.JRHtmlExporterParameter"%>
<%@page import="net.sf.jasperreports.engine.export.JRHtmlExporter"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="javax.servlet.ServletOutputStream"%>
<%@page import="net.sf.jasperreports.engine.util.JRProperties"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
Connection connection = null;
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@192.168.2.10:1521:ytsafe";
String user = "system";
String pwd = "ytsafe";
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, user, pwd);
String path = application.getRealPath("/jasper");
path += "/";
String libsPath=application.getRealPath("WEB-INF/lib")+"/jasperreports-1.3.3.jar";
JRProperties.setProperty(JRProperties.COMPILER_CLASSPATH, libsPath);
System.setProperty("jasper.reports.compile.class.path", libsPath);
JasperCompileManager.compileReportToFile(path+"lossA.jrxml",path+"lossA.jasper");
//JasperCompileManager.compileReportToFile(path+"pieTest.jrxml",path+"pieTest.jasper");
Map map = new HashMap();
map.put("entpID", 101);
byte[] bytes = JasperRunManager.runReportToPdf(path+"lossA.jasper", map, connection);
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
connection.close();
out.clear();
out = pageContext.pushBody();
} catch (Exception e) {
e.printStackTrace();
}
%>
原来,jsp在编译成servlet时,会把换行符等各种标点也一起编译,导致文件长度和response.setContentLength(bytes.length);不一致。解决办法如下,新建一个servlet,把上面jsp的内容贴进去,OK!