I am just writing my solution,helpful for others
If you are implementing PDF file download using struts2,below are the steps
1.in struts.xml or in respective module's struts configuration file
<action name="PDFReport_*" method="{1}" class="your Action Class">
<result name="success" type="stream">
<param name="contentType">application/pdf</param>
<param name="inputName">inputStream</param>
<param name="bufferSize">1024</param>
</result>
</action>
2. in your action class,define inputStream
private InputStream inputStream;
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
3. In the action,assign your stream data to this inputStream.If you are using jasper or some other API to generate PDF File,Those APIs may return PDF file to output stream.Ofcourse, you can write your PDF file to servlet output stream directly. Doing like this will work but you can see the exception 'response has already been committed'.
i have used jasper, So
public
String actionDownload() throws Exception{
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Content-Disposition","attachment; filename=\"" + example.pdf+ "\"");
response.setHeader("Expires", "0");
response.setHeader("Cache-Control",
"must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JasperPrint jp =JasperFillManager.fillReport(....somestuff....);
JasperExportManager.exportReportToPdfStream(jp,baos);
response.setContentLength(baos.size());
ByteArrayInputStream bis=new ByteArrayInputStream(baos.toByteArray());
inputStream = bis;
return "success"
}