文件的下載,需要利用到struts2的Stream結(jié)果類型:
首先來看Struts2的返回類型(來自官網(wǎng)).Stream是用來把一個輸入流返回到瀏覽器,這個就是用來文件下載的
Used for Action Chaining | |
Used for web resource integration, including JSP integration | |
Used for FreeMarker integration | |
Used to control special HTTP behaviors | |
Used to redirect to another URL (web resource) | |
Used to redirect to another action mapping | |
Used to stream an InputStream back to the browser (usually for file downloads) | |
Used for Velocity integration | |
Used for XML/XSLT integration | |
Used to display the raw content of a particular page (i.e jsp, HTML) | |
Used to provide Tiles integration |
Stream結(jié)果類的參數(shù)(來自官網(wǎng)):
- 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).
字節(jié)流的長度(瀏覽器顯示一個進度欄)。用來設置HTTP響應里的Content-Length標頭
- contentDisposition - the content disposition header value for specifing the file name (default = inline, values are typically attachment;filename="document.pdf".
設置標題為什么文件名(規(guī)定文件名=“document.pdf”) 用來設置HTTP響應里的Content-Disposition標頭
- inputName - the name of the InputStream property from the chained action (default = inputStream).
一個動作類屬性的名字,此屬性返回的InputStream對象會被發(fā)送到瀏覽器
- bufferSize - the size of the buffer to copy from input to output (default = 1024).
緩沖區(qū)大?。J= 1024,單位是字節(jié))。通過InputStream對象讀取數(shù)據(jù),通過OutputStream對象向瀏覽器發(fā)送數(shù)據(jù)時的緩沖區(qū)的長度
- 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,它將設置標題語無緩存,防止客戶端從緩存讀取內(nèi)容。 (默認= 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文件內(nèi)容
<?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>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="ViewCss" class="com.download.action.DownLoad">
<result type="stream" name="success">
<!-- Stream返回類型的各項參數(shù) -->
<param name="inputName">inputStream</param>
<param name="contentType">text/css</param>
<param name="contentDisposition">filename="main.css"</param>
<param name="bufferSize">2048</param>
</result>
</action>
<action name="DownloadCss" class="com.download.action.DownLoad">
<result type="stream" name="success">
<param name="inputName">inputStream</param>
<param name="contentType">application/octet-stream</param>
<param name="contentDisposition">filename="main.css"</param>
<param name="bufferSize">2048</param>
</result>
</action>
</package>
</struts>
注意:這兩個action的區(qū)別就是它們的contentType被設置成不同的值,ViewCss是把文件內(nèi)容發(fā)送到瀏覽器,DownloacCss是下載文件.
Download.java內(nèi)容
實現(xiàn)了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內(nèi)容
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Download</title>
<style type="text/css">@import url(css/main.css);</style>
</head>
<body>
<div id="global" style="width: 200px">
<s:url id="url1" action="ViewCss">
<s:param name="filePath">css/main.css</s:param>
</s:url>
<s:a href="%{url1}">View CSS</s:a>
<br />
<s:url id="url2" action="DownloadCss">
<s:param name="filePath">css/main.css</s:param>
</s:url>
<s:a href="%{url2}">Download CSS</s:a>
</div>
</body>
</html>

點擊View Css會把文件內(nèi)容顯示出來,點擊Download Css會下載該文件.