I'm using Jasper to create PDF's through my Actions and cannot figure out a
good way to export the resulting byte[] to the user.
Currently the tail end of my Action class looks like this:
byte[] reportBytes = null;
File jasperReport = new File(location);
try {
reportBytes = JasperRunManager.runReportToPdf(jasperReport.getPath(),
parameters, dataSource);
log.info("Found : " + reportBytes.length);
} catch (JRException e) {
e.printStackTrace();
}
response.setContentType("application/pdf");
response.setContentLength(reportBytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(reportBytes, 0, reportBytes.length);
ouputStream.flush();
ouputStream.close();
return(null);
This will display the pdf, but the action class itself somehow get called a
second time as the above log statement is displayed twice.
I also tried to set the byte[] on the request and forward onto another page
which does:
<%
byte[] reportBytes =
(byte[])request.getAttribute("REPORT_BYTES");
response.setContentType("application/pdf");
response.setContentLength(reportBytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(reportBytes, 0, reportBytes.length);
ouputStream.flush();
ouputStream.close();
%>
But this complains about: getOutputStream() has already been called for this
response
Any ideas?