Struts2文件下載

文件下載是一個很常見的功能,用struts2實現文件下載的步驟:


一)定義一個Action類,FileDownload.Java

  1. package com.struts2.filedownload;  
  2.   
  3. import java.io.InputStream;  
  4.   
  5.   
  6. import org.apache.struts2.ServletActionContext;  
  7.   
  8.   
  9. import com.opensymphony.xwork2.ActionSupport;  
  10.   
  11. //文件下載  
  12. public class FileDownload extends ActionSupport{  
  13.       
  14.     private int number ;  
  15.   
  16.     private String fileName;  
  17.   
  18.     public int getNumber() {  
  19.         return number;  
  20.     }  
  21.   
  22.     public void setNumber(int number) {  
  23.         this.number = number;  
  24.     }  
  25.       
  26.     public String getFileName() {  
  27.         return fileName;  
  28.     }  
  29.   
  30.     public void setFileName(String fileName) {  
  31.         this.fileName = fileName;  
  32.     }  
  33.   
  34.     //返回一個輸入流,作爲一個客戶端來說是一個輸入流,但對於服務器端是一個 輸出流  
  35.     public InputStream getDownloadFile() throws Exception  
  36.     {  
  37.         if(1 == number)  
  38.         {  
  39.            this.fileName = “Dream.jpg” ;  
  40.            //獲取資源路徑  
  41.            return ServletActionContext.getServletContext().getResourceAsStream(“upload/Dream.jpg”) ;  
  42.         }  
  43.           
  44.         else if(2 == number)  
  45.         {  
  46.             this.fileName = “jd2chm源碼生成chm格式文檔.rar” ;  
  47.             //解解亂碼  
  48.             this.fileName = new String(this.fileName.getBytes(“GBK”),“ISO-8859-1”);  
  49.             return ServletActionContext.getServletContext().getResourceAsStream(“upload/jd2chm源碼生成chm格式文檔.rar”) ;  
  50.         }  
  51.         else  
  52.            return null ;  
  53.     }  
  54.       
  55.     @Override  
  56.     public String execute() throws Exception {  
  57.           
  58.         return SUCCESS;  
  59.     }  
  60.   
  61. }  
package com.struts2.filedownload;

import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

//文件下載
public class FileDownload extends ActionSupport{

private int number ;

private String fileName;

public int getNumber() {
    return number;
}

public void setNumber(int number) {
    this.number = number;
}

public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}

//返回一個輸入流,作爲一個客戶端來說是一個輸入流,但對於服務器端是一個 輸出流
public InputStream getDownloadFile() throws Exception
{
    if(1 == number)
    {
       this.fileName = "Dream.jpg" ;
       //獲取資源路徑
       return ServletActionContext.getServletContext().getResourceAsStream("upload/Dream.jpg") ;
    }

    else if(2 == number)
    {
        this.fileName = "jd2chm源碼生成chm格式文檔.rar" ;
        //解解亂碼
        this.fileName = new String(this.fileName.getBytes("GBK"),"ISO-8859-1");
        return ServletActionContext.getServletContext().getResourceAsStream("upload/jd2chm源碼生成chm格式文檔.rar") ;
    }
    else
       return null ;
}

@Override
public String execute() throws Exception {

    return SUCCESS;
}

}

二)在struts.xml文件中配置相關信息

  1. <struts>        
  2.    <package name=“struts2” extends=“struts-default”>        
  3.        <action name=“FileDownload” class=“com.struts2.filedownload.FileDownload”>  
  4.            <result name=“success” type=“stream”>  
  5.                <param name=“contentType”>text/plain</param>  
  6.                <param name=“contentDisposition”>attachment;fileName=“${fileName}”</param>  
  7.                <param name=“inputName”>downloadFile</param>  
  8.                <param name=“bufferSize”>1024</param>  
  9.            </result>  
  10.        </action>  
  11.      
  12.    </package>  
  13.      
  14. </struts>  
<struts> 
<package name=”struts2” extends=”struts-default”>
<action name=”FileDownload” class=”com.struts2.filedownload.FileDownload”>
<result name=”success” type=”stream”>
<param name=”contentType”>text/plain</param>
<param name=”contentDisposition”>attachment;fileName=”${fileName}”</param>
<param name=”inputName”>downloadFile</param>
<param name=”bufferSize”>1024</param>
</result>
</action>

</package>

</struts>

1.結果類型必須要寫成 type=”stream”  ,與之對應的處理類是 org.apache.struts2.dispatcher.StreamResult


2.涉及到的參數:


3.

1)  <param name=”contentDisposition”>attachment;fileName=”${fileName}”</param>

     contentDisposition默認是 inline(內聯的), 比如說下載的文件是文本類型的,就直接在網頁上打開,不能直接打開的纔會打開下載框自己選擇

2)  attachment :下載時會打開下載框

3)  fileName=”${fileName}” :在這定義的名字是一個動態的,該名字是顯示在下載框上的文件名字


4.<param name=”inputName”>downloadFile</param>,這個downloadFile名字要和FileDownload.java類中的getDownloadFile()方法名去掉get 一致


三)用於顯示下載的鏈接界面 filedownload.jsp

  1. <%@ page language=“java” import=“java.util. pageEncoding=“UTF-8”%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC ”-//W3C//DTD HTML 4.01 Transitional//EN”>  
  8. <html>  
  9.   <head>  
  10.     <base href=“<%=basePath%>”>  
  11.       
  12.     <title>My JSP ‘filedownload.jsp’ starting page</title>  
  13.       
  14.     <meta http-equiv=“pragma” content=“no-cache”>  
  15.     <meta http-equiv=“cache-control” content=“no-cache”>  
  16.     <meta http-equiv=“expires” content=“0”>      
  17.     <meta http-equiv=“keywords” content=“keyword1,keyword2,keyword3”>  
  18.     <meta http-equiv=“description” content=“This is my page”>  
  19.     <!– 
  20.     <link rel=”stylesheet” type=”text/css” href=”styles.css”> 
  21.     –>  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.     
  27.     <h2>文件下載內容:</h2><br/>  
  28.     Dream.jpg:<a href=“FileDownload.action?number=1”>點擊下載</a><br/>  
  29.     jd2chm源碼生成chm格式文檔.rar:<a href=“FileDownload.action?number=2”>點擊下載2</a>  
  30.       
  31.       
  32.   </body>  
  33. </html>  
<%@ page language=”java” import=”java.util.” pageEncoding=”UTF-8”%> 
<%
String path = request.getContextPath();
String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
%>

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<base href=”<%=basePath%>”>

&lt;title&gt;My JSP 'filedownload.jsp' starting page&lt;/title&gt;

&lt;meta http-equiv="pragma" content="no-cache"&gt;
&lt;meta http-equiv="cache-control" content="no-cache"&gt;
&lt;meta http-equiv="expires" content="0"&gt;    
&lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;
&lt;meta http-equiv="description" content="This is my page"&gt;
&lt;!--
&lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;
--&gt;

</head>

<body>

&lt;h2&gt;文件下載內容:&lt;/h2&gt;&lt;br/&gt;
Dream.jpg:&lt;a href="FileDownload.action?number=1"&gt;點擊下載&lt;/a&gt;&lt;br/&gt;
jd2chm源碼生成chm格式文檔.rar:&lt;a href="FileDownload.action?number=2"&gt;點擊下載2&lt;/a&gt;

</body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章