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.

Prerequisites

  • 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:

build.gradle
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 {
    mavenCentral()
}

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 {
    useJUnitPlatform()
}

Spring 5 Webclient

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

ReactiveFileUploadClient.java
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;

@RestController
public class ReactiveFileUploadClient {

    @Autowired
    private WebClient.Builder webClientBuilder; (1)

    @GetMapping(value = "/client")
    public void upload() {
        final WebClient webClient = webClientBuilder.build();
        webClient.post()
                .uri("http://localhost:8080/upload")
                .contentType(MediaType.MULTIPART_FORM_DATA)
                .body(BodyInserters.fromMultipartData(fromFile(new File("HELP.md"))))
                .retrieve()
                .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. AWS Java SDK 2 - S3 File upload & download
  4. Download a file using Spring RestTemplate
  5. Disable SSL verification in Spring WebClient
  6. Spring Boot WebClient Basic Authentication
  7. Send Gupshup SMS using Java API

Find more on this topic:

Recommended books for interview preparation:
Buy interview books

Java & Microservices interview refresher for experienced developers.