博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring MVC之MultipartResolver
阅读量:3751 次
发布时间:2019-05-22

本文共 2877 字,大约阅读时间需要 9 分钟。

MultipartResolverspring提供的文件上传解析器的接口,该接口有两个实现类:StandardServletMultipartResolverCommonsMultipartResolverMultipartResolver#isMultipart是判断是否文件上传的请求,post请求、contentType是否是以multipart/开头这两个是先决条件。

spring上下文中,有且只有一个该实例。 spring容器启动时初始化文件上传解析器:

DispatcherServlet#initStrategies(ApplicationContext context) {	initMultipartResolver(context);}
  • StandardServletMultipartResolver

    基于Servlet3.0 API实现,不需要引入额外的jar包,在spring的配置文件中注入此Bean时,不需要配置上传文件大小等参数,但是在web.xml中配置DispatcherServlet时,需要添加multipart-config配置。

    springmvc
    org.springframework.web.servlet.DispatcherServlet
    contextConfigLocation
    classpath:Spring_mvc.xml
    1
    d:/
    5242880
    10485760
    0
    springmvc
    /
    Standard implementation of the {@link MultipartResolver} interface, based on the Servlet 3.0 {@link javax.servlet.http.Part} API. To be added as "multipartResolver" bean to a Spring DispatcherServlet context, without any extra configuration at the bean level (see below). 

    Note: In order to use Servlet 3.0 based multipart parsing, you need to mark the affected servlet with a "multipart-config" section in {@code web.xml}, or with a {@link javax.servlet.MultipartConfigElement} in programmatic servlet registration, or (in case of a custom servlet class) possibly with a {@link javax.servlet.annotation.MultipartConfig} annotation on your servlet class. Configuration settings such as maximum sizes or storage locations need to be applied at that servlet registration level; Servlet 3.0 does not allow for them to be set at the MultipartResolver level.

  • CommonsMultipartResolver

    基于Apache Commons FileUpload实现,继承自apacheCommonsFileUploadSupport。在spring中注入CommonsMultipartResolver时,可以配置"maxUploadSize", "maxInMemorySize" and "defaultEncoding"等属性。需要依赖jarcommons-fileupload、commons-io

    Servlet-based {@link MultipartResolver} implementation for Apache Commons FileUpload 1.2 or above.  

    Provides "maxUploadSize", "maxInMemorySize" and "defaultEncoding" settings as bean properties (inherited from {@link CommonsFileUploadSupport}). See corresponding ServletFileUpload / DiskFileItemFactory properties ("sizeMax", "sizeThreshold", "headerEncoding") for details in terms of defaults and accepted values.

    Saves temporary files to the servlet container's temporary directory. Needs to be initialized either by an application context or via the constructor that takes a ServletContext (for standalone usage).

在controller中用MultipartFile和MultipartHttpServletRequest接收参数即可,具体可以尝试解析这两种方式有什么API

转载地址:http://hvcsn.baihongyu.com/

你可能感兴趣的文章
算法训练 1的个数(输入正整数n,判断从1到n之中,数字1一共要出现几次。例如1123这个数,则出现了两次1。例如15,那么从1到15之中,一共出现了8个1。)
查看>>
算法训练 素因子去重(给定一个正整数n,求一个正整数p,满足p仅包含n的所有素因子,且每个素因子的次数不大于1)
查看>>
算法训练 二进制数数( 给定L,R。统计[L,R]区间内的所有数在二进制下包含的“1”的个数之和。   如5的二进制为101,包含2个“1”。)
查看>>
第十届MathorCup高校数学建模D题解题思路
查看>>
2020年高教社杯全国大学生数学建模竞赛赛题 C题分析与思路!(持续更新)
查看>>
2020年高教社杯全国大学生数学建模竞赛赛题 B题分析与思路!(持续更新)
查看>>
蓝桥杯真题 18省4-测试次数 x星球的居民脾气不太好,但好在他们生气的时候唯一的异常举动是:摔手机。 各大厂商也就纷纷推出各种耐摔型手机。x星球的质监局规定了手机必须经过耐摔测试,并且评定出一个耐
查看>>
蓝桥杯真题 19省3-数列求值 给定数列 1, 1, 1, 3, 5, 9, 17, …,从第 4 项开始,每项都是前 3 项的和。求第 20190324 项的最后 4 位数字。
查看>>
大小写字母转换函数tolower();的用法
查看>>
蓝桥杯 15校4-7对数字 今有7对数字:两个1,两个2,两个3,...两个7,把它们排成一行。 要求,两个1间有1个其它数字,两个2间有2个其它数字,以此类推,两个7之间有7个其它数字。如下就是
查看>>
蓝桥杯真题 17省10-k倍区间 给定一个长度为N的数列,A1, A2, ... AN,如果其中一段连续的子序列Ai, Ai+1, ... Aj(i <= j)之和是K的倍数,我们就称这个区间[i
查看>>
TCP协议的流量控制
查看>>
TCP连接的三次握手过程,为什么不是两次或四次?
查看>>
小白都能看懂的DNS解析过程
查看>>
HTTP和HTTPS的区别?描述HTTPS的工作过程
查看>>
简述一下HTTP的状态码
查看>>
20210227vulhub靶场之环境配置---无法获得靶机IP的疑难解决方式(可以解决VBox和VMware不兼容问题)
查看>>
20210226web渗透学习之SSRF总结
查看>>
2021-06-01web渗透学习之sqlserver提权(转)
查看>>
大数据之Flume
查看>>