Spring MVC multiple file upload with HTML5 multiple file form feature

后端 未结 2 1370
一个人的身影
一个人的身影 2021-02-13 16:55

I am trying to upload multiple files using spring 3.1.2 with @Controller and @RequestMapping.

Here\'s what I did and my configuration.

Html5 form :

<         


        
2条回答
  •  北恋
    北恋 (楼主)
    2021-02-13 17:36

    Although you've already gotten your answer thanks to Alex, I'd just like to elaborate a bit. With Spring binding, form fields are bound to their "name" attributes in the HTML. Since it is impossible to have a form field named files[] (if one declares a variable name with that syntax, its name is files, but it is an array of the declaring type), Spring couldn't match it up - and the behavior in that case is to disregard the data in the request.

    Using a type such as MultipartFile, you can use either a List named "files" or an array as the following examples:

    private List files;
    private MultipartFile[] files;
    

    With appropriate getters and setters, you can then access and mutate the file list accordingly.

提交回复
热议问题