1. 程式人生 > >Struts2實現下載檔案

Struts2實現下載檔案

Struts2框架和檔案下載有關的類是org.apache.struts2.dispatcher.StreamResult,這個類的一些成員變數和下載有關,看原始碼

   //下載檔案的型別
   protected String contentType = "text/plain";
   //下載檔案的長度
    protected String contentLength;
    //
    protected String contentDisposition = "inline";
    //下載檔案的文字編碼
    protected String contentCharSet ;
    //指定檔案輸入流的名稱,通常是InputStream的子類
protected String inputName = "inputStream"; protected InputStream inputStream; //檔案快取大小 protected int bufferSize = 1024; //是否允許快取 protected boolean allowCaching = true;

DownLoadAction.java

package com.struts2.download;

import java.io.IOException;
import java.io.InputStream;

import
org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class DownLoadAction extends ActionSupport { private int number; private String fileName; public void setNumber(int number) { this.number = number; } public int getNumber() { return
number; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public InputStream getDownLoadFile() throws IOException { 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"); } return null; } @Override public String execute() throws Exception { return SUCCESS; } }

filedownload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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%>">

    <title>My JSP 'filedownload.jsp' starting page</title>

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

  </head>

  <body>

<h2>下載內容</h2><br/>
 Dream.jsp <a href="${pageContext.request.contextPath}/DownLoadAction.action?number=1">點選下載</a><br/>
  jd2chm原始碼生成chm格式文件.rar<a href="${pageContext.request.contextPath}/DownLoadAction.action?number=2">點選下載</a><br/>

  </body>
</html>
</package>

    <package name="filedownload" extends="struts-default">
    <action name="DownLoadAction" class="com.struts2.download.DownLoadAction">
    <result name="success" type="stream">
    <param name="contentType">image/jpeg</param><!-- 指定下載檔案型別 -->
    <param name="contentDisposition">attacthment;filename="${fileName}"</param><!-- filename="${fileName}"指定下載檔名 -->
    <param name="inputName">DownLoadFile</param>
    <param name="bufferSize">1024</param><!--指定下載檔案快取的大小  -->

    </result>
    </action>
    </package>

注意 DownLoadFile中DownLoadFile是DownLoadAction.java的getDownLoadFile方法去掉get的名稱,這是約定的。