The DispatcherServlet must be explicitly told how to handle the MultipartRequest.
SpringMVC provides the following ways to upload files
Configure xxx-servlet.xml and add the following code:
The code copy is as follows:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- Set the maximum size of uploaded files to 1MB -->
<property name="maxUploadSize">
<value>1048576</value>
</property>
</bean>
The code copy is as follows:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- Set the maximum size of uploaded files to 1MB -->
<property name="maxUploadSize">
<value>1048576</value>
</property>
</bean>
Note that the file size here is actually only so the total file size
If you configure the file size, you think you need to configure exception information control
Therefore, it is necessary to configure abnormal display
The code copy is as follows:
<!-- When SpringMVC exceeds the upload file limit, it throws org.springframework.web.multipart.MaxUploadSizeExceededException -->
<!-- This exception was thrown by SpringMVC when checking uploaded file information, and it has not entered the Controller method at this time-->
<bean id="exceptionResolver"
>
<property name="exceptionMappings">
<props>
<!-- When encountering a MaxUploadSizeExceededException exception, it will automatically jump to /WEB-INF/jsp/error_fileupload.jsp page-->
<prop
key="org.springframework.web.multipart.MaxUploadSizeExceededException">error_fileupload</prop>
</props>
</property>
</bean>
The code copy is as follows:
<!-- When SpringMVC exceeds the upload file limit, it throws org.springframework.web.multipart.MaxUploadSizeExceededException -->
<!-- This exception was thrown by SpringMVC when checking uploaded file information, and it has not entered the Controller method at this time-->
<bean id="exceptionResolver"
>
<property name="exceptionMappings">
<props>
<!-- When encountering a MaxUploadSizeExceededException exception, it will automatically jump to /WEB-INF/jsp/error_fileupload.jsp page-->
<prop
key="org.springframework.web.multipart.MaxUploadSizeExceededException">error_fileupload</prop>
</props>
</property>
</bean>
Or be lazy and do not set the size, the final default value is unlimited. If you have to control the return information, you can consider returning the specified format data in the exception processing, such as JSON
After the configuration page, as always, add it to the form form: enctype="multipart/form-data"
Then there are actions that need to be processed. There are two ways to
The first method:
The code copy is as follows:
public String login( @RequestParam MultipartFile file, Model model) {
……………………
}
The code copy is as follows:
public String login( @RequestParam MultipartFile file, Model model) {
……………………
}
The name of the file must be guaranteed to be consistent with the attribute value in <input type=file>. If it is uploaded by multiple files, consider using it.
The code copy is as follows:
public String login(@Valid UserInfo userInfo, BindingResult result, @RequestParam MultipartFile[] files, Model model) {
……………………
}
The code copy is as follows:
public String login(@Valid UserInfo userInfo, BindingResult result, @RequestParam MultipartFile[] files, Model model) {
……………………
}
Single file can be omitted @RequestParam Multiple files cannot be omitted
The second method:
The code copy is as follows:
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// Transform to MultipartHttpRequest:
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// Obtain the file:
MultipartFile file = multipartRequest.getFile(" file ");
}
The code copy is as follows:
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// Transform to MultipartHttpRequest:
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// Obtain the file:
MultipartFile file = multipartRequest.getFile(" file ");
}
This way you can also get the file
In fact, the first configuration is to add two jar packages:
commons-fileupload-1.2.2.jar
commons-io-2.1.jar