Kotlin + Spring Webflux File Download - REST API Example

In this article, We will learn 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 Structure:



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


Main Class
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)
}


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