When a form is submitted with a enctype= "Multipart/form-data" attribute, it is found that other input contained in the form cannot be set to the corresponding method parameter.
As follows:
JSP: A form with a enctype= "Multipart/form-data" attribute, containing a username input
<form action= "${ctx}/console/article/testupload" enctype= "Multipart/form-data" method= "
Post ">
<input type=" text "name=" username "/>
<input type=" file "name=" UploadFile "/>
< Input type= "Submit"/>
</form>
Controller: Very common two methods, get into the page, post output form username value (note, no processing uploadfile).
@RequestMapping (value = "/testupload", produces = "Text/html;charset=utf-8", method = requestmethod.get) public
String Testupload () throws Exception {
return "/uploadfile";
}
@RequestMapping (value = "/testupload", produces = "Text/html;charset=utf-8", method = requestmethod.post) public
String Testupload (string username) throws Exception {
System.out.println (username);
return "Redirect:testupload";
}
Found below (the following points are independent of each other):
1, fill in username and select the file click Submit, username output is null;
2, remove the enctype attribute of the form, fill in the username and select the file, click Submit, username output is normal;
3, add multipartfile uploadfile parameter to post method (modify result below), fill in username and select File, click Submit, error.
@RequestMapping (value = "/testupload", produces = "Text/html;charset=utf-8", method = requestmethod.post) public
String Testupload (string username, Multipartfile uploadfile) throws Exception {
System.out.println (username);
return "Redirect:testupload";
}
The error message is as follows:
According to the above findings:
1, with the enctype attribute of the form, need additional configuration to parse the parameters;
2, if the parameter in the method does not process the file, that is, there is no multipartfile parameter, no error, and can not get other parameters.
Find a solution online (see the last link) and learn that you need to configure Multipartresolver to parse a form with enctype= "Multipart/form-data" property
<bean id= "Multipartresolver" class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<property name= "defaultencoding" value= "Utf-8" ></property>
<property name= "Maxuploadsize" Value= "10485760000" ></property>
<property name= "maxinmemorysize" value= "40960" ></property >
</bean>
Report:
https://segmentfault.com/q/1010000002786408