Skip to content

Spring Boot Multipart Size

DodaTech 1 min read

In this tutorial, you'll learn about Fix Spring Boot Multipart Max Upload Size Exceeded. We cover key concepts, practical examples, and best practices.

The Problem

File upload fails when the file exceeds the default maximum size of 1MB.

Quick Fix

Increase max file size

Wrong:

# Default 1MB

Output:

File too large error

Right:

spring.servlet.multipart.max-file-size=50MB
spring.servlet.multipart.max-request-size=50MB

Output:

50MB files accepted

Configure per-request size

Wrong:

// Global setting only

Output:

All requests affected

Right:

spring.servlet.multipart.max-file-size=10MB
// Individual clients can send up to 10MB

Output:

Per-app limit

Handle size exceptions

Wrong:

// No exception handler

Output:

Generic error page

Right:

@ExceptionHandler(MaxUploadSizeExceededException.class)
public ResponseEntity<?> handleMaxSize() {
    return ResponseEntity.badRequest().body("File too large");
}

Output:

Custom error message

Prevention

  • Set max-file-size and max-request-size for the desired limit
  • Catch MaxUploadSizeExceededException for friendly error messages
  • Configure limits appropriate for your use case

Common Mistakes with boot multipart size

  1. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  2. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  3. Using head and tail instead of pattern matching, causing runtime errors on empty lists

These mistakes appear frequently in real-world SPRING code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does the request fail silently?

MaxUploadSizeExceededException is thrown before the controller method is called. Use @ExceptionHandler.

This quick fix is part of the DodaTech Spring & JVM ecosystem series. Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro