Kotlin SpringBoot Upload/Download File – MultipartFile + Thymeleaf + Bootstrap 4 example

In the tutorial, we will show you how to build a SpringBoot web-application to upload/download file with Thymeleaf engine and Bootstrap 4 using Kotlin language.

Technologies

  • Java 8
  • Maven 3.6.1
  • Spring Tool Suite: Version 3.9.4.RELEASE
  • Spring Boot: 2.0.2.RELEASE
  • Bootstrap 4
  • Kotlin – Version: 1.2.41

Goal

We create a SpringBoot project as below:

Kotlin-SpringBoot-Upload-Download-File-MultipartFile-Thymeleaf-Bootstrap-4-project-structure

-> Upload Form:

Kotlin-SpringBoot-Upload-Download-File-MultipartFile-Thymeleaf-Bootstrap-4-upload-view

-> Download Form:

Kotlin-SpringBoot-Upload-Download-File-MultipartFile-Thymeleaf-Bootstrap-4-download-files

Practice

We create a SpringBoot project with below dependencies:

Frontend
Upload Multipart-Form

– Create /templates/multipartfile/uploadform.html file:

Retrieves Files

– Create /templates/multipartfile/listfiles.html file:

Kotlin Backend
File Storage

– Create Kotlin interface FileStorage.kt file:

– Implement above interface by class FileStorageImpl.kt:

Upload/Download File Controller

– Firstly, create a simple data model FileInfo.kt that contains information of a file:

– Implement controller UploadFileController.kt:

– Implement controller DownloadFileController.kt:

Note:
We can configure multipart.max-file in application.properties to limit the size of uploaded file:

Main Class

Init storage folder in main class KotlinSpringBootUploadDownloadFilesApplication.kt:

Run & Check Results

Run the SpringBoot project, then makes upload/download requests ->

Upload File

Kotlin-SpringBoot-Upload-Download-File-MultipartFile-Thymeleaf-Bootstrap-4-upload-view

-> Browser Network logs:

Kotlin-SpringBoot-Upload-Download-File-MultipartFile-Thymeleaf-Bootstrap-4-upload-request

-> Stored Files:

Kotlin-SpringBoot-Upload-Download-File-MultipartFile-Thymeleaf-Bootstrap-4-result-uploadedfile

Retrieve Files

Kotlin-SpringBoot-Upload-Download-File-MultipartFile-Thymeleaf-Bootstrap-4-retrieve-files

Download Files

Kotlin-SpringBoot-Upload-Download-File-MultipartFile-Thymeleaf-Bootstrap-4-download-files

-> Browser Network logs:

Kotlin-SpringBoot-Upload-Download-File-MultipartFile-Thymeleaf-Bootstrap-4-download-file-logs

SourceCode

KotlinSpringBootUploadDownloadFiles

By JavaSampleApproach | June 7, 2018.

Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*