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

你可能感兴趣的文章
mysqldump实现数据备份及灾难恢复
查看>>
mysqldump数据库备份无法进行操作只能查询 --single-transaction
查看>>
mysqldump的一些用法
查看>>
mysqli
查看>>
MySQLIntegrityConstraintViolationException异常处理
查看>>
mysqlreport分析工具详解
查看>>
MySQLSyntaxErrorException: Unknown error 1146和SQLSyntaxErrorException: Unknown error 1146
查看>>
Mysql_Postgresql中_geometry数据操作_st_astext_GeomFromEWKT函数_在java中转换geometry的16进制数据---PostgreSQL工作笔记007
查看>>
mysql_real_connect 参数注意
查看>>
mysql_secure_installation初始化数据库报Access denied
查看>>
MySQL_西安11月销售昨日未上架的产品_20161212
查看>>
Mysql——深入浅出InnoDB底层原理
查看>>
MySQL“被动”性能优化汇总
查看>>
MySQL、HBase 和 Elasticsearch:特点与区别详解
查看>>
MySQL、Redis高频面试题汇总
查看>>
MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
查看>>
mysql一个字段为空时使用另一个字段排序
查看>>
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>
MYSQL一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>