You may not have done so directly but several things in your code are suspect and can be modified to get the desired response. The exception you've gotten does not occur for any other reason other than trying to claim the response output stream after the servlet container has attempted to do so or doing so twice
1) The lines
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
HttpServletResponse httpServletResponse = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
ServletContext servletContext = (ServletContext) externalContext.getContext();
is making repeated (and unnecessary calls) to context resources.
2) You've failed to call responseComplete()
on your FacesContext
instance which will almost certainly guarantee that writing a file for download to the stream will fail
3)While I'm not certain on this, I'd recommend you just move your report processing from the actionListener
to action
on your commandButton
and remove the ActionListener
argument from the method signature accordingly
4)I don't know what type jasperPrint
is, but you can use JasperReport's JasperRunManager.runReportToPdfStream()
function that accepts an input stream of .jasper
file to output your report.
You can combine all that and use the following :
FacesContext facesContext = FacesContext.getCurrentInstance(); //Get the context ONCE
HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
InputStream reportStream = facesContext.getExternalContext().getResourceAsStream("/web/ireport/monthlyReport.jasper");
try {
ServletOutputStream servletOutputStream = response.getOutputStream();
response.setContentType("application/pdf");
facesContext.responseComplete();
try { // Replace this with your desired JR utility method
JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, params);
} catch (JRException ex) {
//
}
servletOutputStream.flush();
servletOutputStream.close();
} catch (IOException ex) {
//
} catch (Exception ex) {
//
}
Unrelated to your question, you need to be absolutely sure that the path /web/ireport/*
is secure. Looks to me like a publicly accessible path.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…