Spring Boot multipart file upload server

Carvia Tech | September 11, 2020 | 2 min read | 266 views

In this tutorial we will develop simple spring boot 2.2 + Junit 5 + Kotlin based multi-part file upload server, and write mvc testcase for its validation.

You can download the source code for this project from Github location:

Gradle setup

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

plugins {
	id("org.springframework.boot") version "2.2.1.RELEASE"
	id("io.spring.dependency-management") version "1.0.8.RELEASE"
	kotlin("jvm") version "1.3.60"
	kotlin("plugin.spring") version "1.3.60"

group = "com.shunya"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {

dependencies {
	testImplementation("org.springframework.boot:spring-boot-starter-test") {
		exclude(group = "org.junit.vintage", module = "junit-vintage-engine")

tasks.withType<Test> {

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

Bootstrap App

Every Spring Boot app has an entry point that bootstraps the server, we will be using the below App.kt class that will start the Spring Container inside an embedded tomcat server.

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

class App

fun main(args: Array<String>) {

Fileupload Controller

The following controller will accept a multi-part file and just print the content of file for demonstration purpose.

import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.multipart.MultipartFile
import java.io.BufferedReader

class FileUploadController {

    val logger = LoggerFactory.getLogger(FileUploadController::class.java)

    fun handleFileUpload(@RequestParam("file") file: MultipartFile): ServiceResponse {
        logger.info("handling fileupload for {}", file.name)
        val content = file.inputStream.bufferedReader().use(BufferedReader::readText)
        logger.info("file content = {}", content)
        return ServiceResponse.ok(content)

data class ServiceResponse(val isSuccess: Boolean,
                           val data: String? = null) {

    companion object {
        fun ok(data: String): ServiceResponse {
            return ServiceResponse(isSuccess = true, data = data)

        fun fail(): ServiceResponse {
            return ServiceResponse(isSuccess = false)


We can configure file uplaod attributes like max upload size, request size etc. using Spring Boot properties file.


Running the application

We can start the server using the below gradle command from terminal.

$ ./gradlew :bootRun

Top articles in this category:
  1. Spring Webclient multipart file upload
  2. Multipart file upload with RestTemplate
  3. AWS SDK 2.0 - S3 File upload & download in Java
  4. SendGrid emails in Spring Boot
  5. Retrofit vs Feign for Server Side
  6. Sendgrid Dynamic Templates with Spring Boot
  7. SendGrid Attachments with Spring Boot

Find more on this topic:
Spring Framework image
Spring Framework

Spring Framework - MVC, Dependency Injection, Spring Hibernate, Spring Data JPA, Spring Boot and Spring Cloud for Microservices Architecture.

Last updated 1 week ago

Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info