Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-15919

only one MultipartFile object populated when using an java.util.Optional MutipartFile array or list @RequestParam

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: 4.3.10
    • Fix Version/s: 4.3.12, 5.0 GA
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      Having the multipart/form-data form:

      <form action="<c:url value="/optionalPost"/>" method="post" enctype="multipart/form-data">
        <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
        <input type="file" name="files" multiple/>
        <input type="submit"/>
      </form>
      

      When selecting multiple files for the file input, only one file is actually received by the controller when handling this request:

         // Does NOT work for multiple files (only one comes in)
        @PostMapping("/optionalPost")
        public String postTest2(@RequestParam Optional<List<MultipartFile>> files) {
          if (files != null && files.isPresent()) {
            System.out.println(files.get().size());
            files.get().forEach(file -> System.out.println(file.getOriginalFilename()));
          }
          return "index";
        }
      

      This fails in the same way:

        // Does NOT work for multiple files (only one comes in)
        @PostMapping("/optionalPost")
        public String postTest3(@RequestParam Optional<MultipartFile[]> files) {
          if (files != null && files.isPresent()) {
            System.out.println(files.get().length);
            Stream.of(files.get()).forEach(file -> System.out.println(file.getOriginalFilename()));
          }
          return "index";
        }
      

      The workaround is to avoid the use of Optional:

        // Works for multiple files
        @PostMapping("/optionalPost")
        public String postTest4(@RequestParam MultipartFile[] files) {
          System.out.println(files.length);
          Stream.of(files).forEach(file -> System.out.println(file.getOriginalFilename()));
          return "index";
        }
      
        // Works for multiple files
        @PostMapping("/optionalPost")
        public String postTest5(@RequestParam List<MultipartFile> files) {
          System.out.println(files.size());
          files.forEach(file -> System.out.println(file.getOriginalFilename()));
          return "index";
        }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                juergen.hoeller Juergen Hoeller
                Reporter:
                broncace Brice Roncace
                Last updater:
                Spring Issuemaster
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Days since last comment:
                  26 weeks ago