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.KotlinCompileplugins {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_11repositories {mavenCentral()}dependencies {implementation("org.springframework.boot:spring-boot-starter-webflux")implementation("com.fasterxml.jackson.module:jackson-module-kotlin")implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")implementation("org.jetbrains.kotlin:kotlin-reflect")implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")testImplementation("org.springframework.boot:spring-boot-starter-test")testImplementation("io.projectreactor:reactor-test")}tasks.withType<KotlinCompile> {kotlinOptions {freeCompilerArgs = listOf("-Xjsr305=strict")jvmTarget = "11"}}tasks.withType<Test> {useJUnitPlatform()}
Rest Controller
package com.knf.dev.demo.controllerimport org.springframework.web.bind.annotation.RestControllerimport org.springframework.web.bind.annotation.RequestMappingimport org.springframework.web.bind.annotation.PostMappingimport org.springframework.web.bind.annotation.RequestPartimport reactor.core.publisher.Monoimport org.springframework.http.codec.multipart.FilePartimport java.lang.Voidimport reactor.core.publisher.Fluximport java.nio.file.Paths@RestController@RequestMapping("api/v1")class UploadDownloadController {private val basePath = Paths.get("./src/main/resources/files/")//single file upload@PostMapping("single/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())) }.then()}//multiple file upload@PostMapping("multi/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())) }.then()}}
Main Class
package com.knf.dev.demoimport org.springframework.boot.autoconfigure.SpringBootApplicationimport org.springframework.boot.runApplication@SpringBootApplicationclass KotlinspringwebfluxfileuploaddownloadApplicationfun main(args: Array<String>) {runApplication<KotlinspringwebfluxfileuploaddownloadApplication>(*args)}
Run
Start Spring Boot with the default embedded Tomcat gradle bootRun.
Multiple file upload: