This code snippet shows the correct way to write file steam to the HTTP Response in an Struts2 applicaiton
// Following entry in the Struts Config XML
<action name="exportToPdf" class="pdfGenerateAction" method="fnExportToPdf">
<result name="SUCCESS" type="stream">
<param name="contentType">application/pdf</param>
<param name="inputName">dataStreamForPdf</param>
<param name="bufferSize">1024</param>
</result>
</action>
Note: Make sure that the inputName param is defined in the action class as a java.io.InputStream with exactly the same name as defined in the struts config xml.
Following is the code in the Struts2 Action class
import com.lowagie.text.pdf.*;
import com.lowagie.text.*;
public class BacklogSelectionScreenAction
{
private InputStream dataStreamForPdf;
public String fnExportToPdf()
{
String status = "SUCCESS";
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
Document myDocument =new Document();
PdfWriter.getInstance(myDocument,outputStream);
myDocument.open();
dataStreamForPdf = (new ByteArrayInputStream(outputStream.toByteArray()));
}
catch(Exception ex)
{
ex.printStackTrace();
status = "ERROR";
}
return status;
}
}