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
配置。<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:Spring_mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <multipart-config> <!--临时文件的目录--> <location>d:/</location> <!-- 上传文件的大小限制 --> <max-file-size>5242880</max-file-size> <!-- 一次表单提交中文件的大小限制 --> <max-request-size>10485760</max-request-size> <!-- 多大的文件会被自动保存到硬盘上 0:所有 --> <file-size-threshold>0</file-size-threshold> </multipart-config> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
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). <p><b>Note:</b> 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
。<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 其他配置参见CommonsFileUploadSupport中以set开头的方法 --> <property name="defaultEncoding" value="UTF-8" /> <property name="maxUploadSize" value="5242880" /> <property name="maxInMemorySize" value="40960" /> </bean>
Servlet-based {@link MultipartResolver} implementation for <a href="http://commons.apache.org/proper/commons-fileupload">Apache Commons FileUpload</a> 1.2 or above. <p>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. <p>Saves temporary files to the servlet container's temporary directory. Needs to be initialized <i>either</i> by an application context <i>or</i> via the constructor that takes a ServletContext (for standalone usage).
在controller中用MultipartFile和MultipartHttpServletRequest接收参数即可,具体可以尝试解析这两种方式有什么API
最后
以上就是魔幻翅膀最近收集整理的关于Spring MVC之MultipartResolver的全部内容,更多相关Spring内容请搜索靠谱客的其他文章。
发表评论 取消回复