2

I'm doing some file upload through jQuery ajax and thus want to have a proper response in any case. I am now struggling with the size limit I set in the application.properties:

spring.servlet.multipart.max-file-size=5MB
spring.servlet.multipart.max-request-size=5MB
spring.http.multipart.enabled=false

I found many tutorials and even some SO answers, but none of them seem to work on my side. Here two examples:

https://www.mkyong.com/spring-boot/spring-boot-file-upload-example-ajax-and-rest/

Multipart File Upload:Size exceed exception in spring boot return JSON error message

I always get an empty response and in the log I see another exception (the ERROR is from my code to see if my handler comes into play:

2018-10-19 15:57:54.183 ERROR 12940 --- [http-nio-9002-exec-2] c.i.p.m.mvc.RestExceptionHandler         : CAUSE: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (12698493) exceeds the configured maximum (5242880)
2018-10-19 15:57:54.185  WARN 12940 --- [http-nio-9002-exec-2] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (12698493) exceeds the configured maximum (5242880)

Here is my code, the commented out parts are some variations I tried.

@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(RestExceptionHandler.class);

    @ExceptionHandler(MultipartException.class)
    //@ExceptionHandler(MaxUploadSizeExceededException.class)
    @ResponseBody
    public String handleTooLargeFiles(HttpServletRequest request, Throwable ex) {
    //public ResponseEntity<?> handleTooLargeFiles(HttpServletRequest request, Throwable ex) {
        LOGGER.error("CAUSE: " + ex.getCause().getMessage());
        return "File size exceeds the allowable limit! (5MB)";
        //return new ResponseEntity("File size exceeds the allowable limit! (5MB)", HttpStatus.PAYLOAD_TOO_LARGE);
    }

}

What am I doing wrong?

10
  • your controller advise is fine and return "File size exceeds the allowable limit! (5MB)" as I checked..can you check by calling rest client(post man or Advance rest client) as might be issue at client side receiving, as your logs are saying it been has gone through with controller advise so i believe somehting issue at client side handling string as response. Commented Oct 19, 2018 at 14:26
  • @kj007 seems I was going in circles, I'm sure I tried it with Postman and also didn't get a proper response, but now I get a response. Though when I try it with ajax, I still don't see any response at all, but this error: net::ERR_CONNECTION_RESET Commented Oct 19, 2018 at 14:43
  • Then seems your Ajax request is not hitting your spring boot server Commented Oct 19, 2018 at 14:51
  • Could you put a debug at controller and try sending a small size then 5 mb from Ajax and check it’s reached to controller Commented Oct 19, 2018 at 14:58
  • @kj007 small size work, but now I've seen something weird, it seems to loop for a while on the server, this I got after one ajax POST (omitting the actual message, but it's the same as above): 2018-10-19 16:59:11.155 WARN 17100 2018-10-19 16:59:12.154 WARN 17100 2018-10-19 16:59:13.154 WARN 17100 2018-10-19 16:59:14.154 WARN 17100 2018-10-19 16:59:15.156 WARN 17100 2018-10-19 16:59:16.155 WARN 17100 Commented Oct 19, 2018 at 15:02

1 Answer 1

1

You also have to set properties for embedded Tomcat, to not terminate your request too early:

# By default they're both "2MB"

server.tomcat.max-swallow-size=-1
server.tomcat.max-http-form-post-size=-1

(or choose some large size like 20MB instead of -1)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer, but there is no tomcat involved in my case. Just plain spring boot with the embedded web server
@Thomas ...and that embedded server is Tomcat by default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.