`
234390216
  • 浏览: 10192130 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
博客专栏
A5ee55b9-a463-3d09-9c78-0c0cf33198cd
Oracle基础
浏览量:460734
Ad26f909-6440-35a9-b4e9-9aea825bd38e
springMVC介绍
浏览量:1771658
Ce363057-ae4d-3ee1-bb46-e7b51a722a4b
Mybatis简介
浏览量:1395324
Bdeb91ad-cf8a-3fe9-942a-3710073b4000
Spring整合JMS
浏览量:393854
5cbbde67-7cd5-313c-95c2-4185389601e7
Ehcache简介
浏览量:678180
Cc1c0708-ccc2-3d20-ba47-d40e04440682
Cas简介
浏览量:529236
51592fc3-854c-34f4-9eff-cb82d993ab3a
Spring Securi...
浏览量:1178634
23e1c30e-ef8c-3702-aa3c-e83277ffca91
Spring基础知识
浏览量:461646
4af1c81c-eb9d-365f-b759-07685a32156e
Spring Aop介绍
浏览量:150077
2f926891-9e7a-3ce2-a074-3acb2aaf2584
JAXB简介
浏览量:66795
社区版块
存档分类
最新评论
阅读更多

    在使用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的支持。

<!--  这里申明的id必须为multipartResolver  -->
<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="100000"/>
</bean>

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

 

 

下面是一个简单示例:

.html文件

<html>
    <head>
        <title>Upload a file please</title>
    </head>
    <body>
        <h1>Please upload a file</h1>
<!--   enctype(编码格式)必须为multipart/form-data  -->
        <form method="post" action="/form" enctype="multipart/form-data">
            <input type="text" name="name"/>
            <input type="file" name="file"/>
            <input type="submit"/>
        </form>
    </body>
</html>

 

 

对应的action controller:

@Controller
public class FileUpoadController {

    @RequestMapping(value = "/form", method = RequestMethod.POST)
    public String handleFormUpload(@RequestParam("name") String name,
        @RequestParam("file") MultipartFile file) {
        //MultipartFile是对当前上传的文件的封装,当要同时上传多个文件时,可以给定多个MultipartFile参数
        if (!file.isEmpty()) {
            byte[] bytes = file.getBytes();
            // store the bytes somewhere
           //在这里就可以对file进行处理了,可以根据自己的需求把它存到数据库或者服务器的某个文件夹

           return "redirect:uploadSuccess";
       } else {
           return "redirect:uploadFailure";
       }
    }

}
 

 

 

 

 

14
1
分享到:
评论
2 楼 shengshihouzhe 2016-03-16  
可以用
1 楼 l975764577 2014-12-25  
楼主能不能把demo发下给我  谢谢,975764577@qq.com

相关推荐

Global site tag (gtag.js) - Google Analytics