structs实现文件下载例子说明
更新时间:2023-06-12 01:07: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实现文件下载例子说明06-12
2018-2024年中国热熔融成型造粒机行业市场研究及深度专项调查投05-14
材料力学练习题11-02
园艺产品加工学实验指导 14秋季 11食安、生工用07-08
外汇期货12-04
论法律漏洞的填补06-13
数据库sqlserver实验报告09-12
医药设计院名录12-19
- 教学能力大赛决赛获奖-教学实施报告-(完整图文版)
- 互联网+数据中心行业分析报告
- 2017上海杨浦区高三一模数学试题及答案
- 招商部差旅接待管理制度(4-25)
- 学生游玩安全注意事项
- 学生信息管理系统(文档模板供参考)
- 叉车门架有限元分析及系统设计
- 2014帮助残疾人志愿者服务情况记录
- 叶绿体中色素的提取和分离实验
- 中国食物成分表2020年最新权威完整改进版
- 推动国土资源领域生态文明建设
- 给水管道冲洗和消毒记录
- 计算机软件专业自我评价
- 高中数学必修1-5知识点归纳
- 2018-2022年中国第五代移动通信技术(5G)产业深度分析及发展前景研究报告发展趋势(目录)
- 生产车间巡查制度
- 2018版中国光热发电行业深度研究报告目录
- (通用)2019年中考数学总复习 第一章 第四节 数的开方与二次根式课件
- 2017_2018学年高中语文第二单元第4课说数课件粤教版
- 上市新药Lumateperone(卢美哌隆)合成检索总结报告
- 文件下载
- 例子
- structs
- 说明
- 实现
- 2021年大学英语技能辩论赛策划书
- 万科风情商业街城商铺销售营销推广
- 酒店行业应用Hero演示通
- 原告李某某与被告张某某离婚纠纷一案
- 2015三级河南省建造师施工管理考试技巧重点
- 城市生活垃圾分类标志GBT19095-2003
- 2011-2012听评课活动的实施方案
- chap 3 软件测试技术--黑盒测试及其用例的设计
- 办人民满意的教育实施方案
- 新疆分公司营运部团员青年思想状况调研报告
- 《黄冈小状元》43-49订正
- 激光焊接基本原理
- 电动汽车行驶里程与电池SOC相关性分析与建模
- 公办中小学校章程参考样本
- 《复兴之路》观后感
- 定时闹钟课程设计大作业
- 科技兴趣小组活动总结
- Sketchup建筑模型制作操作流程
- 第七章 机械的运转及其速度波动的调节
- 中学生必背古诗词八十首