Spring MVC multiple file upload with HTML5 multiple file form feature

后端 未结 2 1378
一个人的身影
一个人的身影 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:34

    Have included commons-fileupload dependency?

    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3</version>
    </dependency>
    

    I tested the fileupload works fine even with ArrayList as the parameter type on the controller handler

    0 讨论(0)
  • 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<MultipartFile> files;
    private MultipartFile[] files;
    

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

    0 讨论(0)
提交回复
热议问题