zoukankan      html  css  js  c++  java
  • Struts2笔记——文件上传

     文件上传概述

    * 要想使用HTML 表单上传一个或多个文件, 必须把 HTML 表单的 enctype 属性设置为multipart/form-data,把它的 method 属性设置为post
    * 为了让用户能够选择一个文件进行上传, 程序员必须提供一个 <input type=“file”> 字段. 

    ====================================

    Struts 对文件上传的支持

    * 在 Struts 应用程序里, FileUpload 拦截器和 Jakarta Commons FileUpload 组件可以完成文件的上传.
    * 步骤:
        > 1. 在 Jsp 页面的文件上传表单里使用 file 标签. 如果需要一次上传多个文件, 就必须使用多个 file 标签, 但它们的名字必须是相同的
        > 2. 在 Action中新添加 3 个和文件上传相关的属性. 这 3 个属性的名字必须是以下格式

     

        > uploadImage 是 jsp 页面上的 file 标签的名字.
            上传文件:<input type="file" name="uploadImage">
        > 如果是上传单个文件, uploadImage属性的类型就是 java.io.File, 它代表被上传的文件, 第二个和第三个属性的类型是 String
            它们分别代表上传文件的文件名和文件类型

            定义方式是分别是:   jsp页面file组件的名称+ContentType,

                                            jsp页面file组件的名称+FileName

        > 如果上上传多个文件, 可以使用数组或 List 

    ====================================

    单文件上传代码如下 

    图片
    图片

    ====================================

    File Upload 拦截器 

    图片 
     ====================================

    在jsp页面显示错误信息 

    在struts.xml文件中根据

        <result name=“input”>/upload/error.jsp</result>中所指向的error.jsp页面可以使用

        <s:fielderror/>显示错误信息

      ====================================
      
    查看struts-messages.properties文件 

    配置如下:

    struts.messages.error.uploading=Error uploading: {0}

    struts.messages.error.file.too.large=File too large: {0} "{1}""{2}" {3}

    struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0}"{1}" "{2}" {3}

    struts.messages.error.file.extension.not.allowed=File extension not allowed: {0}"{1}" "{2}" {3}

    {0}:<input type=“file” name=“uploadImage”>中name属性的值

    {1}:上传文件的名称

    {2}:上传文件保存到临时目录的名称

    {3}:上传文件的类型(对struts.messages.error.file.too.large是上传文件的大小) 

    图片

     ====================================
     
    修改显示错误的资源文件的信息 

    第一步:创建新的资源文件 例如fileuploadmessage.properties,放置在src下

               在该资源文件中增加如下信息

    struts.messages.error.uploading=上传错误:{0}

    struts.messages.error.file.too.large=上传文件太大:{0} "{1}" "{2}" {3}

    struts.messages.error.content.type.not.allowed=上传文件的类型不允许: {0} "{1}" "{2}" {3}

    struts.messages.error.file.extension.not.allowed=上传文件的后缀名不允许: {0} "{1}" "{2}" {3}

     


    第二步:在struts.xml文件加载该资源文件

          <!-- 设置comonfileupload上传组件允许的文件大小 才能测试出上传文件过大出现的错误信息 -->

          <constant name="struts.multipart.maxSize"value="86170804"/>

     

          <!-- 配置上传文件的出错信息的资源文件 -->

           <constant name="struts.custom.i18n.resources" value="fileuploadmessage“/>  


    ====================================
     

    多文件上传代码 

    图片 

  • 相关阅读:
    百度病了,必应挂了,Yandex疯了。
    SpringBoot从零单排 ------ 拦截器的使用
    SpringBoot从零单排 ------初级入门篇
    我为什么放弃MySQL?最终选择了MongoDB
    NSURL组成部分详解
    关于MPMoviePlayerController 缓存播放的一些技术准备
    动画系列收藏
    代码简化
    自动循环滚动ScrollView
    iOS WKWebView 使用笔记
  • 原文地址:https://www.cnblogs.com/pwc1996/p/4839157.html
Copyright © 2011-2022 走看看