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

    在使用springMVC进行系统实现时,springMVC默认的解析器里面是没有加入对文件上传的解析的,这可以方便我们实现自己的文件上传。但如果你想使用springMVC对文件上传的解析器来处理文件上传的时候就需要在spring的applicationContext里面加上springMVC提供的MultipartResolver的申明。这样之后,客户端每次进行请求的时候,springMVC都会检查request里面是否包含多媒体信息,如果包含了就会使用MultipartResolver进行解析,springMVC会使用一个支持文件处理的MultipartHttpServletRequest来包裹当前的HttpServletRequest,然后使用MultipartHttpServletRequest就可以对文件进行处理了。Spring已经为我们提供了一个MultipartResolver的实现,我们只需要拿来用就可以了,那就是org.springframework.web.multipart.commons.CommsMultipartResolver。因为springMVC的MultipartResolver底层使用的是Commons-fileupload,所以还需要加入对Commons-fileupload.jar的支持。

    Xml代码  收藏代码
    1. <!--  这里申明的id必须为multipartResolver  -->  
    2. <bean id="multipartResolver"  
    3.     class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
    4.   
    5.     <!-- one of the properties available; the maximum file size in bytes -->  
    6.     <property name="maxUploadSize" value="100000"/>  
    7. </bean>  

    CommonsMultipartResolver允许设置的属性有:
        defaultEncoding:表示用来解析request请求的默认编码格式,当没有指定的时候根据Servlet规范会使用默认值ISO-8859-1。当request自己指明了它的编码格式的时候就会忽略这里指定的defaultEncoding。
        uploadTempDir:设置上传文件时的临时目录,默认是Servlet容器的临时目录。
        maxUploadSize:设置允许上传的最大文件大小,以字节为单位计算。当设为-1时表示无限制,默认是-1。
        maxInMemorySize:设置在文件上传时允许写到内存中的最大值,以字节为单位计算,默认是10240。

    下面是一个简单示例:

    .html文件

    Html代码  收藏代码
    1. <html>  
    2.     <head>  
    3.         <title>Upload a file please</title>  
    4.     </head>  
    5.     <body>  
    6.         <h1>Please upload a file</h1>  
    7. <!--   enctype(编码格式)必须为multipart/form-data  -->  
    8.         <form method="post" action="/form" enctype="multipart/form-data">  
    9.             <input type="text" name="name"/>  
    10.             <input type="file" name="file"/>  
    11.             <input type="submit"/>  
    12.         </form>  
    13.     </body>  
    14. </html>  

    对应的action controller:

    Java代码  收藏代码
      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.         //MultipartFile是对当前上传的文件的封装,当要同时上传多个文件时,可以给定多个MultipartFile参数  
      8.         if (!file.isEmpty()) {  
      9.             byte[] bytes = file.getBytes();  
      10.             // store the bytes somewhere  
      11.            //在这里就可以对file进行处理了,可以根据自己的需求把它存到数据库或者服务器的某个文件夹  
      12.   
      13.            return "redirect:uploadSuccess";  
      14.        } else {  
      15.            return "redirect:uploadFailure";  
      16.        }  
      17.     }  
      18.   
      19. }  
  • 相关阅读:
    JavaScript中的闭包
    SQL 备忘
    SqlServer 2005 升级至SP2过程中出现"身份验证"无法通过的问题
    unable to start debugging on the web server iis does not list an application that matches the launched url
    Freebsd 编译内核
    Freebsd 6.2中关于无线网络的设定
    【Oracle】ORA01219
    【Linux】Windows到Linux的文件复制
    【Web】jar命令行生成jar包
    【Linux】CIFS挂载Windows共享
  • 原文地址:https://www.cnblogs.com/henuyuxiang/p/6146108.html
Copyright © 2011-2022 走看看