0
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@PostMapping(value = "/upload",
        consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public @ResponseBody ResponseBody uploadAttachments(
        @RequestParam(value = "comment", required = true) String comment,
        @RequestPart(value = "files", required = false) List<MultipartFile> files
) {
    /* do magic */
}

I'm using this mapping to process a form with attachments. It consumes multipart/form-data.

How can I limit the maximum number of attached files to 10?

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
  • maybe you should focus more on the total size of files rather than the number of files? if you limit the number (i dont know how right now though) you may still get 10 files of 1GB each? just an observation – masadwin May 25 '17 at 10:01
  • Yes, I totally agree with you. I've already restricted `max-file-size` to 2MB and `max-request-size` to 20MB. And now I'm looking for a way to put restrictions on the list size. – naXa stands with Ukraine May 25 '17 at 10:11
  • https://stackoverflow.com/questions/6203740/spring-web-mvc-validate-individual-request-params maybe this might be of some help to you – masadwin May 25 '17 at 10:24
  • 1
    @masadwin Thank you! this [answer](https://stackoverflow.com/a/39212603/1429387) was helpful for me. I used `@Size(max = 10)` annotation on my `files` parameter. Please post your answer so I can accept it. – naXa stands with Ukraine May 25 '17 at 12:05
  • thank you. glad to be of help – masadwin May 25 '17 at 12:38

1 Answers1

1

as indicated in this answer Spring Web MVC - validate individual request params

use @Size(max = ?) annotation on your parameter to limit the input size

masadwin
  • 422
  • 2
  • 5
  • 11