文件下载使用Java,Struts 2的和AJAX [英] File download using Java, Struts 2 and AJAX

查看:149
本文介绍了文件下载使用Java,Struts 2的和AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用java,struts2的和Ajax给文件下载。

I want to give file download using java,struts2 and ajax.

在我的html页面有一个名为导出按钮,点击其上Ajax调用将作出安排,以执行查询并使用code将创建.xls文件,我想给这个文件下载到用户没有硬盘驱动器上存储了。

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.

有没有人知道怎么说struts2的使用和Ajax的java做?

Does any one know how to do that using struts2 and ajax in java?

有没有例子可用?

让我知道,如果你从我需要更多的细节......

Let me know if you need more details from me...

感谢。

amar4kintu

amar4kintu

推荐答案

您不必使用AJAX在这种情况下。只要有你的按钮提交表单到你的Struts动作,并有操作使用流结果键入

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.

例如:

在你的Struts的XML:

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>

您的行动随后将提供一个公开的InputStream的getInputStream()一起传递数据。

Your action will then provide a public InputStream getInputStream() to pass along the data.

我presume,不管库,你正在使用生成Excel文件( POI ?)可以输出写入到任意的OutputStream

I presume that whatever library you're using to generate the Excel files (POI?) can write the output to an arbitrary OutputStream.

一个快速和肮脏的方式将其转换成一个的InputStream

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());

这篇关于文件下载使用Java,Struts 2的和AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆