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
下载缓冲区的大小
Struts2下载实现源代码
< ?xml version="1.0" encoding="UTF-8" ?>"-//ApacheSoftwareFoundation//DTD Struts Configuration 2.0//EN""<Ahref="http://struts.apache.org/dtds/struts-2.0.dtd">
http://struts.apache.org/dtds/struts-2.0.dtd< /A>">
< PACKAGE name="default" extends="struts-default"><!--在这里添加Action定义 -->
< !-- 简单文件下载 -->
< ACTIONclass=example.FileDownloadActionname="download">
< RESULT name="success" type="stream">
< M name="contentType">text/plain< /M>
< M name="inputName">inputStream< /M>
<Mname="contentDisposition">attachment;filename="struts2中文.txt"</M>
< M name="bufferSize">4096< /M>
< /RESULT>
< /ACTION>
< BR> < !-- 文件下载,支持中文附件名 -->
< ACTIONclass=example.FileDownloadAction2name="download2">
< !-- 初始文件名 -->
< M name="fileName">Struts中文附件.txt< /M>
< RESULT name="success" type="stream">
< M name="contentType">text/plain< /M>
< M name="inputName">inputStream< /M>
< !-- 使用经过转码的文件名作为下载文件名,downloadFileName属性
对应action类中的方法 getDownloadFileName() -->
<Mname="contentDisposition">attachment;filename="$downloadFileName"</M>
< M name="bufferSize">4096< /M>
< /RESULT>
< /ACTION>
< !-- 下载现有文件 -->
< ACTIONclass=example.FileDownloadAction3name="download3">
< M name="inputPath">/download/系统说明.doc< /M>
< !-- 初始文件名 -->
< M name="fileName">系统说明.doc< /M>
< RESULT name="success" type="stream">
<Mname="contentType">application/octet-stream;charset=ISO8859-1</M>
< M name="inputName">inputStream< /M>
< !-- 使用经过转码的文件名作为下载文件名,downloadFileName属性
对应action类中的方法 getDownloadFileName() -->
<Mname="contentDisposition">attachment;filename="$downloadFileName"</M>
< M name="bufferSize">4096< /M>
< /RESULT>
< /ACTION>
< /PACKAGE>