zoukankan      html  css  js  c++  java
  • struts2文件上传、下载、防止重复提交

    Struts2 文件上传
    Struts2 文件上传基于 Struts2 拦截器实现; Struts2 文件上传使用的是 fileupload 组件; Form 配置 enctype="multipart/form-data"; Struts2 获取上传文件:name (name 是文件表单的 name) Struts2 获取上传文件名:name+FileName; Struts2 获取上传文件的类型:name+ContentType;
    配置文件的大小及类型
    <paramname="allowedTypes">image/bmp,image/x-png,image/gif,image/jpg,image/jpeg</param> <paramname="maximumSize">81101</param>
    <s:fielderror></s:fielderror>
    大文件上传
    Struts2 文件上传大小默认是 2M;
    <constantname="struts.multipart.maxSize"value="20000000"></constant>
    多文件上传时,对应的属性变成数组,循环上传。

    Struts2文件下载
    返回的是文件流
    <result type="stream">
            <param name="contentDisposition">attachment;filename=${fileName}</param>
    </result>
    return InputStreamgetInputStream();
    示例:
        private String fileName;

        public String getFileName() throws Exception{
            fileName=new String(fileName.getBytes(),"ISO8859-1");
            return fileName;
        }

        public void setFileName(String fileName) {
            this.fileName = fileName;
        }
        
        public InputStream getInputStream()throws Exception{
            File file=new File("C:/例1.jpg");
            this.fileName="例1";
            return new FileInputStream(file);
        }

    使用<s:token/>标签防重复提交
    <s:token></s:token> :加在 form 里; 使用 token 拦截器:
             <interceptor-ref name="token"></interceptor-ref>
             <interceptor-ref name="defaultStack"></interceptor-ref>
             <result name="invalid.token">/student.jsp</result>
    在 struts.xml 里配置,假如出现重复提 交,则直接回到页面;
    <s:actionerror/>:在页面上显示错误信息;
    使用 tokenSession 拦截器防重复提交
    tokenSesssion 拦截器直接无视重复提交的请求;
        <interceptor-refname="tokenSession"></interceptor-ref>
    <interceptor-refname="defaultStack"></interceptor-ref>

  • 相关阅读:
    一分钟了解Docker
    RobotFramework
    RobotFramework不同版本优劣势
    从零学习基于Python的RobotFramework自动化
    Python 接口自动化常用方法封装
    (转载)解决MySql 数据库 提示:1045 access denied for user 'root'@'localhost' using password yes
    (转载)html中div使用自动高度
    javascriptDOM对象之scrollTo()方法,滚动到页面指定位置
    CSS3之响应式布局
    Html5NodeJs安装less之千辛万苦CMD系列
  • 原文地址:https://www.cnblogs.com/cyf18/p/14289452.html
Copyright © 2011-2022 走看看