The dispatcherservlet must be clearly told how to handle multipartrequest.
Springmvc file upload is provided in the following ways
Configure Xxx-servlet.xml and add the following code:
Copy Code code as follows:
<bean id= "Multipartresolver"
class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<!--set the maximum size of the uploaded file to 1MB-->
<property name= "Maxuploadsize" >
<value>1048576</value>
</property>
</bean>
Copy Code code as follows:
<bean id= "Multipartresolver"
class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<!--set the maximum size of the uploaded file to 1MB-->
<property name= "Maxuploadsize" >
<value>1048576</value>
</property>
</bean>
Note that the size of the file here is actually the total size of the file
If the file size is configured, you need to configure exception information control
So you need to configure an exception display
Copy Code code as follows:
<!--Springmvc throws Org.springframework.web.multipart.MaxUploadSizeExceededException--> when it exceeds the upload file limit
<!--This exception is springmvc when checking the uploaded file information, and has not yet entered the controller method-->
<bean id= "Exceptionresolver"
class= "Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" >
<property name= "Exceptionmappings" >
<props>
<!--automatically jumps to/web-inf/jsp/error_fileupload.jsp page when Maxuploadsizeexceededexception exception is encountered-->
<prop
key= "Org.springframework.web.multipart.MaxUploadSizeExceededException" >error_fileupload</prop>
</props>
</property>
</bean>
Copy Code code as follows:
<!--Springmvc throws Org.springframework.web.multipart.MaxUploadSizeExceededException--> when it exceeds the upload file limit
<!--This exception is springmvc when checking the uploaded file information, and has not yet entered the controller method-->
<bean id= "Exceptionresolver"
class= "Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" >
<property name= "Exceptionmappings" >
<props>
<!--automatically jumps to/web-inf/jsp/error_fileupload.jsp page when Maxuploadsizeexceededexception exception is encountered-->
<prop
key= "Org.springframework.web.multipart.MaxUploadSizeExceededException" >error_fileupload</prop>
</props>
</property>
</bean>
Or lazy point, do not set the size, the final default is the value is not limited. If you do not want to control the return information, you can consider returning the specified format data in the exception handling that should be in place, such as JSON
Configure the page as always on the need to add to form form: enctype= "Multipart/form-data"
And then there's the action that needs to be handled, and there are two ways
The first way:
Copy Code code as follows:
Public String Login (@RequestParam multipartfile file, model model) {
..................
}
Copy Code code as follows:
Public String Login (@RequestParam multipartfile file, model model) {
..................
}
The name of the file must be consistent with the attribute value in the <input type=file>, and if multiple files are uploaded, consider using the
Copy Code code as follows:
Public String Login (@Valid UserInfo UserInfo, bindingresult result, @RequestParam multipartfile[] files, model model) {
..................
}
Copy Code code as follows:
Public String Login (@Valid UserInfo UserInfo, bindingresult result, @RequestParam multipartfile[] files, model model) {
..................
}
Single file can be omitted @RequestParam multiple files are not omitted
The second way:
Copy Code code as follows:
Public Modelandview HandleRequest (HttpServletRequest request,
HttpServletResponse response) throws Exception {
Transition to Multiparthttprequest:
Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
Get files:
Multipartfile file = multipartrequest.getfile ("file");
}
Copy Code code as follows:
Public Modelandview HandleRequest (HttpServletRequest request,
HttpServletResponse response) throws Exception {
Transition to Multiparthttprequest:
Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
Get files:
Multipartfile file = multipartrequest.getfile ("file");
}
This also allows you to get to the file
In fact, the first need to configure is to add two jar packages:
Commons-fileupload-1.2.2.jar
Commons-io-2.1.jar