Struts2文件上传

文件上传概述

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

Struts 对文件上传的支持

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

private FileuploadImage; //上传的文件

private StringuploadImageContentType//上传的文件的类型

private String uploadImageFileName;    //上传文件的名称

uploadImage jsp 页面上的 file 标签的名字 .

          上传文件:<input type="file"name="uploadImage">

如果是上传单个文件 , uploadImage 属性的类型就是 java.io.File , 它代表被上传的文件 , 第二个和第三个属性的类型是 String, 它们分别代表上传文件的文件名和文件类型

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

                                           jsp页面file组件的名称+FileName

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




单文件上传代码如下




定义上传出错要转向的页面

第四步:在struts.xml文件中增加如下配置

 <package name="upload"namespace="/upload" extends="struts-default" >

          <!-- 单文件上传-->

        <action name="uploadAction_*" class="cn.itcast.upload.UploadAction" method="{1}">

           <resultname="success">/upload/success.jsp</result>

  

           <!-- 定义上传出错要转向的页面 -->

           <resultname="input">/upload/error.jsp</result>

       </action>

 <package>


设置上传文件的总开关

	<!-- 配置文件上传的总大小 -->
	<constant name="struts.multipart.maxSize" value="2097152000"></constant>


File Upload 拦截器



<!-- 配置拦截器的参数,这里是文件上传拦截器 -->
			<interceptor-ref name="defaultStack">
              	<!-- 
              		配置文件上传拦截器的参数
              			* 与定义参数的顺序无关
              			* 允许的类型(allowedTypes)和允许的扩展名(allowedExtensions)必须保持一致
              	 -->
              	<!-- 
              		* 配置上传文件的大小
              			* struts.xml文件中配置的是上传文件的总大小
              			* 这里配置的是上传文件的单个大小
              	 -->
              	<param name="fileUpload.maximumSize">20971520</param>
              	<!-- 配置上传文件允许的类型,如果配置多个值的话,用","隔开 -->
              	<param name="fileUpload.allowedTypes">text/plain,application/msword</param>
              	<!-- 配置上传文件的扩展名,如果配置多个值的话,用","隔开 -->
              	<param name="fileUpload.allowedExtensions">.txt</param>
            </interceptor-ref>


在jsp页面显示错误信息

struts.xml文件中根据

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

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

 

查看struts-messages.properties文件

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

第一步:创建新的资源文件例如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文件加载该资源文件

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

       <constantname="struts.custom.i18n.resources" value=“cn….xxx.fileuploadmessage“/>

多文件上传代码

第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jarcommons-io-1.3.2.jar。这两个文件可以从http://commons.apache.org/下载。

第二步:把form表的enctype设置为:“multipart/form-data“,如下:

<formenctype="multipart/form-data"action="${pageContext.request.contextPath}/xxx.action" method="post">

  <input type="file" name="uploadImages">

  <input type="file" name="uploadImages">

</form>

第三步:在Action类中添加以下属性,属性红色部分对应于表单中文件字段的名称

publicclass uploadAction{

  private File[] uploadImages;//得到上传的文件

  privateString[] uploadImagesContentType;//得到文件的类型

  privateString[] uploadImagesFileName;//得到文件的名称

  //这里略省了属性的getter/setter方法

  publicString saveFiles() throws Exception{

  String realpath =ServletActionContext.getServletContext().getRealPath("/images");

  File file = new File(realpath);

  if(!file.exists())file.mkdirs();

  for(inti=0 ;i<uploadImages.length;i++){ File uploadImage = uploadImages[i];

   FileUtils.copyFile(uploadImage, new File(file,uploadImagesFileName[i]));

}

  return "success";

  }}


总结

struts2框架的文件上传:
    * 单文件上传:
        * 在动作类action中声明相关属性:
            * 在动作类action中,要声明与页面中表单name属性同名的属性,同名的属性的类型时File类型;
            * 在动作类action中,要声明[同名的属性]ContentType,类型时String类型;
            * 在动作类action中,要声明[同名的属性]FileName,类型时String类型
            * 给所有属性提供get和set方法
        * 在业务方法中,处理文件上传:
            * 获取要上传文件的路径,保存的位置
            * 在目标文件夹内,创建一个与上传文件同名的文件
            * 通过FileUtils工具类提供copyFile()方法,将临时文件内容拷贝到目标文件夹下的那个同名的文件
        * 设置上传文件的总大小
            * 在struts.xml文件中,<constant name="struts.multipart.maxSize" value="2097152000"></constant>
        * 设置上传文件的大小、类型和扩展名:
            * 在自定义的配置文件中,在action标签下:
                <!-- 配置拦截器的参数,这里是文件上传拦截器 -->
                <interceptor-ref name="defaultStack">
                      <!--
                          配置文件上传拦截器的参数
                              * 与定义参数的顺序无关
                              * 允许的类型(allowedTypes)和允许的扩展名(allowedExtensions)必须保持一致
                       -->
                      <!--
                          * 配置上传文件的大小
                              * struts.xml文件中配置的是上传文件的总大小
                              * 这里配置的是上传文件的单个大小
                       -->
                      <param name="fileUpload.maximumSize">20971520</param>
                      <!-- 配置上传文件允许的类型,如果配置多个值的话,用","隔开 -->
                      <param name="fileUpload.allowedTypes">text/plain,application/msword</param>
                      <!-- 配置上传文件的扩展名,如果配置多个值的话,用","隔开 -->
                      <param name="fileUpload.allowedExtensions">.txt</param>
                </interceptor-ref>
             * 自定义上传文件的错误提示信息:
                 * 在动作类action同目录下,创建一个名为fileuploadmessage.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}
        
        
    * 多文件上传:
        * 所有流程于配置都与单文件上传一致。
        * 需要注意的是:
            * 在页面中,虽然是多文件上传,但是页面中表单的name属性的值必须保持一致;
            * 在动作类action中声明的相关属性,类型改成数组;
            * 在业务方法中,相关处理流程改成单文件上传的循环。
        
       


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值