skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Thursday, September 13, 2012

Write file stream to response in Struts2 applicaiton

Posted by Admin at 10:14 PM – 0 comments
 

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;
  
 }


}

Labels: Struts2 Example

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger