structs实现文件下载例子说明

更新时间:2023-06-12 01:07:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

在实际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>

本文来源:https://www.bwwdw.com/article/n6w1.html

Top