struts2 中 result type="stream"
Stream result type是Struts2中比较有用的一个feature。特别是在动态生成图片和文档下载的情况下
1:图片验证码:
Action类,action主要要提供一个获取InputStrem的方法:
public class CheckCodeAction extends ActionSupport implements SessionAware {
private Logger log = LoggerFactory.getLogger(this.getClass());
private InputStream imageStream;
private Map session;
public String getCheckCodeImage(String str, int show, ByteArrayOutputStream output) {
Random random = new Random();
BufferedImage image = new BufferedImage(80, 30, BufferedImage.TYPE_3BYTE_BGR);
Font font = new Font("Arial", Font.PLAIN, 24);
int distance = 18;
Graphics d = image.getGraphics();
d.setColor(Color.WHITE);
d.fillRect(0, 0, image.getWidth(), image.getHeight());
d.setColor(new Color(random.nextInt(100) + 100, random.nextInt(100) + 100, random.nextInt(100) + 100));
for (int i = 0; i < 10; i++) {
d.drawLine(random.nextInt(image.getWidth()), random.nextInt(image.getHeight()), random.nextInt(image.getWidth()),
random.nextInt(image.getHeight()));
}
d.setColor(Color.BLACK);
d.setFont(font);
String checkCode = "";
char tmp;
int x = -distance;
for (int i = 0; i < show; i++) {
tmp = str.charAt(random.nextInt(str.length() - 1));
checkCode = checkCode + tmp;
x = x + distance;
d.setColor(new Color(random.nextInt(100) + 50, random.nextInt(100) + 50, random.nextInt(100) + 50));
d.drawString(tmp + "", x, random.nextInt(image.getHeight() - (font.getSize())) + (font.getSize()));
}
d.dispose();
try {
ImageIO.write(image, "jpg", output);
} catch (IOException e) {
log.warn("生成验证码错误.", e);
}
return checkCode;
}
public String execute() throws Exception {
ByteArrayOutputStream output = new ByteArrayOutputStream();
String checkCode = getCheckCodeImage("ABCDEFGHJKLMNPQRSTUVWXYZ123456789", 4, output);
this.session.put(Constants.CHECK_CODE_KEY, checkCode);
//这里将output stream转化为 inputstream
this.imageStream = new ByteArrayInputStream(output.toByteArray());
output.close();
return SUCCESS;
}
public InputStream getImageStream() {
return imageStream;
}
public void setSession(Map session) {
this.session = session;
}
struts配置文件:
<action name="checkCode" class="CheckCodeAction">
<result name="success" type="stream">
<param name="contentType">image/jpeg</param>
<!-- 指定提供InputStream的filed name -->
<param name="inputName">imageStream</param>
<param name="bufferSize">1024</param>
</result>
<interceptor-ref name="defaultStack"/>
</action>
2:文件下载:
我这里的应用是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属性说明
Struts2 中result type属性说明 首先看一下在struts-default.xml中对于result-type的定义: <result-types><result-t ...
- struts2文件下载 <result type="stream">
<!--struts.xml配置--> <action name="download" class="com.unmi.action.DownloadA ...
- Struts2 中 result type=”json” 的参数解释
转自:http://wangquanhpu.iteye.com/blog/1461750 1, ignoreHierarchy 参数:表示是否忽略等级,也就是继承关系,比如:TestAction 继承 ...
- struts2 action result type类型
struts2 action result type类型 1.chain:用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息. com.opensymphony. ...
- struts2.xml 中result type属性说明
chain 用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息. com.opensymphony.xwork2.Acti ...
- Struts2中 Result类型配置详解
一个result代表了一个可能的输出.当Action类的方法执行完成时,它返回一个字符串类型的结果码,框架根据这个结果码选择对应的result,向用户输出.在com.opensymphony.xwor ...
- Struts2 语法--result type
result type: dispatcher,redirect:只能跳转到jsp,html之类的页面,dispatcher属于服务器跳转, redirect属于客户端跳转 chain: 等同于for ...
- 04. struts2中Result配置的各种视图转发类型
概述 <action name="helloworld" class="com.liuyong666.action.HelloWorldAction"&g ...
- Struts2.xml中result type属性说明
在struts2配置XML里,result中type属性有以下几种: 1.dispatcher:服务器跳转到前台,后面跟着可以是JSP.htm等等前台页面,默认是这种. 2.redirect:客户端跳 ...
随机推荐
- nginx.conf的完整配置说明
#用户 用户组 user www www; #工作进程,根据硬件调整,有人说几核cpu,就配几个,我觉得可以多一点 worker_processes 5: #错误日志 error_log logs/e ...
- luogu2633 Count on a tree
主席树放到树上而已 #include <algorithm> #include <iostream> #include <cstdio> using namespa ...
- 【字符串+BFS】Problem 7. James Bond
https://www.bnuoj.com/v3/external/gym/101241.pdf [题意] 给定n个字符串,大小写敏感 定义一个操作:选择任意m个串首尾相连组成一个新串 问是否存在一个 ...
- Toad Oracle 本地/远程数据库导入/导出 数据库备份
1. Toad进入数据库后,选择 Database ==> Export ===> Export Utility Wizard ,选择export user(按用户导出),选择Toa ...
- OpenJudge 6042 雇佣兵
37:雇佣兵 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 雇佣兵的体力最大值为M,初始体力值为0.战斗力为N.拥有X个能量元素. 当雇佣兵的体力值恰好为M时,才可以参加一个 ...
- 【HDOJ6318】Swaps and Inversions(树状数组)
题意: 给定一串数组,其中含有一个逆序对则需要花费x,交换相邻两个数需要花费y,输出最小花费. n<=1e5,-1e9<=a[i]<=1e9 思路: #include<cstd ...
- linux下reboot和shutdown关机命令详解
我 们在操作Linux v/服务器的时候肯定会有需要重启系统,或者关闭系统等操作.有些用户是直接到VPS主机商家面板上操作的,这样一来比较麻烦,二来有些面板还不易于使用 容易导致面板卡死.所以最好的方 ...
- Android服务Service
安卓Service服务 一 Service简介 Service是运行在后台的,没有界面的,用来处理耗时比较长的.Service不是一个单独的进程,也不是一个单独的线程. Service有两种类型 ...
- 使用微软的 ilasm 和 ildasm 对. net程序进行编译和反编译
为了保证示例的完整性,请先准备好一个 c#写的 exe 程序,或者可以使用我提供的 exe 程序也可以(很简单,为了测试这里仅生成了一个带按钮的 winform,单击按钮提示弹窗) Test WinF ...
- 解决maven Generating project in Interactive mode
在idea建一个基于maven结构的web项目时,cmd输出卡死在Generating project in Interactive mode不动了 用命令mvn archetype:generate ...