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

你可能感兴趣的文章
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>
MYSQL:基础——3N范式的表结构设计
查看>>
MYSQL:基础——触发器
查看>>
Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 参数--lock-tables浅析
查看>>
mysqldump 导出中文乱码
查看>>
mysqldump 导出数据库中每张表的前n条
查看>>
mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
查看>>
Mysqldump参数大全(参数来源于mysql5.5.19源码)
查看>>
mysqldump备份时忽略某些表
查看>>
mysqldump实现数据备份及灾难恢复
查看>>
mysqldump数据库备份无法进行操作只能查询 --single-transaction
查看>>
mysqldump的一些用法
查看>>