DEV Community

Munaf Badarpura
Munaf Badarpura

Posted on

Spring Boot File Upload with Multipart Support: Complete Guide

File uploading is a key feature in many web applications, whether for user profile images, document uploads, or media files. Spring Boot simplifies this process with its robust support for handling multipart file uploads.

In this blog, we'll explore how to implement file uploads in a Spring Boot application using multipart support, including a REST controller, service layer, and database integration. We'll also discuss different file upload approaches and why multipart is often the preferred method.

Different Ways of File Uploading

There are several approaches to handle file uploads in Spring Boot, each suited for specific use cases:

  1. Multipart Form Data:
    • Uses the MultipartFile interface to handle files sent via HTTP POST requests.
    • Ideal for web forms where users upload files alongside other form data (e.g., name, description).
    • Example: Uploading a profile picture with metadata like username and file type.
  2. Base64 Encoded Files:
    • Files are encoded as Base64 strings and sent in the request body (e.g., as JSON).
    • Useful for APIs where files are embedded in payloads, but increases payload size due to encoding.
    • Drawback: Less efficient for large files due to encoding overhead.
  3. Direct Stream Upload:
    • Files are streamed directly to a server or storage service (e.g., Amazon S3) without temporary storage.
    • Best for large files or high-performance applications.
    • Requires integration with external storage services.
  4. Servlet-based Upload:
    • Uses HttpServletRequest to access raw input streams for file data.
    • More manual and less abstracted than MultipartFile, but offers fine-grained control.
    • Suitable for custom or legacy applications.

In this blog, we'll focus on the multipart form data approach due to its simplicity and widespread use in web applications.

Why Use Multipart?

Multipart form data is the go-to choice for file uploads in Spring Boot for several reasons:

  • Efficient File Handling: Spring Boot's MultipartFile interface abstracts the complexity of handling file streams, making it easy to process uploaded files.
  • Support for Metadata: Multipart requests allow sending files alongside additional data (e.g., file name, type, or description) in a single request.
  • Browser Compatibility: HTML forms natively support multipart form data, making it ideal for web applications with file upload forms.
  • Scalability: Multipart uploads can be integrated with cloud storage services like Amazon S3, allowing scalable file storage.
  • Built-in Support: Spring Boot provides out-of-the-box support for multipart uploads via annotations like @ModelAttribute and configuration properties for file size limits.

Implementation: File Upload with Multipart Support

1. Add the following dependencies in your pom.xml :

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

2. Create the File Upload Request Model :

@Data
public class FileUploadRequest {
    private String name;
    private String type;
    private MultipartFile image;
}
Enter fullscreen mode Exit fullscreen mode

3. Define the Entity for File Metadata :

@Entity
@Data
public class FileUploading {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String type;
    private String imageURL;
}
Enter fullscreen mode Exit fullscreen mode

4. Implement the File Upload Service :

@Service
public class FileUploadingService {
    private final FileUploadingRepository fileUploadingRepository;

    public FileUploadingService(FileUploadingRepository fileUploadingRepository) {
        this.fileUploadingRepository = fileUploadingRepository;
    }

    public FileUploading uploadImage(FileUploadRequest fileUploadRequest) {
        MultipartFile image = fileUploadRequest.getImage();
        String fileName = image.getOriginalFilename();
        // Simulate S3 upload (replace with actual S3 logic)
        String imageUrl = "https://amazon/images/url/" + fileName;
        FileUploading fileUploading = new FileUploading();
        fileUploading.setName(fileUploadRequest.getName());
        fileUploading.setType(fileUploadRequest.getType());
        fileUploading.setImageURL(imageUrl);
        return fileUploadingRepository.save(fileUploading);
    }
}
Enter fullscreen mode Exit fullscreen mode

For real S3 integration, use the AWS SDK:

AmazonS3 s3Client = AmazonS3ClientBuilder.standard().build();
s3Client.putObject("your-bucket-name", fileName, image.getInputStream(), new ObjectMetadata());
String imageUrl = s3Client.getUrl("your-bucket-name", fileName).toString();
Enter fullscreen mode Exit fullscreen mode

5. Create the REST Controller ****:

@RestController
@RequestMapping("/file-uploading")
public class FileUploadingController {
    private final FileUploadingService fileUploadingService;

    public FileUploadingController(FileUploadingService fileUploadingService) {
        this.fileUploadingService = fileUploadingService;
    }

    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public FileUploading uploadImage(@ModelAttribute FileUploadRequest fileUploadRequest) {
        return fileUploadingService.uploadImage(fileUploadRequest);
    }
}
Enter fullscreen mode Exit fullscreen mode

6. Configure File Upload Limits :

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
Enter fullscreen mode Exit fullscreen mode

Read complete blog here : https://www.codingshuttle.com/blogs/spring-boot-caching-with-redis-boost-performance-with-fast-operations-2025/

Top comments (0)