zoukankan      html  css  js  c++  java
  • SpringMVC multipart文件上传

    一、介绍
       spring内建的multipart支持网络程序文件上传。我们可以通过配置MultipartResolver来启动上传支持。它定义在org.springframework.web.multipart包中。spring是通过使用Commons FileUpload插件来完成MultipartResolver的。
       默认情况下,spring不处理multipar的form信息,因为开发者默认会自己去处理这部分信息,当然我们可以随时打开这个支持。这样对于每一个请求,都会查看它是否包含multipart的信息,如果没有则按流程继续执行。如果发现有,就会交给已经被声明的MultipartResolver进行处理,然后我们就能像处理其他普通属性一样处理文件上传了。
    二、使用MultipartResolver
      下面的例子显示了如何使用CommonsMultipartResolver

    [html] view plaincopy
     
    1. <bean id="multipartResolver"  
    2.     class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
    3.   
    4.     <!-- one of the properties available; the maximum file size in bytes -->  
    5.     <property name="maxUploadSize" value="100000"/>  
    6. </bean>  

    当然,我们要把所需的jar包放到lib中, 就是commons-fileupload.jar.

    三、处理一个文件上传的form
       当MultipartResolver处理完成以后,请求被处理成和普通请求一样。下面是页面文件。

    [html] view plaincopy
     
    1. <html>  
    2.     <head>  
    3.         <title>Upload a file please</title>  
    4.     </head>  
    5.     <body>  
    6.         <h1>Please upload a file</h1>  
    7.         <form method="post" action="/form" enctype="multipart/form-data">  
    8.             <input type="text" name="name"/>  
    9.             <input type="file" name="file"/>  
    10.             <input type="submit"/>  
    11.         </form>  
    12.     </body>  
    13. </html>  

    下一步是创建一个controller来处理文件上传。controller也和其他的一样,除了在我们的方法参数中使用MultipartHttpServletRequest或者MultipartFile。

    [java] view plaincopy
     
    1. @Controller  
    2. public class FileUpoadController {  
    3.   
    4.     @RequestMapping(value = "/form", method = RequestMethod.POST)  
    5.     public String handleFormUpload(@RequestParam("name") String name,  
    6.         @RequestParam("file") MultipartFile file) {  
    7.   
    8.         if (!file.isEmpty()) {  
    9.             byte[] bytes = file.getBytes();  
    10.             // store the bytes somewhere  
    11.            return "redirect:uploadSuccess";  
    12.        } else {  
    13.            return "redirect:uploadFailure";  
    14.        }  
    15.     }  
    16.   
    17. }  

    最后,我们要声明我们的controller和multipar解析器

    [html] view plaincopy
     
    1. <beans>  
    2.     <bean id="multipartResolver"  
    3.         class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>  
    4.     <!-- Declare explicitly, or use <context:annotation-config/> -->  
    5.     <bean id="fileUploadController" class="examples.FileUploadController"/>  
    6.   
    7. </beans>   

    --------------------------

    如果你对java、swing、各种框架、javascript、css、linux、数据库编程等知识很感兴趣,或者正在从事这些工作,请加入我建的群:464696550

  • 相关阅读:
    关于web测试收集
    Webdriver初探
    Java学习笔记 11/15:一个简单的JAVA例子
    持续集成环境搭建总结
    启动速度
    excel中用到的函数
    jmerter使用
    接口测试
    内存泄漏场景
    手机连接电脑那些事
  • 原文地址:https://www.cnblogs.com/zhangwei595806165/p/5102280.html
Copyright © 2011-2022 走看看