SpringMVC上传文件

使用springMVC提供的CommonsMultipartFile类进行读取文件

需要用到上传文件的两个jar包 commons-logging.jar、commons-io-xxx.jar

1、在spring配置文件中配置文件上传解析器

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!-- 文件上传解析器 -->  
  2. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  3.     <property name="defaultEncoding" value="utf-8"></property>  
  4.     <property name="maxUploadSize" value="10485760000"></property><!-- 最大上传文件大小 -->  
  5.     <property name="maxInMemorySize" value="10960"></property>  
  6. </bean>  

2、文件上传页面(index.jsp)

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!-- method必须为post 及enctype属性-->  
  2. <form action="fileUpload.do" method="post" enctype="multipart/form-data">  
  3.     <input type="file" name="file">  
  4.     <input type="submit" value="上传">  
  5. </form>  

3、FileController类

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @Controller  
  2. public class FileController{  
  3.       
  4.     @RequestMapping("/fileUpload.do")  
  5.     public String fileUpload(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request,HttpServletResponse response){  
  6.         long startTime=System.currentTimeMillis();   //获取开始时间  
  7.         if(!file.isEmpty()){  
  8.             try {  
  9.                 //定义输出流 将文件保存在D盘    file.getOriginalFilename()为获得文件的名字   
  10.                 FileOutputStream os = new FileOutputStream("D:/"+file.getOriginalFilename());  
  11.                 InputStream in = file.getInputStream();  
  12.                 int b = 0;  
  13.                 while((b=in.read())!=-1){ //读取文件   
  14.                     os.write(b);  
  15.                 }  
  16.                 os.flush(); //关闭流   
  17.                 in.close();  
  18.                 os.close();  
  19.                   
  20.             } catch (FileNotFoundException e) {  
  21.                 e.printStackTrace();  
  22.             } catch (IOException e) {  
  23.                 e.printStackTrace();  
  24.             }  
  25.         }  
  26.         long endTime=System.currentTimeMillis(); //获取结束时间  
  27.         System.out.println("上传文件共使用时间:"+(endTime-startTime));  
  28.         return "success";  
  29.     }  
  30. }  

上传了一个3.54M的PDF文件 共使用29132毫秒(以自己计算机实际为准)


基于上一篇文件上传发现效率很慢,我们应该对它进行优化  使用springMVC对文件上传的解析器

来处理文件上传的时候需要在springapplicationContext里面加上springMVC提供的MultipartResolver的申明

这样客户端请求的时候 springMVC会检查request里面是否包含多媒体信息 如果包含了就会使用MultipartResolver进行解析,

springMVC会使用一个支持文件  处理的MultipartHttpServletRequest来包裹当前的HttpServletRequest

然后使用MultipartHttpServletRequest就可以对文件进行处理了


此处只改动FileController类 其他配置参考上一篇 http://blog.csdn.net/itmyhome1990/article/details/27976873

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @Controller  
  2. public class FileController{  
  3.       
  4.     @RequestMapping("/fileUpload.do")  
  5.     public String fileUpload(HttpServletRequest request,HttpServletResponse response){  
  6.         long startTime=System.currentTimeMillis();   //获取开始时间  
  7.           
  8.         CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());  
  9.         if(multipartResolver.isMultipart(request)){ //判断request是否有文件上传  
  10.             MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;  
  11.             Iterator<String> ite = multiRequest.getFileNames();  
  12.             while(ite.hasNext()){  
  13.                 MultipartFile file = multiRequest.getFile(ite.next());  
  14.                 if(file!=null){  
  15.                     File localFile = new File("D:/"+file.getOriginalFilename());  
  16.                     try {  
  17.                         file.transferTo(localFile); //将上传文件写到服务器上指定的文件  
  18.                     } catch (IllegalStateException e) {  
  19.                         e.printStackTrace();  
  20.                     } catch (IOException e) {  
  21.                         e.printStackTrace();  
  22.                     }  
  23.                 }  
  24.             }  
  25.         }  
  26.         long endTime=System.currentTimeMillis(); //获取结束时间  
  27.         System.out.println("上传文件共使用时间:"+(endTime-startTime));  
  28.           
  29.         return "success";  
  30.     }  
  31. }  


同样上传一个3.54M的PDF文件 只使用了16毫秒(已自己计算机实际为准)

可见差别之悬殊。

转自http://blog.csdn.net/itmyhome1990/article/details/27976873


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值