1. 程式人生 > >struts2之檔案下載

struts2之檔案下載

在某些應用程式裡, 可能需要動態地把一個檔案傳送到使用者的瀏覽器中, 而這個檔案的名字和存放位置在程式設計時是無法預知的,所以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下載機制很有必要。