struts 2文件下载

1.首先项目里必须有struts2至少依赖的6个jar

2.先写一个下载的donwload.jsp页面

<%@ 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%>">
    
    <title>My JSP 'index.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><a href="login/download?fileName=1.txt">下载</a></h2>
  </body>
</html>

 

 2.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<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>

 3.建一个DownloadAction,当然,你的服务器也就是tomcat的工程下先建一个soft文件夹,然后存入1.txt的文件,文件名必须匹配

package com.hooy1.action;

import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


    public class DownloadAction extends ActionSupport{
        private String fileName;//接受页面传过来的文件名
        
  public String getFileName() {
  return fileName;
 }//对应struts配置中的${fileName}
public void setFileName(String fileName) {
this.fileName = fileName; } public InputStream getInputStream() { return ServletActionContext.getServletContext().getResourceAsStream("/soft/" + fileName);
//getInputStream()方法将返回一个InputStream,在struts.xml中,配置了一个参数inputStream,通过这个方法获得流对象。
}
public String execute(){
    
System.out.println(fileName);
return "success"; } }

 

 3.主要是struts.xml的配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="Login" extends="struts-default" namespace="/login">
        <action name="login_*" class="com.hooy1.action.LoginAction" method="{1}" >
            <result name="success">/index.jsp</result>
        </action>
          <action name="download" class="com.hooy1.action.DownloadAction">
           <result type="stream" name="success">
                                <param name="contentType">application/octet-stream</param>
                                <param name="inputName">inputStream</param>
                                <param name="contentDisposition">attachment;filename="${fileName}"</param>
                                <param name="bufferSize">4096</param>
           </result>
        </action>
    </package>
</struts>

contentType是内容类型,根据不同文件类型参照MIME标准设置,本例是文本。

 

posted @ 2012-11-12 15:12  小木v587  阅读(196)  评论(0)    收藏  举报