java
39 lines · 6 steps
Configuring upload size limits in Spring
Cap multipart upload sizes and turn the resulting overflow exception into a clean HTTP 413 response.
Explained by
highlit
1package com.example.uploads.config;
2
3import jakarta.servlet.MultipartConfigElement;
4import org.springframework.boot.web.servlet.MultipartConfigFactory;
5import org.springframework.context.annotation.Bean;
6import org.springframework.context.annotation.Configuration;
7import org.springframework.util.unit.DataSize;
8import org.springframework.web.bind.annotation.ExceptionHandler;
9import org.springframework.web.bind.annotation.RestControllerAdvice;
10import org.springframework.web.multipart.MaxUploadSizeExceededException;
11import org.springframework.http.HttpStatus;
12import org.springframework.http.ProblemDetail;
13
14@Configuration
15public class MultipartConfig {
16
17 @Bean
18 public MultipartConfigElement multipartConfigElement() {
19 MultipartConfigFactory factory = new MultipartConfigFactory();
20 factory.setMaxFileSize(DataSize.ofMegabytes(5));
21 factory.setMaxRequestSize(DataSize.ofMegabytes(15));
22 factory.setFileSizeThreshold(DataSize.ofKilobytes(512));
23 return factory.createMultipartConfig();
24 }
25
26 @RestControllerAdvice
27 static class UploadSizeAdvice {
28
29 @ExceptionHandler(MaxUploadSizeExceededException.class)
30 public ProblemDetail handleOversizedUpload(MaxUploadSizeExceededException ex) {
31 ProblemDetail problem = ProblemDetail.forStatusAndDetail(
32 HttpStatus.PAYLOAD_TOO_LARGE,
33 "Uploaded content exceeds the maximum allowed size.");
34 problem.setTitle("Payload Too Large");
35 problem.setProperty("maxFileSizeBytes", ex.getMaxUploadSize());
36 return problem;
37 }
38 }
39}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Enforcing upload limits at the container level rejects oversized requests before they consume memory or disk.
- 2A @RestControllerAdvice centralizes exception-to-response mapping so every endpoint returns consistent errors.
- 3ProblemDetail gives you a standardized, machine-readable error body with room for custom fields.
Related explainers
typescript
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import * as Joi from 'joi';
Validating env config at boot in NestJS
configuration
schema-validation
environment-variables
Intermediate
8 steps
java
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
java
package com.acme.billing.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties;
Feature-flagged beans with Spring @ConditionalOnProperty
feature-flags
conditional-beans
strategy-pattern
Intermediate
5 steps
java
public static Map<String, String> parseCookieHeader(String header) { Map<String, String> cookies = new LinkedHashMap<>(); if (header == null || header.isBlank()) { return cookies;
Parsing an HTTP Cookie header in Java
string-parsing
http
url-decoding
Intermediate
6 steps
java
public class TimedSocketReader { private static final int READ_TIMEOUT_MS = 5_000; private static final int CONNECT_TIMEOUT_MS = 3_000;
Reading a socket with connect and read timeouts
sockets
timeouts
io
Intermediate
8 steps
java
public final class EncodingDetector { public enum Encoding { UTF_8, UTF_16LE, UTF_16BE, UTF_32LE, UTF_32BE, ASCII, UNKNOWN
Detecting text encoding from raw bytes in Java
byte-manipulation
encoding-detection
bitwise-operations
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/configuring-upload-size-limits-in-spring-explained-java-efd5/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.