博客
关于我
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/

你可能感兴趣的文章
Mysql join原理
查看>>
MySQL Join算法与调优白皮书(二)
查看>>
Mysql order by与limit混用陷阱
查看>>
Mysql order by与limit混用陷阱
查看>>
mysql order by多个字段排序
查看>>
MySQL Order By实现原理分析和Filesort优化
查看>>
mysql problems
查看>>
mysql replace first,MySQL中处理各种重复的一些方法
查看>>
MySQL replace函数替换字符串语句的用法(mysql字符串替换)
查看>>
mysql replace用法
查看>>
Mysql Row_Format 参数讲解
查看>>
mysql select, from ,join ,on ,where groupby,having ,order by limit的执行顺序和书写顺序
查看>>
MySQL Server 5.5安装记录
查看>>
mysql server has gone away
查看>>
mysql slave 停了_slave 停止。求解决方法
查看>>
MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
查看>>
MYSQL sql语句针对数据记录时间范围查询的效率对比
查看>>
mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
查看>>
mysql Timestamp时间隔了8小时
查看>>
Mysql tinyint(1)与tinyint(4)的区别
查看>>