Spring Webclient multipart file upload

Carvia Tech | September 12, 2020 | 2 min read | 16 views

In this article we will learn how to use Spring 5 WebClient for multipart file upload to a remote file upload server.


  • Spring Boot 2.3.3

  • IDE - IntelliJ or Eclipse

  • Java 8/11

  • File upload server refer to this post

Upload server setup

Make sure you have a file upload server up and running. Follow instructions from this article.

Gradle setup

We will create a Spring Boot project using https://start.spring.io/ and edit the build.gradle with the following changes:

plugins {
    id 'org.springframework.boot' version '2.3.3.RELEASE'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'

group = 'com.carvia'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    testImplementation 'io.projectreactor:reactor-test'

test {

Spring 5 Webclient

Now we will create a WebClient based client that will take a file and upload it to remote service.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.MediaType;
import org.springframework.http.client.MultipartBodyBuilder;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;

import java.io.File;

public class ReactiveFileUploadClient {

    private WebClient.Builder webClientBuilder; (1)

    @GetMapping(value = "/client")
    public void upload() {
        final WebClient webClient = webClientBuilder.build();
                .body(BodyInserters.fromMultipartData(fromFile(new File("HELP.md"))))
                .bodyToMono(String.class)   (2)
                .block();   (3)

    public MultiValueMap<String, HttpEntity<?>> fromFile(File file) {
        MultipartBodyBuilder builder = new MultipartBodyBuilder();
        builder.part("file", new FileSystemResource(file));
        return builder.build();
1 Spring Boot auto configures a WebClientBuilder instance for us.
2 Our upload server returns a string response success
3 We are blocking the current thread till we receive the response from server. We can choose not to do so and move forward.

Using RestTemplate multipart file upload

Top articles in this category:
  1. Spring Boot multipart file upload server
  2. Multipart file upload with RestTemplate
  3. Disable SSL verification in Spring WebClient
  4. AWS SDK 2.0 - S3 File upload & download in Java
  5. Spring Boot WebClient Basic Authentication
  6. Send Gupshup SMS using Java API
  7. SendGrid Attachments with Spring Boot

Find more on this topic:
Spring Framework image
Spring Framework

Spring Framework - MVC, Dependency Injection, Spring Hibernate, Spring Data JPA, Spring Boot and Spring Cloud for Microservices Architecture.

Last updated 1 week ago

Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info