structs实现文件下载例子说明
更新时间:2023-08-18 12:59:01 阅读量: 资格考试认证 文档下载
- struct什么函数推荐度:
- 相关推荐
在实际web应用中,大部分文件下载都是通过url文件链接直接下载的,同样在Struts中也可以这样实现。但是考虑到盗链,跨服务器访问等因素,直接文件流下载也是必要的。那么,在Struts2.0中如何实现数据流下载呢?
Struts2.0默认支持多种格式的result type,stream即是其中的一种。如果我这里要实现一个Generate Report的功能,将Report存放在一个InputStream里面,Action的示例代码内容如下:
Java代码
1. package com.test;
2.
3. import java.io.InputStream;
4. import java.util.HashMap;
5. import java.util.Map;
6.
7. public class ReportsAction extends ActionSupport {
8.
9. // 定义HTML类型的Report
10. private static final int HTML_TYPE = 0;
11.
12. // 定义EXCEL类型的Report
13. private static final int EXCEL_TYPE = 1;
14.
15. // Report类型
16. private int reportType;
17.
18. // Report输出流
19. public InputStream reportStream;
20.
21. // 输出流Content Type
22. public String contentType;
23.
24. // 输出流的生成的文件名
25. public String fileName;
26.
27. public ReportsAction() {
28. }
29.
30. public String getContentType() {
31. return contentType;
32. }
33. 34. public String getFileName() {
35. return fileName;
36. }
37.
38. public InputStream getReportStream() {
39. return reportStream;
40. }
41.
42. public int getReportType() {
43. return reportType;
44. }
45.
46. public void setReportType(int reportType) {
47. this.reportType = reportType;
48. }
49.
50. public String generateReport() {
51. switch (reportType) {
52. case HTML_TYPE:
53. // 获取HTML流
54. reportStream = service.getHtmlStream();
55. // contentType为MIME定义的,详细的内容可参考下面的这个网站:/media/media_mimeref.asp
56. contentType = "text/html";
57. // inline表示文件直接输出到网页上,不出现保存打开对话框
58. fileName = "inline; filename=\"Report.htm\"";
59. break;
60. case EXCEL_TYPE:
61. // 获取EXCEL流
62. reportStream = service.getExcelStream();
63. // contentType设定
64. contentType = "application/vnd.ms-excel";
65. // attachment表示网页会出现保存、打开对话框
66. fileName = "attachment; filename=\"Report.xls\"";
67. break;
68. default:
69. ;
70. }
71. return SUCCESS;
72. }
73.
74.}
当然,Struts的配置也是非常重要的,如下:
Xml代码
1. <!DOCTYPE struts PUBLIC
2. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
3. "/dtds/struts-2.0.dtd">
4. <struts>
5. <package name="root" namespace="/">
6. <action name="generateReport" method="generateReport"
7. class="com.test.ReportsAction">
8. <result name="success" type="stream">
9. <!-- 对应ReportsAction中的属性
contentType -->
10. <param name="contentType">${contentType}</param>
11. <!-- ReportsAction中对应的InputStream的属性名 -->
12. <param name="inputName">reportStream</param>
13. <!-- 对应ReportsAction中的属性fileName,定义流输出格式 -->
14. <param name="contentDisposition">${fileName}</param>
15. <!-- 定义bufferSize,可选 -->
16. <param name="bufferSize">1024</param>
17. </result>
18. ...
19. </action>
20. </package>
21.</struts>
页面部分我就不详细写了,比如,可以在一个form的提交中绑定这个action,普通的网页调用代码如下:
Html代码
1. <form id="generateReportForm" action="generateReport.action" method="POST">
2. </form>
当然,你也可以用一个Struts中的标签来实现,示例代码如下: Xml代码
1. <s:form theme="simple" validate="true">
2. <s:submit cssStyle="width:160px" action="generateReport" value="Generate HTML Report" />
3. <s:url id="generateUrl" action="generateReport"></s:url>
4. <s:a href="%{generateUrl}"><s:textfield name="tail.button.generatexls" /></s:a>
5. </s:form>
正在阅读:
structs实现文件下载例子说明08-18
后张梁施工方案 12-07
档案安全工作自查报告12-12
单位根检验和误差修正模型_原理及应用07-22
诊断病理学 - 图文11-24
2013-2014学年下学期小学六年级数学期末试卷有答案04-09
多媒体辅助英语专业翻译教学的实践12-26
交大港航-渠化工程练习和思考题和答案10-09
女人为什么爱购物02-09
- 梳理《史记》素材,为作文添彩
- 2012呼和浩特驾照模拟考试B2车型试题
- 关于全面推进施工现场标准化管理实施的通知(红头文件)
- 江西省房屋建筑和市政基础设施工程施工招标文件范本
- 律师与公证制度第2阶段练习题
- 2019-2020年最新人教版PEP初三英语九年级上册精编单元练习unit6训练测试卷内含听力文件及听力原文
- 小升初数学模拟试卷(十四) 北京版 Word版,含答案
- 认识创新思维特点 探讨创新教育方法-精选教育文档
- 00266 自考 社会心理学一(复习题大全)
- 多媒体在语文教学中的运用效果
- 派出所派出所教导员述职报告
- 低压电工作业考试B
- 18秋福建师范大学《管理心理学》在线作业一4
- 中国铝业公司职工违规违纪处分暂行规定
- 13建筑力学复习题(答案)
- 2008年新密市师德征文获奖名单 - 图文
- 保安员培训考试题库(附答案)
- 银川市贺兰一中一模试卷
- 2011—2017年新课标全国卷2文科数学试题分类汇编 - 1.集合
- 湖北省襄阳市第五中学届高三生物五月模拟考试试题一
- 文件下载
- 例子
- structs
- 说明
- 实现