struts2文件上传

struts2中如何实现上传
     * 引入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar
     
     * 定义jsp
          <form action="${pageContext.request.contextPath}/upload/uploadAction_saveFile.action"  
	          name="form1"  method="post"  enctype="multipart/form-data" >
	             
	             上传文件名称:<input type="file" name="uploadImage">
	           <input type="submit" value="上传">
         </form>
         
     * 创建action
	          public class UploadAction extends ActionSupport {
				/*
				 * * 上传文件名称:<input type="file" name="uploadImage"> 对应的是file上传组件name属性的值
				 * * 上传的文件先保存为临时文件,目录和名称如下  
				 *    E:\\apache-tomcat-6.0.18\\work\\Catalina\\localhost\\itcast0706struts2\\upload__24bfe9c0_1311be9e985__8000_00000000.tmp
				 * * 当程序处理完毕是,struts2会删除该临时文件
				 *    Removing file uploadImage 
				 *       E:\\apache-tomcat-6.0.18\\work\\Catalina\\localhost\\itcast0706struts2\\upload__24bfe9c0_1311be9e985__8000_00000000.tmp
			
				 */
				private File uploadImage;//对应的是file上传组件name属性的值
				
				//上传文件的类型[格式:上传组件name属性的值+ContentType]
				private String uploadImageContentType ;
				
				//上传文件的真实名称[格式:上传组件name属性的值+FileName]
				private String uploadImageFileName;
				
				public String saveFile(){
					System.out.println(uploadImageContentType+"   "+uploadImageFileName);
					ServletContext sc=ServletActionContext.getServletContext();
					String realPath=sc.getRealPath("/pic");
					try {
						File destFile=new File(realPath,uploadImageFileName);
						FileUtils.copyFile(uploadImage, destFile);   //uploadImage.renameTo(destFile)方法也可以移动文件
						
					} catch (IOException e) {
						e.printStackTrace();
					}
					return "success";
				}
		    }
    *  创建strurs_upload.xml文件
            <struts>
		    <!--配置上传文件的总开关,控制上传文件的大小(默认值是2M),是上传组件自带的
		        org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException,
		         抛出的异常是上传组件自带的,
		    -->
		    <constant name="struts.multipart.maxSize" value="222097152"></constant>
		
		    <package name="upload" namespace="/upload"  extends="struts-default">
		         <action name="uploadAction_*" class="cn.itcast.upload.UploadAction" method="{1}">
		             
		            <!-- 修改默认栈中上传拦截器的属性值-->
					<!-- 
						如果使用的不是默认栈是自定义栈 而自定义栈参考了默认栈 则在param的name中需要写清楚
						如defaultStack.fileUpload.maximumSize
					-->
		            <interceptor-ref name="defaultStack">
		               <!-- 控制上传文件的大小,分开关,这个开关是struts2提供的 -->
		               <param name="fileUpload.maximumSize">12097152</param>
		               <!-- 配置允许上传文件的类型,如果有多个用","隔开 -->
		               <param name="fileUpload.allowedTypes">text/plain,application/vnd.ms-powerpoint</param>
		               <!-- 配置允许上传文件的扩展名,如果有多个用","隔开-->
		               <param name="fileUpload.allowedExtensions ">ppt</param>
		            </interceptor-ref>
		          
		            <result name="success">/upload/success.jsp</result>
		            <!--当文件上传失败时,要转到由input属性所指向的页面,在该页面给出提示信息-->
		            <result name="input">/upload/error.jsp</result>
		         </action>
		    </package>
		</struts>
		
    * 测试		
    
    
    * 处理错误信息
        * 在处理错误信息的jsp页面(error.jsp)
           * 引入<%@ taglib uri="/struts-tags"   prefix="s"%>
           * 使用<s:fielderror></s:fielderror>显示错误信息
           * 默认显示的错误信息是英文的,该英文信息存在struts2-core-2.1.8.1.jar\org\apache\struts2\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}:html页面上传组件的name属性的值:<input type="file" name="uploadImage">
                {1}:上传文件的真实名称
                {2}:上传的临时文件(保存到临时目录的临时名称)
                {3}:上传文件的类型(对struts.messages.error.file.too.large来说表示上传文件的大小)
                
           
        * 修改上传文件的错误信息为中文
             * 在于当前action的同级目录(其他目录也可),创建fileupload.properties文件,文件名称自定义
                  内容如下             
                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文件中加载该资源文件   
                   <!-- 配置加载自定义的国际化资源文件,有多个的话,用","隔开
				         * cn.itcast.converter.converter:加载cn.itcast.converter.converter.properties资源文件
				         * cn.itcast.upload.fileupload:加载cn.itcast.upload.fileupload.properties资源文件
				    -->
				    <constant name="struts.custom.i18n.resources" 
				              value="cn.itcast.converter.converter,
				                     cn.itcast.upload.fileupload"></constant>
             
     
					    
		
		
		
         
         
   

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值