struts2 文件下载

来源:互联网 发布:let it be 知乎 编辑:程序博客网 时间:2024/05/21 17:46

1.定义Action类(设定Web-inf/UploadImages下已经有3个图片)

package com.test.action;

import java.io.InputStream;

import java.io.UnsupportedEncodingException;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileDownAction extends ActionSupport {

 

 private String fileList[]=new String[]{

 "1333546631718.jpg",

 "张三.jpg",

 "1333546632125.jpg"

 };

 

 private String DownDir="UploadImages/";

 

  private String fileName;// 初始的通过param指定的文件名属性    

   

     public InputStream getInputStream() throws Exception {    

   

      String url=DownDir+getDownloadFileName();

      System.out.println("url:"+url);

         return ServletActionContext.getServletContext().getResourceAsStream(url);    

   

     }    

     

     

     /**

      * 下载访问的Action

      * @return

      * @throws Exception

      */

     public String DownAction() throws Exception {    

   

         return "downResult";    

   

     }   

     

     /**

      * 显示文件列表访问的Action

      * @return

      * @throws Exception

      */

     public String FileListAction() throws Exception {    

      

         return "fileListResult";    

   

     } 

   

     public void setFileName(String fileName) {    

   

         this.fileName = fileName;    

   

     }    

   

     /** 提供转换编码后的供下载用的文件名 */    

   

     public String getDownloadFileName() {    

   

         String downFileName = fileName;    

   

         try {    

   

             downFileName = new String(downFileName.getBytes(), "utf-8");    

   

         } catch (UnsupportedEncodingException e) {    

   

             e.printStackTrace();    

   

         }    

   

         return downFileName;    

   

     }

  public String[] getFileList() {

   return fileList;

  }

  public void setFileList(String[] fileList) {

   this.fileList = fileList;

  }

  public String getFileName() {

   return fileName;

  }    

}

2.定义struts.xml

<action name="downAction" class="com.test.action.FileDownAction">

          

   <result name="fileListResult">/FileUp/FileDown.jsp</result>

           <result name="downResult" type="stream">

     <!-- 指定只能下载pjpeg格式的图片 -->

              <param name="contentType">image/pjpeg</param>

   

     <!-- 对应Action中getInputStream()方法 -->

              <param name="inputName">inputStream</param>

              <!--动态获取文件名!-->

              <param name="contentDisposition">attachment;filename="${DownloadFileName}"</param>

              <param name="bufferSize">4096</param>

           </result>

       </action>

3.定义Jsp

<table width="70%" border="1">

   <caption>文件下载列表</caption>

   <tr><td>文件名称</td><td>下载链接</td></tr>

  

    <s:iterator value="fileList" status="sta">

    <tr>

    <td><s:property/></td>

    <td>

     <href="<s:url value='downAction!DownAction.action'>

     <s:param name='fileName'><s:property value='fileList[#sta.getIndex()]'/></s:param>

     </s:url>

     ">下载</a>

    </td>

    </tr>

    </s:iterator>

   </table>

4.在浏览器地址中访问:http://localhost:8989/Struts2/downAction!FileListAction.action,点击下载即可