吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:struts2实现文件下载的Action

只谈情不闲聊 提交于 2020-02-26 14:54:07
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>原始的下载</title>
    <meta name="author" content="Yeeku.H.Lee" />
    <meta name="website" content="http://www.crazyit.org" />
    <meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<body>
<h1>原始的下载</h1>
<ul>
<li>
    下载疯狂Java联盟的Logo:
    <a href="/WEB-INF/images/疯狂联盟.jpg">下载图形文件</a> 
</li>
<li>
    下载疯狂Java联盟的Logo的压缩文件:
    <a href="/WEB-INF/images/wjc_logo.zip">下载压缩文件</a>
</li>
</ul>
</body>
</html>
<?xml version="1.0" encoding="GBK"?>
<project name="struts" basedir="." default="">
    <property name="dist" value="classes"/>
    <property name="src" value="src"/>
    
    <path id="classpath">
        <fileset dir="lib">
            <include name="*.jar"/>
        </fileset>
        <pathelement path="${dist}"/>
    </path>

    <target name="compile" description="Compile all source code">
        <delete dir="${dist}"/>
        <mkdir dir="${dist}"/>
        <copy todir="${dist}">
            <fileset dir="${src}">
                <exclude name="**/*.java"/>
            </fileset>        
        </copy>
        <javac destdir="classes" debug="true" includeantruntime="yes"
            deprecation="false" optimize="false" failonerror="true">
            <src path="${src}"/>
            <classpath refid="classpath"/>
        </javac>
    </target>

</project>
<?xml version="1.0" encoding="GBK"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
    <!-- 定义Struts 2的核心Filter -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <!-- 让Struts 2的核心Filter拦截所有请求 -->
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2016, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date: 
--%>

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>下载前的登录页面</title>
</head>
<body>
    <h3>下载前的登录页面</h3>
    <span style="color:red">${requestScope.tip}</span>
    <s:form action="login">
        <s:textfield name="user" label="用户名"/>
        <s:textfield name="pass" label="密码"/>
        <s:submit value="登录"/>
    </s:form>
</body>
</html>
<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2016, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date: 
--%>

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Struts 2的文件下载</title>
</head>
<body>
    <h1>Struts 2的文件下载</h1>
    <ul>
    <li>
    下载疯狂Java联盟的Logo:
        <a href="download.action">下载图形文件</a> 
    </li>
    <li>
    下载疯狂Java联盟的Logo的压缩文件:
        <a href="download2.action">下载压缩文件</a>
    </li>
    </ul>
</body>
</html>
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <!-- 配置Struts 2应用的字符集 -->
    <constant name="struts.i18n.encoding" value="GBK"/>
    <package name="lee" extends="struts-default">
        <action name="download" class="org.crazyit.app.action.FileDownloadAction">
        <!-- 指定被下载资源的位置 -->
            <param name="inputPath">/WEB-INF/images/疯狂联盟.jpg</param>
            <!-- 配置结果类型为stream的结果 -->
            <result type="stream">
                <!-- 指定下载文件的文件类型 -->
                <param name="contentType">image/jpg</param>
                <!-- 指定由getTargetFile()方法返回被下载文件的InputStream -->
                <param name="inputName">targetFile</param>
                <param name="contentDisposition">filename="wjc_logo.jpg"</param>
                <!-- 指定下载文件的缓冲大小 -->
                <param name="bufferSize">4096</param>
            </result>
        </action>

        <action name="download2" class="org.crazyit.app.action.AuthorityDownAction">
            <!-- 定义被下载文件的物理资源 -->
            <param name="inputPath">/WEB-INF/images/wjc_logo.zip</param>
            <result type="stream">
                <!-- 指定下载文件的文件类型 -->
                <param name="contentType">application/zip</param>
                <!-- 指定由getTargetFile()方法返回被下载文件的InputStream -->
                <param name="inputName">targetFile</param>
                <param name="contentDisposition">filename="wjc_logo.zip"</param>
                <!-- 指定下载文件的缓冲大小 -->
                <param name="bufferSize">4096</param>
            </result>
            <!-- 定义一个名为login的结果 -->
             <result name="login">/WEB-INF/content/loginForm.jsp</result>
        </action>

        <action name="login" class="org.crazyit.app.action.LoginAction">
            <result>/WEB-INF/content/struts2Down.jsp</result>
        </action>

        <action name="*">
            <result>/WEB-INF/content/{1}.jsp</result>    
        </action>

    </package>
</struts>
package org.crazyit.app.action;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.*;

import java.util.Map;
import java.io.InputStream;

/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
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;
    }
}
package org.crazyit.app.action;

import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.*;

/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
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);
    }
}
package org.crazyit.app.action;

import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
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;
    }
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!