Kotlin + Spring Webflux File Download - REST API Example

In this section, we will show you how to download files in a RESTful Kotlin + Spring WebFlux application.

More Spring WebFlux practice:


Technologies used:

  • Kotlin
  • Spring Boot 2.5.4
  • Spring Webflux
  • Java 11
  • Gradle


Project Strcuture:



Project Dependency Management(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.GetMapping
import kotlin.Throws
import java.io.IOException
import org.springframework.web.bind.annotation.PathVariable
import reactor.core.publisher.Mono
import java.lang.Void
import org.springframework.http.ZeroCopyHttpOutputMessage
import org.springframework.core.io.ClassPathResource
import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType
import org.springframework.http.server.reactive.ServerHttpResponse

@RestController
class DownloadFile {

@GetMapping("/download/{fileName}")
@Throws(IOException::class)
fun downloadFile(
@PathVariable fileName: String,
response: ServerHttpResponse
): Mono<Void> {
val zeroCopyResponse = response as ZeroCopyHttpOutputMessage
response.getHeaders()[HttpHeaders.CONTENT_DISPOSITION] =
"attachment; filename=$fileName"
response.getHeaders().contentType = MediaType.APPLICATION_OCTET_STREAM
val resource = ClassPathResource(fileName)
val file = resource.file
return zeroCopyResponse.writeWith(
file, 0,
file.length()
)
}
}



Spring Boot Main Driver

package com.knf.dev.demo

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

@SpringBootApplication
class KotlinspringwebfluxdownloadfileApplication

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

Run the application

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