Struts2实现文件下载

一 视图

1 loginForm.jsp

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>下载前的登录页面</title>
</head>
<body>
    <h3>下载前的登录页面</h3>
    <span style="color:red">${requestScope.tip}</span>
    <s:form action="login">
        <s:textfield name="user" label="用户名"/>
        <s:textfield name="pass" label="密码"/>
        <s:submit value="登录"/>
    </s:form>
</body>
</html>

2 struts2Down.jsp

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Struts 2的文件下载</title>
</head>
<body>
    <h1>Struts 2的文件下载</h1>
    <ul>
    <li>
    下载疯狂Java联盟的Logo:
        <a href="download.action">下载图形文件</a>
    </li>
    <li>
    下载疯狂Java联盟的Logo的压缩文件:
        <a href="download2.action">下载压缩文件</a>
    </li>
    </ul>
</body>
</html>

二 配置文件

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <!-- 配置Struts 2应用的字符集 -->
    <constant name="struts.i18n.encoding" value="GBK"/>
    <package name="lee" extends="struts-default">
        <action name="download" class="org.crazyit.app.action.FileDownloadAction">
        <!-- 指定被下载资源的位置 -->
            <param name="inputPath">/WEB-INF/images/疯狂联盟.jpg</param>
            <!-- 配置结果类型为stream的结果 -->
            <result type="stream">
                <!-- 指定下载文件的文件类型 -->
                <param name="contentType">image/jpg</param>
                <!-- 指定由getTargetFile()方法返回被下载文件的InputStream -->
                <param name="inputName">targetFile</param>
                <param name="contentDisposition">filename="wjc_logo.jpg"</param>
                <!-- 指定下载文件的缓冲大小 -->
                <param name="bufferSize">4096</param>
            </result>
        </action>

        <action name="download2" class="org.crazyit.app.action.AuthorityDownAction">
            <!-- 定义被下载文件的物理资源 -->
            <param name="inputPath">/WEB-INF/images/wjc_logo.zip</param>
            <result type="stream">
                <!-- 指定下载文件的文件类型 -->
                <param name="contentType">application/zip</param>
                <!-- 指定由getTargetFile()方法返回被下载文件的InputStream -->
                <param name="inputName">targetFile</param>
                <param name="contentDisposition">filename="wjc_logo.zip"</param>
                <!-- 指定下载文件的缓冲大小 -->
                <param name="bufferSize">4096</param>
            </result>
            <!-- 定义一个名为login的结果 -->
             <result name="login">/WEB-INF/content/loginForm.jsp</result>
        </action>

        <action name="login" class="org.crazyit.app.action.LoginAction">
            <result>/WEB-INF/content/struts2Down.jsp</result>
        </action>

        <action name="*">
            <result>/WEB-INF/content/{1}.jsp</result>    
        </action>

    </package>
</struts>

三 过滤器

1 LoginAction

package org.crazyit.app.action;

import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;


public class LoginAction implements Action
{
    private String user;
    private String pass;

    // user的setter和getter方法
    public void setUser(String user)
    {
        this.user = user;
    }
    public String getUser()
    {
        return this.user;
    }

    // pass的setter和getter方法
    public void setPass(String pass)
    {
        this.pass = pass;
    }
    public String getPass()
    {
        return this.pass;
    }

    public String execute()
    {
        ActionContext.getContext().getSession()
            .put("user" , getUser());
        return SUCCESS;
    }
}

2 AuthorityDownAction

package org.crazyit.app.action;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.*;

import java.util.Map;
import java.io.InputStream;

public class AuthorityDownAction
    implements Action
{
    private String inputPath;
    public void setInputPath(String value)
    {
        inputPath = value;
    }

    public InputStream getTargetFile() throws Exception
    {
        // ServletContext提供getResourceAsStream()方法
        // 返回指定文件对应的输入流
        return ServletActionContext.getServletContext()
            .getResourceAsStream(inputPath);
    }

    public String execute() throws Exception
    {
        // 取得ActionContext实例
        ActionContext ctx = ActionContext.getContext();
        // 通过ActionContext访问用户的HttpSession
        Map session = ctx.getSession();
        String user = (String)session.get("user");
        // 判断Session里的user是否通过检查
        if ( user !=  null && user.equals("crazyit.org"))
        {
            return SUCCESS;
        }
        ctx.put("tip", "您还没有登录,或者登录的用户名不正确,请重新登录!");
        return LOGIN;
    }
}

3 FileDownloadAction

package org.crazyit.app.action;

import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.*;

public class FileDownloadAction
    extends ActionSupport
{
    // 该成员变量可以在配置文件中动态指定该值
    private String inputPath;
    // inputPath的setter方法
    public void setInputPath(String value)
    {
        inputPath = value;
    }
    /*
    定义一个返回InputStream的方法,
    该方法将作为被下载文件的入口,
    且需要配置stream类型结果时指定inputName参数,
    inputName参数的值就是方法去掉get前缀、首字母小写的字符串
    */
    public InputStream getTargetFile() throws Exception
    {
        // ServletContext提供getResourceAsStream()方法
        // 返回指定文件对应的输入流
        return ServletActionContext.getServletContext()
            .getResourceAsStream(inputPath);
    }
}

四 测试

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值