文件的下载,需要利用到struts2的Stream结果类型:
首先来看Struts2的返回类型(来自官网).Stream是用来把一个输入流返回到浏览器,这个就是用来文件下载的
Chain Result
Used for Action Chaining
Dispatcher Result
Used for web resource integration, including JSP integration
FreeMarker Result
Used for FreeMarker integration
HttpHeader Result
Used to control special HTTP behaviors
Redirect Result
Used to redirect to another URL (web resource)
Redirect Action Result
Used to redirect to another action mapping
Stream Result
Used to stream an InputStream back to the browser (usually for file downloads)
Velocity Result
Used for Velocity integration
XSL Result
Used for XML/XSLT integration
PlainText Result
Used to display the raw content of a particular page (i.e jsp, HTML)
Tiles Result
Used to provide Tiles integration
Stream结果类的参数(来自官网):
contentType - the stream mime-type as sent to the web browser (default = text/plain). MIME类型(默认是 text / plain)。用来设置HTTP响应里的Content-Type标头
contentLength - the stream length in bytes (the browser displays a progress bar).
字节流的长度(浏览器显示一个进度栏)。用来设置HTTP响应里的Content-Length标头
contentDisposition - the content disposition header value for specifing the file name (default = inline, values are typically attachment;filename="document.pdf".
设置标题为什么文件名(规定文件名=“document.pdf”) 用来设置HTTP响应里的Content-Disposition标头
inputName - the name of the InputStream property from the chained action (default = inputStream).
一个动作类属性的名字,此属性返回的InputStream对象会被发送到浏览器
bufferSize - the size of the buffer to copy from input to output (default = 1024).
缓冲区大小(默认= 1024,单位是字节)。通过InputStream对象读取数据,通过OutputStream对象向浏览器发送数据时的缓冲区的长度
allowCaching if set to 'false' it will set the headers 'Pragma' and 'Cache-Control' to 'no-cahce', and prevent client from caching the content. (default = true)
如果设置为false,它将设置标题语无缓存,防止客户端从缓存读取内容。 (默认= true)
contentCharSet if set to a string, ';charset=value' will be added to the content-type header, where value is the string set. If set to an expression, the result of evaluating the expression will be used. If not set, then no charset will be set on the header
如果设置为一个字符串, charset为设置的字符串,如果没有设置,然后将头没有字符集的设置
e.g:
struts.xml文件内容
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
inputStream
text/css
filename="main.css"
2048
inputStream
application/octet-stream
filename="main.css"
2048
注意:这两个action的区别就是它们的contentType被设置成不同的值,ViewCss是把文件内容发送到浏览器,DownloacCss是下载文件.
Download.java内容
实现了ServletContextAware接口
package com.download.action;
import java.io.InputStream;
import javax.servlet.ServletContext;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;
public class Download extends ActionSupport implements ServletContextAware{
private String filePath;
private ServletContext servletContext;
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public InputStream getInputStream() throws Exception {
return servletContext.getResourceAsStream(filePath);
}
}
index.jsp内容
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>