Springmvc the form data with the file cannot be bound to the parameter

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.