spring-guides / gs-uploading-files

Uploading Files :: Learn how to build a Spring application that accepts multi-part file uploads.
http://spring.io/guides/gs/uploading-files/
Apache License 2.0
469 stars 503 forks source link

max-file-size doesn't seem to do anything #59

Closed andrewe123 closed 5 years ago

andrewe123 commented 5 years ago

I might be misunderstanding how the max-size configuration is supposed to be applied, but it doesn't seem to do anything.

You can set it to 0B (or anything else), and see that the configuration is picked up.

spring.servlet.multipart.max-file-size=0B
spring.servlet.multipart.max-request-size=0B
    private final StorageService storageService;
    private MultipartConfigElement multipartConfigElement;

    @Autowired
    public FileUploadController(StorageService storageService, MultipartConfigElement multipartConfigElement) {
        this.storageService = storageService;
        this.multipartConfigElement = multipartConfigElement;
    }
...
    @PostMapping("/")
    public String handleFileUpload(@RequestParam("file") MultipartFile file,
            RedirectAttributes redirectAttributes) {
      multipartConfigElement.getMaxFileSize(); // Value is 0 when inspected.
...

But it doesn't seem to do anything. The 16B test file still gets saved without any impact from the max-size configuration.

Although not directly related to this app, the documentation here lead me to believe that setting the max-size values would automatically produce some error, and if that's not the case it might be helpful to clarify.

andrewe123 commented 5 years ago

The configuration does work, but doesn't seem to work with MockMVC.

When I ran the application and did a test with postman it worked.

https://stackoverflow.com/a/55532976/5654793