Kotlin + Spring Webflux File Upload - Single&Multiple - REST API Example

This article shows you how to upload single|multiple files in the Kotlin + Spring Webflux application. 

More Spring WebFlux practice:
Technologies Used:
  • Kotlin
  • Spring Boot 2.5.4
  • Spring Webflux
  • Java 11
  • Gradle

Project Structure:

Project Dependency(build.gradle.kts)

Spring boot dependencies, no need for an extra library for file upload.

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("org.springframework.boot") version "2.5.4"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
kotlin("jvm") version "1.5.21"
kotlin("plugin.spring") version "1.5.21"

group = "com.knf.dev.demo"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {

dependencies {

tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"

tasks.withType<Test> {

Rest Controller
package com.knf.dev.demo.controller

import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestPart
import reactor.core.publisher.Mono
import org.springframework.http.codec.multipart.FilePart
import java.lang.Void
import reactor.core.publisher.Flux
import java.nio.file.Paths

class UploadDownloadController {
private val basePath = Paths.

//single file upload
fun uploadFile(@RequestPart("file") filePartMono:
Mono<FilePart>): Mono<Void> {
return filePartMono
.doOnNext { fp: FilePart ->
println("Received File : " + fp.filename()) }
.flatMap { fp: FilePart -> fp.
transferTo(basePath.resolve(fp.filename())) }

//multiple file upload
fun uploadMultipleFiles(@RequestPart("files") partFlux:
Flux<FilePart>): Mono<Void> {
return partFlux
.doOnNext { fp: FilePart ->
println(fp.filename()) }
.flatMap { fp: FilePart -> fp.
transferTo(basePath.resolve(fp.filename())) }

Main Class
package com.knf.dev.demo

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

class KotlinspringwebfluxfileuploaddownloadApplication

fun main(args: Array<String>) {


Start Spring Boot with the default embedded Tomcat gradle bootRun.

API testing using postman:

Single file upload:

Multiple file upload:

Popular posts from this blog

Spring boot video streaming example-HTML5

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Spring Boot + Mockito simple application with 100% code coverage

Spring Boot + OpenCSV Export Data to CSV Example

Custom Exception Handling in Quarkus REST API

DataTable-Pagination example with Spring boot, jQuery and ajax

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Node JS mini projects with source code - free download

Spring boot web project free download:User Registration System

Java - Blowfish Encryption and decryption Example