• java:struts框架3(自定义拦截器,token令牌,文件上传和下载(单/多))


    1.自定义拦截器:

      

      struts.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
        <package name="inteceptor" extends="struts-default">
        
            <interceptors>
                <!-- 配置自定义拦截器 -->
                <interceptor name="myInter" class="cn.zzsxt.interceptor.MyInterceptor"></interceptor>
                <interceptor name="permission" class="cn.zzsxt.interceptor.PermissionInterceptor"></interceptor>
        
                <!-- 自定义拦截器栈 -->        
                <interceptor-stack name="myStack">
                    <!-- 引入自定义拦截器 -->
                    <interceptor-ref name="myInter"></interceptor-ref>
                    <!-- 引入timer拦截器 -->
                    <interceptor-ref name="timer"></interceptor-ref>
                    <!-- 引入默认拦截器栈 -->
                    <interceptor-ref name="defaultStack"></interceptor-ref>
                </interceptor-stack>
            
                <!-- 自定义访问权限的拦截器栈 -->            
                <interceptor-stack name="permissionStack">
                    <interceptor-ref name="permission"></interceptor-ref>
                    <interceptor-ref name="defaultStack"></interceptor-ref>
                </interceptor-stack>
            </interceptors>
            
            
            <action name="timerAction" class="cn.zzsxt.action.TimerAction">
                <result name="success">/success.jsp</result>
                <interceptor-ref name="myStack"></interceptor-ref>
                <interceptor-ref name="permissionStack"></interceptor-ref>
            </action>
            <action name="userAction" class="cn.zzsxt.action.UserAction">
                <result name="success" type="redirectAction">homeAction</result>
                <result name="login">/login.jsp</result>
                <interceptor-ref name="permissionStack"></interceptor-ref>
            </action>
            <action name="homeAction" class="cn.zzsxt.action.HomeAction">
                <result name="success">/list.jsp</result>
                <result name="login" type="redirect">/login.jsp</result>
                <!-- 引入拦截器栈 -->
                <interceptor-ref name="permissionStack"></interceptor-ref>
            </action>
        </package>
    </struts>

    2.token令牌:

      struts.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
        
        <!-- 上传文件最大大小,默认为2097152字节(2M) -->
        <constant name="struts.multipart.maxSize" value="209715200"></constant>
        
        <!-- 设置编码集 request.setCharacterEncoding("UTF-8")-->
        <constant name="struts.i18n.encoding" value="UTF-8"></constant>
        
        
        <package name="tokenDemo" extends="struts-default">
            <action name="user-*" class="cn.zzsxt.action.UserAction" method="{1}">
                <result name="success">/index.jsp</result>
                <!-- token验证失败后跳转的结果视图 -->
                <result name="invalid.token">/error.jsp</result>
                <!-- 引入token拦截器 -->
                <interceptor-ref name="token"></interceptor-ref>
                <!-- 引入默认拦截器栈 -->
                <interceptor-ref name="defaultStack"></interceptor-ref>
            </action>
        </package>
    </struts>

      add.jsp:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ taglib uri="/struts-tags" prefix="s"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        <title>My JSP 'index.jsp' starting page</title>
      </head>
      
      <body>
          <form action="user-doAdd.action" method="post">
              <!-- token令牌 -->
              <s:token></s:token>
              用户编号:<input type="text" name="user.id"><br>
              用户名称:<input type="text" name="user.userName"><br>
              密码:<input type="text" name="user.password"><br>
              <input type="submit" value="新增"/>
          </form>
      </body>
    </html>

      action:

    package cn.zzsxt.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    import cn.zzsxt.entity.User;
    
    public class UserAction extends ActionSupport{
        private User user;
    
        public User getUser() {
            return user;
        }
    
        public void setUser(User user) {
            this.user = user;
        }
        
        public String doAdd() throws Exception {
            System.out.println("执行了用户新增,新增了:"+user);
            return this.SUCCESS;
        }
    }

      

    3.文件上传和下载(单/多):

      struts.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
        
        <!-- 上传文件最大大小,默认为2097152字节(2M) -->
        <constant name="struts.multipart.maxSize" value="209715200"></constant>
        <!-- 设置编码集 request.setCharacterEncoding("UTF-8")-->
        <constant name="struts.i18n.encoding" value="UTF-8"></constant>
        
        
        <package name="upload" extends="struts-default">
            <action name="singleUpload" class="cn.zzsxt.action.SingleUploadAction">
                <!-- result中name属性缺省(默认)为success -->
                <result>/success.jsp</result>
            </action>
            <action name="mutilUpload" class="cn.zzsxt.action.MutilUploadAction">
                <!-- result中name属性缺省(默认)为success -->
                <result>/success.jsp</result>
            </action>
            
            <action name="download" class="cn.zzsxt.action.DownloadAction">
                <result type="stream">
                    <!-- inputName="输入流的名称",回掉action中getInputStream()方法获取输入流 -->
                    <param name="inputName">inputStream</param>
                    <!--contentType="文件类型" application/octet-stream代表对下载类型不限制  -->
                    <param name="contentType">application/octet-stream</param>
                    <!-- attachment:以附件方式保存 filename="保存的文件名称" ${fileName}获取action中fileName属性的值 -->
                    <param name="contentDisposition">attachment;filename=${fileName}</param>
                </result>
            </action>
        </package>
    </struts>

      SingleUploadAction:

    package cn.zzsxt.action;
    
    import java.io.File;
    
    import javax.servlet.ServletContext;
    
    import org.apache.commons.io.FileUtils;
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class SingleUploadAction extends ActionSupport{
        private File upload;//文件
        private String uploadFileName;//文件名称:属性名+FileName
        private String uploadContentType;//文件类型:属性名+ContentType
        
        public File getUpload() {
            return upload;
        }
    
        public void setUpload(File upload) {
            this.upload = upload;
        }
    
        public String getUploadFileName() {
            return uploadFileName;
        }
    
        public void setUploadFileName(String uploadFileName) {
            this.uploadFileName = uploadFileName;
        }
    
        public String getUploadContentType() {
            return uploadContentType;
        }
    
        public void setUploadContentType(String uploadContentType) {
            this.uploadContentType = uploadContentType;
        }
    
        /**
         * 执行文件上次
         */
        @Override
        public String execute() throws Exception {
            System.out.println("文件名称:"+uploadFileName);
            System.out.println("文件类型:"+uploadContentType);
            ServletContext servletContext = ServletActionContext.getServletContext();
            String realPath = servletContext.getRealPath("/upload");//获取文件上次的真实目录
            File uploadDir = new File(realPath);
            if(!uploadDir.exists()){
                uploadDir.mkdirs();//创建目录
            }
            //上传
            FileUtils.copyFile(upload, new File(uploadDir,uploadFileName));
            return this.SUCCESS;
        }
    }

      SingleUpload.jsp:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        <title>单文件上传</title>
      </head>
      
      <body>
           <form action="singleUpload.action" method="post" enctype="multipart/form-data">
               文件:<input type="file" name="upload"></br>
               <input type="submit" value="上传"/>
           </form>
      </body>
    </html>

      MutilUploadAction:

    package cn.zzsxt.action;
    
    import java.io.File;
    
    import javax.servlet.ServletContext;
    
    import org.apache.commons.io.FileUtils;
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class MutilUploadAction extends ActionSupport{
        private File[] upload;//文件
        private String[] uploadFileName;//文件名称:属性名+FileName
        private String[] uploadContentType;//文件类型:属性名+ContentType
        public File[] getUpload() {
            return upload;
        }
        public void setUpload(File[] upload) {
            this.upload = upload;
        }
        public String[] getUploadFileName() {
            return uploadFileName;
        }
        public void setUploadFileName(String[] uploadFileName) {
            this.uploadFileName = uploadFileName;
        }
        public String[] getUploadContentType() {
            return uploadContentType;
        }
        public void setUploadContentType(String[] uploadContentType) {
            this.uploadContentType = uploadContentType;
        }
    
    
        /**
         * 执行文件上次
         */
        @Override
        public String execute() throws Exception {
            System.out.println("文件名称:"+uploadFileName);
            System.out.println("文件类型:"+uploadContentType);
            ServletContext servletContext = ServletActionContext.getServletContext();
            String realPath = servletContext.getRealPath("/upload");//获取文件上次的真实目录
            File uploadDir = new File(realPath);
            if(!uploadDir.exists()){
                uploadDir.mkdirs();//创建目录
            }
            //遍历待上传的文件
            for(int i=0;i<upload.length;i++){
                //上传
                FileUtils.copyFile(upload[i], new File(uploadDir,uploadFileName[i]));
            }
            return this.SUCCESS;
        }
    }

      MutilUpload.jsp:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        <title>单文件上传</title>
      </head>
      
      <body>
           <form action="mutilUpload.action" method="post" enctype="multipart/form-data">
               文件1:<input type="file" name="upload"></br>
               文件2:<input type="file" name="upload"></br>
               文件3:<input type="file" name="upload"></br>
               <input type="submit" value="上传"/>
           </form>
      </body>
    </html>

      DownloadAction:

    package cn.zzsxt.action;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    
    import javax.servlet.ServletContext;
    
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class DownloadAction extends ActionSupport{
        private String fileName;//接受要下载的文件名称
        private InputStream inputStream;
        
        public String getFileName() {
            return fileName;
        }
    
        public void setFileName(String fileName) {
            this.fileName = fileName;
        }
        
    
        public InputStream getInputStream() {
            return inputStream;
        }
    
        public void setInputStream(InputStream inputStream) {
            this.inputStream = inputStream;
        }
    
        /**
         * 文件下载
         */
        @Override
        public String execute() throws Exception {
            //获取服务器端保存文件的目录
            ServletContext servletContext = ServletActionContext.getServletContext();
            String realPath = servletContext.getRealPath("/upload");//获取文件上传的真实目录
            File file = new File(realPath,fileName);
            inputStream = new FileInputStream(file); //将文件转为输入流
            //解决下载时中文名称乱码问题
            String str = new String(fileName.getBytes("UTF-8"),"iso-8859-1");
            fileName = str;
            return this.SUCCESS;
        }
    }

      Download.jsp:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        <title>文件下载</title>
      </head>
      
      <body>
          <a href="download.action?fileName=1.txt">1.txt</a><br>
          <a href="download.action?fileName=mystruts.png">mystruts.png</a><br>
          <a href="download.action?fileName=Log4J详解.docx">Log4J详解.docx</a><br>
      </body>
    </html>
  • 相关阅读:
    语文文法
    1223 递归下降语法分析程序设计
    有穷的自动机构造
    文法分析
    text
    0916 词法分析(3)
    "firstday"-软件工程
    实验四 递归下降语法分析程序设计
    构造该正规式的有穷状态自动机
    评论
  • 原文地址:https://www.cnblogs.com/kuangzhisen/p/7354748.html
Copyright © 2020-2023  润新知