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 {
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.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

@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.demo

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

@SpringBootApplication
class KotlinspringwebfluxfileuploaddownloadApplication

fun main(args: Array<String>) {
runApplication<KotlinspringwebfluxfileuploaddownloadApplication>(*args)
}


Run

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

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

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete