I want to give file download using java,struts2 and aja开发者_如何学Cx.
On my html page there is a button called "export" clicking on which ajax call will be made which will execute a query and will create .xls file using code and I want to give that file for download to user without storing it on hard drive.
Does any one know how to do that using struts2 and ajax in java?
Is there any example available?
Let me know if you need more details from me...
Thanks.
amar4kintu
You don't have to use AJAX in this case. Just have your button submit the form to your Struts action, and have the action use the stream result type.
Example:
In your Struts XML:
<result name="download" type="stream">
<param name="contentDisposition">attachment;filename=report.xls</param>
<param name="contentType">application/vnd.ms-excel</param>
<param name="inputName">inputStream</param>
<param name="bufferSize">1024</param>
</result>
Your action will then provide a public InputStream getInputStream()
to pass along the data.
I presume that whatever library you're using to generate the Excel files (POI?) can write the output to an arbitrary OutputStream
.
A quick-and-dirty way to convert that to an InputStream
:
// Using Workbook from Apache POI for example...
Workbook wb;
// ...
ByteArrayOutputStream bos = new ByteArrayOutputStream();
wb.write(bos);
InputStream bis = new ByteArrayInputStream(bos.toByteArray());
Just for your reference, we can do the same using Annotation:
public class MyAction {
private InputStream fileInputStream;
private String logoName;
@Action(value="/downloadLogo",
results={
@Result(name="success", type="stream",
params = {
"contentType", "application/image/gif",
"inputName", "fileInputStream",
"contentDisposition", "filename=\"${logoName}\"",
"bufferSize", "1024"
})
}
)
public String downloadLogo() throws Exception {
logoName = "test.jpg";
fileInputStream = new FileInputStream(new File("DirePath", logoName));
}
}
As a follow-up to amar4kintu's question regarding files saved as ExportReport.action instead of report.xls, this happens in IE if the following format is not followed in your struts.xml file:
<result name="download" type="stream">
<param name="contentDisposition">attachment;filename="${flashcardSetBean.title}.xlsx"</param>
<param name="contentType">application/vnd.openxmlformats-officedocument.spreadsheetml.sheet</param>
<param name="inputName">inputStream</param>
<param name="bufferSize">1024</param>
</result>
It seems like the contentDisposition
line in particular must indicate that the file is an attachment and that the filename is surrounded by quotes.
I'd use this kind of annotation on the Action class:
@Result(name = "success", type= StreamResult.class,
params = {"contentType", "application/vnd.ms-excel",
"contentDisposition", "attachment; filename=report.xls"},
value = "reportFileStream"
)
A better approach to pipe outstream to an inputstream is explained below as opposed to the response by ZoogieZork above
InputStream is = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream((PipedInputStream) is);
wb.write(out);
The advantage of this method is that the entire buffer is not stored in memory but is rather piped using a small internal circular buffer. This is better both in terms of memory as well as CPU overhead.
Reference: Convert a Java OutputStream to an InputStream
atlast, I was able to solve it as following..
I wrote following line in my action class function and I was able to download file with name report.xls
instead of ExportReport.action
. I do not know exactly .. why?
response.setHeader("Content-Disposition","attachment;filename=rpt.xls");
Following is in my struts.xml
file. I removed <contentDispositin>
param from it because it was not working from struts.xml
file and I put it in my action Java file as above.
<result name="success" type="stream" >
<param name="inputName">fileStream</param>
<param name="contentType">application/vnd.ms-excel</param>
<param name="bufferSize">1024</param>
</result>
Hope this will help someone.
Thanks.
amar4kintu
精彩评论