Skip to content

Spring Boot Multipart Upload

DodaTech 1 min read

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

The Problem

File upload controller receives null for the MultipartFile parameter.

Quick Fix

Add MultipartFile parameter

Wrong:

@PostMapping("/upload")
public String upload(String file) { }

Output:

String instead of file

Right:

@PostMapping("/upload")
public String upload(@RequestParam("file") MultipartFile file) { }

Output:

Correct file parameter

Configure multipart properties

Wrong:

// Default 1MB max

Output:

Larger files rejected

Right:

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

Output:

Larger files accepted

Add enctype to form

Wrong:

<form action="/upload" method="post">
  <input type="file" name="file">
</form>

Output:

Missing enctype

Right:

<form action="/upload" method="post" enctype="multipart/form-data">
  <input type="file" name="file">
</form>

Output:

Correct form encoding

Prevention

  • Use @RequestParam MultipartFile for the file parameter
  • Configure max file size in properties
  • HTML form must have enctype=multipart/form-data

Common Mistakes with boot multipart upload

  1. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  2. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  3. Using return to exit a function early instead of wrapping a pure value in the monad

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 is MultipartFile null?

The form must use enctype=multipart/form-data and the input name must match the @RequestParam value.

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