免费高清特黄a大片,九一h片在线免费看,a免费国产一级特黄aa大,国产精品国产主播在线观看,成人精品一区久久久久,一级特黄aa大片,俄罗斯无遮挡一级毛片

分享

Struts2文件下載

 pint 2012-05-27

1:struts2配置文件如下:

   

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts./dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.   
  8.     <constant name="struts.enable.DynamicMethodInvocation" value="false" />  
  9.     <constant name="struts.devMode" value="false" />  
  10.     <constant name="struts.multipart.maxSize" value="2073741824" />  
  11.   
  12.     <package name="logsin" extends="struts-default">  
  13.   
  14.         <action name="download" class="com.dingxun.download.DownloadAction">  
  15.   
  16.         <interceptors>  
  17.             <interceptor name="downloadinterceptor"  
  18.                 class="com.dingxun.download.DownloadInterceptor">  
  19.             </interceptor>  
  20.             <interceptor-stack name="myinterceptor">  
  21.                 <interceptor-ref name="downloadinterceptor" />  
  22.                 <interceptor-ref name="defaultStack"></interceptor-ref>  
  23.             </interceptor-stack>  
  24.         </interceptors>  
  25.   
  26.         <default-interceptor-ref name="myinterceptor"></default-interceptor-ref>  
  27.   
  28.         <global-results>  
  29.             <result name="login">/login.jsp</result>  
  30.         </global-results>  
  31.           
  32.             <result name="success" type="stream">  
  33.                 <param name="contentType">application/octet-stream</param>    
  34.                 <param name="inputName">inputStream</param>            
  35.                 <param name="contentDisposition">attachment;filename="${file}"</param>  
  36.                 <param name="bufferSize">5120</param>  
  37.             </result>  
  38.         </action>  
  39.     </package>  
  40.   
  41.     <!--   <include file="example.xml"/> -->  
  42.   
  43.     <!-- Add packages here -->  
  44.   
  45. </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代碼如下:

  

  1. import java.io.InputStream;  
  2. import java.io.UnsupportedEncodingException;  
  3. import org.apache.struts2.ServletActionContext;  
  4. import com.opensymphony.xwork2.ActionSupport;  
  5. public class DownloadAction extends ActionSupport{  
  6.   
  7.      private String file;  //    需要下載的文件名,通過鏈接傳過來  
  8.   
  9.      //getFile()是處理保存中文文件名帶來的問題,主要是用來處理保存文件時中文名字亂碼問題  
  10.      public String getFile() {  
  11.       String fileName= "";  
  12.       try {  
  13.        fileName = new String(file.getBytes(),"ISO8859-1");  //把file轉(zhuǎn)換成ISO8859-1編碼格式  
  14.       } catch (UnsupportedEncodingException e) {  
  15.        e.printStackTrace();  
  16.       }  
  17.       return fileName;  
  18.        
  19.      }  
  20.   
  21.      public void setFile(String file) {  
  22.          this.file = file;  
  23.      }  
  24.   
  25.      //   對于上邊的文件路徑,給出他的輸入流(實(shí)際文件資源),對應(yīng)在配置文件中的InputName屬性名  
  26.      public InputStream getInputStream() {  
  27.          System.out.println("得到實(shí)際文件資源file:"+file);  
  28.       InputStream o = ServletActionContext.getServletContext().getResourceAsStream(  
  29.         "upload//" + file);  
  30.       return o;  
  31.      }  
  32.   
  33.      // execute方法只需返回SUCCESS  
  34.      public String execute() throws Exception {  
  35.       return SUCCESS;  
  36.     }  
  37. }  

 

3:jsp頁面如下:

  

  1. <%@ page language="java" contentType="text/html; charset=GBK"  
  2.     pageEncoding="GBK"%>  
  3.     <%@ taglib prefix="s" uri="/struts-tags" %>  
  4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=GBK">  
  8. <title>Insert title here</title>  
  9. </head>  
  10. <body>  
  11. <a href="download.action?file=execute.jar" mce_href="download.action?file=execute.jar">execute.jar</a><br>  
  12. <a href="download.action?file=close.jar" mce_href="download.action?file=close.jar">close.jar</a><br>  
  13. <a href="download.action?file=CMOS.txt" mce_href="download.action?file=CMOS.txt">CMOS.txt</a><br>  
  14. <a href="download.action?file=截圖00.jpg" mce_href="download.action?file=截圖00.jpg">截圖00.jpg</a><br>  
  15. <a href="download.action?file=030484-01.doc" mce_href="download.action?file=030484-01.doc">030484-01.doc</a><br>  
  16. </body>  
  17. </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的編碼格式要和頁面的編碼格式相同

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約