aspose file tools*
The moose likes Struts and the fly likes Struts2- Files DownLoad(Streaming) - as inline , attachment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2- Files DownLoad(Streaming) - as inline , attachment" Watch "Struts2- Files DownLoad(Streaming) - as inline , attachment" New topic
Author

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

mohan kumar r
Ranch Hand

Joined: Jan 19, 2006
Posts: 38
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.


mohan
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jan 19, 2006
Posts: 38
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

Joined: Nov 02, 2010
Posts: 1
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

Joined: Feb 02, 2011
Posts: 38
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

Joined: Aug 01, 2007
Posts: 485

@sekhar you have to configure in struts.xml file.


ocjp 6 — Feeding a person with food is a great thing in this world. Feeding the same person by transferring the knowledge is far more better thing. The reason is the amount of satisfaction which we get through food is of only one minute or two. But the satisfaction which we can get through the knowledge is of life long.
shekhar john
Ranch Hand

Joined: Feb 02, 2011
Posts: 38
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.
 
Consider Paul's rocket mass heater.
 
subject: Struts2- Files DownLoad(Streaming) - as inline , attachment