关于struts2 中result type="stream"
来源:互联网 发布:免费制作网站软件 编辑:程序博客网 时间:2024/05/16 07:32
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 中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 result type="stream"的用法
- struts2文件下载 <result type="stream">
- struts2文件下载 <result type="stream">
- struts2里的struts.xml中 当result的type="stream"
- Struts2中result type
- 关于struts2的result type
- Struts2中 的type=stream
- 关于struts2中result的type配置记录
- struts2中result type介绍
- 关于struts2的xml中<result type="redirect">参数问题
- Struts2的中文乱码解决
- 阻止分段攻击HotWC3网站注册散列数的依据
- poj 1363
- 深入详解保护模式下的内存分页机制
- linux ubuntu上如何安装pyWebQQ
- 关于struts2 中result type="stream"
- hibernate对应的ORDBMS的方言
- 数据库迁移大总结。。(sqlserver2000迁移到-2008-mysql5.0)
- 重构第十一章学习
- [AIX] ksh中的命令行编辑
- 字符编码详解及由来
- 音频视频编程相关:GStreamer/ffmpeg/directshow/vfw linux和window下几种流行的音频视频编程框架作一个总结,防止自己迷惘,免于晕头转向。
- 使用的Aptana插件开发AIR应用程序
- ISO8583接口的详细资料