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

你可能感兴趣的文章
MQTT 持久会话与 Clean Session 详解
查看>>
MQTT工作笔记0007---剩余长度
查看>>
MQTT工作笔记0009---订阅主题和订阅确认
查看>>
Mqtt搭建代理服务器进行通信-浅析
查看>>
MS Edge浏览器“STATUS_INVALID_IMAGE_HASH“兼容性问题
查看>>
ms sql server 2008 sp2更新异常
查看>>
MS UC 2013-0-Prepare Tool
查看>>
MSBuild 教程(2)
查看>>
msbuild发布web应用程序
查看>>
MSB与LSB
查看>>
MSCRM调用外部JS文件
查看>>
MSCRM调用外部JS文件
查看>>
MSEdgeDriver (Chromium) 不适用于版本 >= 79.0.313 (Canary)
查看>>
MsEdgeTTS开源项目使用教程
查看>>
msf
查看>>
MSSQL数据库查询优化(一)
查看>>
MSSQL数据库迁移到Oracle(二)
查看>>
MSSQL日期格式转换函数(使用CONVERT)
查看>>
MSTP多生成树协议(第二课)
查看>>
MSTP是什么?有哪些专有名词?
查看>>