Struts2 文件下载

1.  Struts 2文件下载目录结构

2.  下载Action类

package com.zhanggaosong.action;

import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport {

// 1.下载的目标资源 2.下载的文件类型 3.下载的文件名

private String res;// 下载的资源
private String resType; // 下载的文件类型
private String resName;// 下载的文件名

public String getRes() {
return res;
}

public void setRes(String res) throws Exception {
this.res = new String(res.getBytes("ISO-8859-1"),"UTF-8");
}

public String getResType() {
return resType;
}

public void setResType(String resType) {
this.resType = resType;
}

public String getResName() {
return resName;
}

public void setResName(String resName) {
this.resName = resName;
}

/**
* 该方法代表了文件下载的入口,它就是我们要让客户下载的文件的输入流
*/
public InputStream getTarget() throws Exception {

// 返回res资源所对应的输入流
return new FileInputStream(ServletActionContext.getServletContext()
.getRealPath("/") + res);
}
}

3. struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />

<package name="default" namespace="/" extends="struts-default">

<action name="downloadAction" class="com.zhanggaosong.action.DownloadAction">
<result type="stream">
<!--该参数指定二进制流的类型 -->
<param name="contentType">${resType}</param>
<!-- 指定返回InputStream的方法 -->
<param name="inputName">target</param>

<param name="contentDisposition">filename=${resName}</param>
<!--控制文件下载时缓冲的大小 -->
<param name="bufferSize">4096</param>
</result>
</action>
</package>

<include file="example.xml"/>

<!-- Add packages here -->

</struts>

4.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

5.  下载页面 

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>下载页面</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/downloadAction?res=第三章 流程控制语句和数组.ppt&resType=application/vnd.ms-powerpoint&resName=stru.ppt">下载Struts PPT</a>
</body>
</html>

 

posted @ 2013-03-12 14:00  zhgs_cq  阅读(183)  评论(0编辑  收藏  举报