本文共 2877 字,大约阅读时间需要 9 分钟。
MultipartResolver
是spring
提供的文件上传解析器的接口,该接口有两个实现类:StandardServletMultipartResolver
、CommonsMultipartResolver
,MultipartResolver#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
实现,继承自apache
的CommonsFileUploadSupport
。在spring中注入CommonsMultipartResolver
时,可以配置"maxUploadSize", "maxInMemorySize" and "defaultEncoding"
等属性。需要依赖jar
:commons-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/