1:struts2配置文件如下:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts./dtds/struts-2.0.dtd">
-
- <struts>
-
- <constant name="struts.enable.DynamicMethodInvocation" value="false" />
- <constant name="struts.devMode" value="false" />
- <constant name="struts.multipart.maxSize" value="2073741824" />
-
- <package name="logsin" extends="struts-default">
-
- <action name="download" class="com.dingxun.download.DownloadAction">
-
- <interceptors>
- <interceptor name="downloadinterceptor"
- class="com.dingxun.download.DownloadInterceptor">
- </interceptor>
- <interceptor-stack name="myinterceptor">
- <interceptor-ref name="downloadinterceptor" />
- <interceptor-ref name="defaultStack"></interceptor-ref>
- </interceptor-stack>
- </interceptors>
-
- <default-interceptor-ref name="myinterceptor"></default-interceptor-ref>
-
- <global-results>
- <result name="login">/login.jsp</result>
- </global-results>
-
- <result name="success" type="stream">
- <param name="contentType">application/octet-stream</param>
- <param name="inputName">inputStream</param>
- <param name="contentDisposition">attachment;filename="${file}"</param>
- <param name="bufferSize">5120</param>
- </result>
- </action>
- </package>
-
- <!-- <include file="example.xml"/> -->
-
- <!-- Add packages here -->
-
- </struts>
備注1 contentType:指定被下載文件的文件類型。 application/octet-stream 默認(rèn)值,可以下載所有類型 inputName:指定被下載文件的入口輸入流, 和DownloadAction中的getInputStream()對應(yīng),主要是獲得實(shí)際資源文件 contentDisposition:指定下載的文件名,一般和文件名一致,但是要注意中文件名保存時亂碼問題,解決辦法就是進(jìn)行編碼處理 如:<param name="contentDisposition">attachment;filename="${file}"</param> 要求DownloadAction中的getFile()返回的文件名編碼方式為
ISO8859-1,所以要進(jìn)行下面處理: public String getFile() { String fileName= ""; try { fileName = new String(file.getBytes(),"ISO8859-1"); //把file轉(zhuǎn)換成ISO8859-1編碼格式 } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return fileName; }
bufferSize:指定下載文件時的緩沖大小。
備注2 <result name="success" type="stream"> 要注意的是result的type類型要為:stream 備注3 上面的攔截器主要是用來設(shè)置下載權(quán)限的,如果用戶沒有登陸則不能下載,在配置攔截器時間要注意順序問題
2:DownloadAction代碼如下:
- import java.io.InputStream;
- import java.io.UnsupportedEncodingException;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class DownloadAction extends ActionSupport{
-
- private String file;
-
-
- public String getFile() {
- String fileName= "";
- try {
- fileName = new String(file.getBytes(),"ISO8859-1");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- return fileName;
-
- }
-
- public void setFile(String file) {
- this.file = file;
- }
-
-
- public InputStream getInputStream() {
- System.out.println("得到實(shí)際文件資源file:"+file);
- InputStream o = ServletActionContext.getServletContext().getResourceAsStream(
- "upload//" + file);
- return o;
- }
-
-
- public String execute() throws Exception {
- return SUCCESS;
- }
- }
3:jsp頁面如下:
- <%@ page language="java" contentType="text/html; charset=GBK"
- pageEncoding="GBK"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=GBK">
- <title>Insert title here</title>
- </head>
- <body>
- <a href="download.action?file=execute.jar" mce_href="download.action?file=execute.jar">execute.jar</a><br>
- <a href="download.action?file=close.jar" mce_href="download.action?file=close.jar">close.jar</a><br>
- <a href="download.action?file=CMOS.txt" mce_href="download.action?file=CMOS.txt">CMOS.txt</a><br>
- <a href="download.action?file=截圖00.jpg" mce_href="download.action?file=截圖00.jpg">截圖00.jpg</a><br>
- <a href="download.action?file=030484-01.doc" mce_href="download.action?file=030484-01.doc">030484-01.doc</a><br>
- </body>
- </html>
注意 這里唯一要說明的一點(diǎn)是鏈接包含有中文的時候處理辦法,因?yàn)殒溄拥膶?shí)際上是按get方式傳送的,所以可以在Tomcat中做處理 在server.xml中加入URIEncoding="編碼格式" 如:<Connector port="8080" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK" /> 但還有一點(diǎn)要注意,那就是URIEncoding的編碼格式要和頁面的編碼格式相同
|