Struts2下载文件实现的说明
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头。
如下例:
down.jsp
<a href="<s:url value='test/fileDown.do?fileName=struts2配置参数详解.txt'> </s:url>">下载</a>
struts.xml配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="com" namespace="/test" extends="struts-default">
<action name="fileDown" class="it.com.down.FileDown">
<result type="stream">
<!-- 下载文件类型定义 -->
<param name="contentType">
application/octet-stream
</param>
<!-- 下载文件输出流定义 -->
<param name="inputName">inputStream</param>
<!-- 下载文件处理方法 -->
<param name="contentDisposition">
attachment;filename="${fileName}"
</param>
<!-- 下载文件的缓冲大小 -->
<param name="bufferSize">4096</param>
</result>
</action>
</package>
</struts>
文件下载Action:
public class FileDown extends ActionSupport {
private String fileName;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public InputStream getInputStream() throws UnsupportedEncodingException {
return ServletActionContext.getServletContext().getResourceAsStream(
"/WEB-INF/" + fileName);
}
public String execute(){
System.out.println(fileName+"----------");
return "success";
}
}
这个例子运行可,可能会出现:下载页面的文件名为:fileDown.do或htm等情况。
这在实际保存时,要改成相应的文件名,在不知道原文件类型情况下就无法改了。
这种情况,可以这样解决:
将struts.xml配置改为:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="com" namespace="/test" extends="struts-default">
<result type="stream">
<!-- 下载文件类型定义 -->
<param name="contentType">
application/octet-stream
</param>
<!-- 下载文件输出流定义 -->
<param name="inputName">inputStream</param>
<!-- 下载文件的缓冲大小 -->
<param name="bufferSize">4096</param>
</result>
</action>
</package>
</struts>
文件下载action改为:
public class FileDown extends ActionSupport {
private String fileName;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public InputStream getInputStream(){
HttpServletResponse response = ServletActionContext.getResponse();
response.setHeader("Content-Disposition", "attachment;fileName="+
fileName);
return ServletActionContext.getServletContext().getResourceAsStream(
"/WEB-INF/" + fileName);
}
public String execute(){
System.out.println(fileName+"----------");
return "success";
}
}
这样就行了。
如果下载文件名称是中文,会出现乱码现象,解决方案:
将上面action相应的改为如下两步
(1)设置:
public void setFileName(String fileName) throws UnsupportedEncodingException {
this.fileName = new String(fileName.getBytes("ISO-8859-1"),"UTF-8");
}
(2)设置:
response.setHeader("Content-Disposition", "attachment;fileName="
+ java.net.URLEncoder.encode(fileName,"UTF-8"));
问题解决。