First of all, we need to know what Spring Boot is. Let me briefly talk about it here. Spring Boot can be regarded as a framework in a framework--->Integrates various frameworks, such as security, jpa, data, cloud, etc. It does not need to care about configuration and can quickly start development. If you are interested, you can understand the principle of automated configuration implementation. In essence, it is a conditional configuration implementation of spring 4.0. If you leave the bets deep, you will see it.
The principle of Spring Boot file upload is actually Spring MVC, because this part of the work is done by Spring MVC rather than Spring Boot. So, how does SpringMVC handle the file upload process?
picture:
First, the project starts the relevant configuration, and then performs the second step above, the DispatcherServlet will look for the bean with the id of multipartResolver. In the configuration, the bean points to CommonsMultipartResolve, which implements the MultipartResolver interface.
The fourth step here will determine whether the multipart file is the isMultipart method. Return true: the multipartResolver method will be called. Passing HttpServletRequest will return a MultipartHttpServletRequest object, and then a DispatcherServlet will be processed to the Controller layer. Return false: it will be ignored and continue to pass HttpServletRequest.
In MVC, you need to configure it in the configuration file webApplicationContext.xml as follows:
<bean id="multipartResolver"> <property name="defaultEncoding" value="UTF-8"/> <property name="maxUploadSize" value="100000000"/> <property name="uploadTempDir" value="fileUpload/temp"/> </bean>
Spring Boot has been automatically configured, just use it directly, there is no problem with making a test. There is a default upload limit size, but in actual development we still make some configurations.
As follows in application.properties:
# multipart config#Default support file upload spring.http.multipart.enabled=true#File upload directory spring.http.multipart.location=/tmp/xunwu/images/#Maximum supported file size spring.http.multipart.max-file-size=4Mb#Maximum supported request size spring.http.multipart.max-request-size=20MB
Of course, you can also write configuration classes to implement them, and the specific ones will not be displayed.
After reading the above, you must have a rough understanding. Let's talk about it here. Spring provides Multipart's parser: MultipartResolver. The above is CommonsMultipartResolver. It is implemented based on a third party of Commons File Upload. This is also something before Servlet 3.0. After 3.0+, you can also not rely on third-party libraries. You can use StandardServletMultipartResolver, which also implements the MultipartResolver interface. We can take a look at its implementation:
* Copyright 2002-2017 the original author or authors.package org.springframework.web.multipart.support;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.Part;import org.apache.commons.logging.LogFactory;import org.springframework.web.multipart.MultipartException;import org.springframework.web.multipart.MultipartHttpServletRequest;import org.springframework.web.multipart.MultipartResolver;/** * 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) * possible 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. * * @author Juergen Hoeller * @since 3.1 * @see #setResolveLazily * @see HttpServletRequest#getParts() * @see org.springframework.web.multipart.commons.CommonsMultipartResolver */public class StandardServletMultipartResolver implements MultipartResolver { private boolean resolveLazily = false; /** * Set whether to resolve the multipart request lazy request lazily at the time of * file or parameter access. * <p>Default is "false", resolving the multipart elements immediately, throwing * corresponding exceptions at the time of the {@link #resolveMultipart} call. * Switch this to "true" for lazy multipart parsing, throwing parse exceptions * once the application attempts to obtain multipart files or parameters. */ public void setResolveLazily(boolean resolveLazily) { this.resolveLazily = resolveLazily; } @Override public boolean isMultipart(HttpServletRequest request) { // Same check as in Commons FileUpload... if (!"post".equals(request.getMethod().toLowerCase())) { return false; } String contentType = request.getContentType(); return (contentType != null && contentType.toLowerCase().startsWith("multipart/")); } @Override public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException { return new StandardMultipartHttpServletRequest(request, this.resolveLazily); } @Override public void cleanupMultipart(MultipartHttpServletRequest request) { // To be on the safe side: explicitly delete the parts, // but only actual file parts (for Resin compatibility) try { for (Part part : request.getParts()) { if (request.getFile(part.getName()) != null) { part.delete(); } } } catch (Throwable ex) { LogFactory.getLog(getClass()).warn("Failed to perform cleanup of multipart items", ex); } }}Here is the latter implementation configuration class of the test I wrote before. You can take a look at it briefly to understand:
package com.bj.config;import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;import org.springframework.boot.autoconfigure.web.MultipartProperties;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.multipart.MultipartResolver;import org.springframework.web.multipart.support.StandardServletMultipartResolver;import org.springframework.web.servlet.DispatcherServlet;import javax.servlet.MultipartConfigElement;@Configuration@EnableConfigurationProperties(MultipartProperties.class)public class FileUploadConfig { private final MultipartProperties multipartProperties; public FileUploadConfig(MultipartProperties multipartProperties){ this.multipartProperties=multipartProperties; } /** * Register parser* @return */ @Bean(name= DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME) @ConditionalOnMissingBean(MultipartResolver.class) public StandardServletMultipartResolver multipartResolver(){ StandardServletMultipartResolver multipartResolver = new StandardServletMultipartResolver(); multipartResolver.setResolveLazily(multipartProperties.isResolveLazily()); return multipartResolver; } /** * Upload configuration* @return */ @Bean @ConditionalOnMissingBean public MultipartConfigElement multipartConfigElement(){ return this.multipartProperties.createMultipartConfig(); }}Summarize
The above is the principle of Spring Boot file uploading introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!