Struts2中 的type=stream
来源:互联网 发布:ca证书网络检测未通过 编辑:程序博客网 时间:2024/05/16 01:02
struts2中 result="stream",可以实现文件的下载功能。
这里有两篇比较清楚和基础的文章:http://kin111.blog.51cto.com/738881/167475
http://developer.51cto.com/art/200906/130122.htm
我这里的应用是POI在内存中生成excel文件供客户端下载。struts.xml中的配置如下
- <!-- 进销存信息查询统计 -->
- <action name="jxcQuery_*" class="com.chengzhong.action.JxcChaXunAction" method="{1}">
- <result name="success">jxcchaxun.jsp</result>
- <!--进销存信息导出为excel -->
- <result name="toexcelOK" type="stream">
- <param name="contentType">application/vnd.ms-excel</param>
- <param name="contentDisposition">attachment;filename="${fileName}"</param>
- <param name="inputName">excelIS</param>
- </result>
- <result name="error">../404.htm</result>
- </action>
1.type="stream" 把一般内容输出到流
2.参数contentType的地方指定为application/vnd.ms-excel
3.设置为 attachment 将会告诉浏览器下载该文件,filename 指定下载文件保有存时的文件名,若未指定将会是以浏览的页面名作为文件名,如以 download.action 作为文件名。这里使用的是动态文件名,${fileName}。它将通过 Action 的 getFileName() 获得文件名。也就是说Action里面要有一个getFileName ()的方法。
比如下面这个:
- public String getFileName() {
- String fileName = "进销存信息统计表.xls"
- try {
- //中文文件名需要转码为 ISO8859-1,否则乱码
- return new String(fileName.getBytes(), "ISO8859-1");
- } catch (UnsupportedEncodingException e) {
- return "jxc.xls";
- }
- }
或者Action 里面有一个fileName的属性,提供它的set 和get 方法。在程序合适的位置给fileName赋值。这两者本质上应该是一样的。
我刚开始没有用注释处的方法转码,用中文做文件名的时候,它就象前面说的用浏览的页面名作为文件名了。
4.<param name="inputName">excelIS</param>
参数inputName指定输入流的名称。和前面的文件名一样,Action里或者提供一个getExcelIS()的方法,或者提供excelIS属性。
5.对流的操作:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
/************************************************************/
DataImportAndExportTool.InfoToExcel(Info, sheetName).write(baos);
byte[] ba = baos.toByteArray();
baos.close();
excelIS = new ByteArrayInputStream(ba);
/*******************************************************************/
//DataImportAndExportTool.InfoToExcel(Info, sheetName).write(fOut); //这样excel表保存在了服务器上
} catch (Exception e) {
e.printStackTrace();
}
return "toexcelOK";、
DataImportAndExportTool.InfoToExcel(Info, sheetName)是我生成excel的方法,info是一个list,sheetName 是工作薄名,这句执行后生成了一个workbook。
ByteArrayOutputStream类是在创建它的实例时,程序内部创建一个byte型别数组的缓冲区,然后利用ByteArrayOutputStream和ByteArrayInputStream的实例向数组中写入或读出byte型数据。在网络传输中我们往往要传输很多变量,我们可以利用ByteArrayOutputStream把所有的变量收集到一起,然后一次性把数据发送出去。
ByteArrayOutputStream用途很多。其中缓冲是一个。对象的深浅复制也会用到
注意:以下是一些参数说明
contentType
内容类型,和互联网MIME标准中的规定类型一致,例如text/plain代表纯文本,text/xml表示XML,image/gif代表GIF图片,image/jpeg代表JPG图片
inputName
下载文件的来源流,对应着action类中某个类型为Inputstream的属性名,例如取值为inputStream的属性需要编写getInputStream()方法
contentDisposition
文件下载的处理方式,包括内联(inline)和附件(attachment)两种方式,而附件方式会弹出文件保存对话框,否则浏览器会尝试直接显示文件。取值为:
attachment;filename="struts2.txt",表示文件下载的时候保存的名字应为struts2.txt。如果直接写filename="struts2.txt",那么默认情况是代表inline,浏览器会尝试自动打开它,等价于这样的写法:inline; filename="struts2.txt"
bufferSize
下载缓冲区的大小
。在这里面,contentType属性和contentDisposition分别对应着HTTP响应中的头Content-Type和Content-disposition头。
- Struts2中 的type=stream
- 关于struts2 中result type="stream"
- 关于struts2 中result type="stream"
- 关于struts2 中result type="stream"
- 关于struts2 中result type="stream"
- 关于struts2 中result type="stream"
- Struts2 result type="stream"的用法
- struts2 result type=stream
- struts2 <result type = "stream">
- struts2里的struts.xml中 当result的type="stream"
- struts2文件下载 <result type="stream">
- struts2文件下载 <result type="stream">
- struts2文件下载 <result type="stream">
- struts2实现文件导出type="stream"
- Struts2.0默认支持多种格式的result type,stream即是其中的一种。
- Struts2中@Result的type问题
- struts2中Result的type说明
- Struts2 result中type的参数说明
- 串的基本操作
- ajax传递日期参数问题
- http协议学习系列(Cookie和Session)
- OC学习日记07-数组
- 封装几个特别好看的弹窗!
- Struts2中 的type=stream
- mysql游标循环的使用
- 《Unity5实战》一书的勘误
- Android开发之单线程模型中Message、Handler、MessageQueue、Looper之间的关系
- 什么是线程安全和线程不安全
- Android动态加载jar/dex
- 点滴记录,重在积累
- xcode中省略block块中的参数名
- 手机端开发注意事项