在我的JasperReports项目中,我创建了报告并尝试生成报告,但不断得到以下异常:
Severe: java.lang.IllegalStateException: getWriter() has already been called for this response
at org.apache.catalina.connector.Response.getOutputStream(Response.java:746)
at org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:206)
at com.vehicletracking.vtss.classes.jsfbean.JasperReportBean.pdf(JasperReportBean.java:35)
at com.vehicletracking.vtss.classes.jsfbean.DailyTrackingBean.generatePDFReport(DailyTrackingBean.java:471)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at javax.el.ELUtil.invokeMethod(ELUtil.java:326)
at javax.el.BeanELResolver.invoke(BeanELResolver.java:536)
at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
at com.sun.el.parser.AstValue.getValue(AstValue.java:136)
这是我的代码:
public class JasperReportBean {
public String pdf(List<DailyTrakingReportBean> jasperReportList) throws JRException, IOException {
try {
JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(jasperReportList);
String reportPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/reports/newReport.jasper");
JasperPrint jasperPrint = JasperFillManager.fillReport(reportPath, new HashMap(), beanCollectionDataSource);
HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
httpServletResponse.addHeader("Content-disposition", "attachment; filename=FleetReport.pdf");
ServletOutputStream outputStream = httpServletResponse.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
FacesContext.getCurrentInstance().responseComplete();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
这是我的图书馆:
我无法理解我在做错的地方。