• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts2- Files DownLoad(Streaming) - as inline , attachment

 
mohan kumar r
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
we are using Jasper to generate PDF and user can download by clicking on a link.

struts config as below

<result name="success" type="stream">
<param name="contentType">application/pdf</param>
<param name="contentDisposition">filename="document.pdf"</param>
</result>

Inside Action class

JasperExportManager.exportReportToPdfStream(jp,response.getOutputStream());

we have two problems,

1. If the contentDisposition is 'inline',If the PDF file is of bigger sige,the file is not downloading properly, I mean downloaded file contains some binary value like Ċ��c�eF7. But if the contentDisposition is attachement, File is downloading properly.
2. We can see some exception

[2/24/09 18:53:40:362 SGT] 00000027 WebApp E [Servlet Error]-[Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action.]: java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action.at org.apache.struts2.dispatcher.StreamResult.doExecute(StreamResult.java:189)


Please advise.

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "stream" result, by default, expects to find an "inputStream" action property.

You're trying to both write directly to the response stream *and* use the "stream" result: you should do only one or the other.
 
mohan kumar r
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
}

 
ranisri rao
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried with the below 3 steps mentioned below to generate the PDF using the inputStream. But the stream is written to the browser window instead of showing a dialogue box. The browser is displaying the stream data. Can you tell me whether I am missing any thing ?

1.in struts.xml or in respective module's struts configuration file
2. in your action class,define inputStream
3. In the action,assign your stream data to this inputStream.

I am getting the stream from the render() of PD4ML. That stream I had assigned to the inputSteam.
Still data stream is displayed as junk in the browser.

Can you suggest where I am missing ?
 
shekhar john
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello i m also working on downloading the file,but i m working on tiles plugin and i m confused to where i write my param.
 
Mohana Rao Sv
Ranch Hand
Posts: 485
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@sekhar you have to configure in struts.xml file.
 
shekhar john
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok but i am using the Annotaion in my action class.so how can i use param in struts.xml can you please give a sample for that.or any tutorial url .i tried lots of possibilities but i cant get it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic