struts2之文件下载

时间:2022-10-21 14:55:55

在某些应用程序里, 可能需要动态地把一个文件发送到用户的浏览器中, 而这个文件的名字和存放位置在编程时是无法预知的,所以struts2提供了文件下载机制。
在struts2 中使用 type="stream" 的 result 进行下载,但是需要对result配置参数,这些参数在struts-2.3.4/docs/WW/stream-result.html目录下有详细的解释,在这里对参数文档大概解释一下:

//内容类型
contentType - the stream mime-type as sent to the web browser (default = text/plain).
//流的大小 即 下载文件的长度,以字节为单位
contentLength - the stream length in bytes (the browser displays a progress bar).
//设定 Content-Dispositoin 响应头. 该响应头指定接应是一个文件下载类型, 一般取值为 attachment;filename="document.pdf".
contentDisposition - the content disposition header value for specifing the file name (default = inline,
values are typically attachment;filename="document.pdf".
//指定文件输入流的 getter 定义的那个属性的名字. 默认为 inputStream
inputName - the name of the InputStream property from the chained action (default = inputStream).
//缓存大小,默认为1024字节
bufferSize - the size of the buffer to copy from input to output (default = 1024).
//是否允许使用缓存 默认为true
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)
//指定下载的字符集
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

//这些参数也可以在Action类中通过getter方法获取
These parameters can also be set by exposing a similarly named getter method on your Action. For example, you
can provide getContentType() to override that parameter for the current action.

我们可以知道可以通过文件配置或在Action类中用getter方法给参数赋值,那么下面的例子就结合起来说明:

Action类:

public class FileDownloadAction extends ActionSupport {

private static final long serialVersionUID = 1L;
/**
* 声明contentLength,contentDisposition,inputStream参数
* 提供getter方法给它们赋值
*/
private long contentLength;
private String contentDisposition;
private InputStream inputStream;

public long getContentLength() {
return contentLength;
}
public String getContentDisposition() {
return contentDisposition;
}
public InputStream getInputStream() {
return inputStream;
}

@Override
public String execute() throws Exception {
//获取ServletContext对象,
ServletContext servlet = ServletActionContext.getServletContext();
//获取文件所在路径
String fileName = servlet.getRealPath("/files/test.txt");
inputStream = new FileInputStream(fileName);//创建inputStream对象
contentLength = inputStream.available();//给文件内容长度赋值
contentDisposition = "attachment;filename=test.txt";//指定响应头

return super.execute();
}

}
在struts.xml文件中配置

<action name="fileDownload" class="com.filedownload.action.FileDownloadAction">
<!-- 将返回结果的type属性设为stream -->
<result name="success" type="stream">
<!-- 设置下载文件的内容类型为 text/plain
设置缓冲区大小为2048字节
-->
<param name="contentType">text/plain</param>
<param name="bufferSize">2048</param>
</result>
</action>
在页面上通过超链接下载

<a href="fileDownload">fileDownload</a>


通过这几个步骤,struts2下载就可以完成了。在例子中的文件是写死的,用来做测试,在实际开发中提供下载的内容很多的时候或者需要动态生成的话,运用struts2下载机制很有必要。