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

分享

[置頂] Struts2之文件下載

 心殤無痕 2014-10-31
原文地址:http://blog.csdn.net/hzc543806053/article/details/7538723




文件上傳鏈接:

1)Servlet 文件上傳 ————  點(diǎn)擊打開鏈接

2)Struts2 文件上傳 ———— 點(diǎn)擊打開鏈接



文件下載是一個(gè)很常見的功能,用struts2實(shí)現(xiàn)文件下載的步驟:


一)定義一個(gè)Action類,F(xiàn)ileDownload.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.     //返回一個(gè)輸入流,作為一個(gè)客戶端來說是一個(gè)輸入流,但對(duì)于服務(wù)器端是一個(gè) 輸出流  
  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. }  

二)在struts.xml文件中配置相關(guān)信息

  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>  

1.結(jié)果類型必須要寫成 type="stream"  ,與之對(duì)應(yīng)的處理類是 org.apache.struts2.dispatcher.StreamResult


2.涉及到的參數(shù):


3.

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

     contentDisposition默認(rèn)是 inline(內(nèi)聯(lián)的), 比如說下載的文件是文本類型的,就直接在網(wǎng)頁上打開,不能直接打開的才會(huì)打開下載框自己選擇

2)  attachment :下載時(shí)會(huì)打開下載框

3)  fileName="${fileName}" :在這定義的名字是一個(gè)動(dòng)態(tài)的,該名字是顯示在下載框上的文件名字


4.<param name="inputName">downloadFile</param>,這個(gè)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>文件下載內(nèi)容:</h2><br/>  
  28.     Dream.jpg:<a href="FileDownload.action?number=1">點(diǎn)擊下載</a><br/>  
  29.     jd2chm源碼生成chm格式文檔.rar:<a href="FileDownload.action?number=2">點(diǎn)擊下載2</a>  
  30.       
  31.       
  32.   </body>  
  33. </html>  



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

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多