博客
关于我
Struts2实现文件下载
阅读量:177 次
发布时间:2019-02-28

本文共 4076 字,大约阅读时间需要 13 分钟。

一 视图

1 loginForm.jsp

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %><%@ taglib prefix="s" uri="/struts-tags"%>    下载前的登录页面    

下载前的登录页面

${requestScope.tip}

2 struts2Down.jsp

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %><%@ taglib prefix="s" uri="/struts-tags"%>    Struts 2的文件下载    

Struts 2的文件下载

二 配置文件

/WEB-INF/images/疯狂联盟.jpg
image/jpg
targetFile
filename="wjc_logo.jpg"
4096
/WEB-INF/images/wjc_logo.zip
application/zip
targetFile
filename="wjc_logo.zip"
4096
/WEB-INF/content/loginForm.jsp
/WEB-INF/content/struts2Down.jsp
/WEB-INF/content/{1}.jsp

三 过滤器

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);    }}

四 测试

转载地址:http://svmj.baihongyu.com/

你可能感兴趣的文章
Netbeans 8.1启动参数配置
查看>>
NetBeans IDE8.0需要JDK1.7及以上版本
查看>>
NetBeans之JSP开发环境的搭建...
查看>>
NetBeans之改变难看的JSP脚本标签的背景色...
查看>>
netbeans生成的maven工程没有web.xml文件 如何新建
查看>>
netcat的端口转发功能的实现
查看>>
Netcraft报告: let's encrypt和Comodo发布成千上万的网络钓鱼证书
查看>>
Netem功能
查看>>
netfilter应用场景
查看>>
Netflix:当你按下“播放”的时候发生了什么?
查看>>
Netflix推荐系统:从评分预测到消费者法则
查看>>
netframework 4.0内置处理JSON对象
查看>>
Netgear WN604 downloadFile.php 信息泄露漏洞复现(CVE-2024-6646)
查看>>
Netgear wndr3700v2 路由器刷OpenWrt打造全能服务器(十一)备份
查看>>
netlink2.6.32内核实现源码
查看>>
netmiko 自动判断设备类型python_Python netmiko模块的使用
查看>>
NetMizer 日志管理系统 多处前台RCE漏洞复现
查看>>
NetMizer-日志管理系统 dologin.php SQL注入漏洞复现(XVE-2024-37672)
查看>>
Netpas:不一样的SD-WAN+ 保障网络通讯品质
查看>>
netron工具简单使用
查看>>