How to Convert a Spring MultipartFile to a File

How to Convert a Spring MultipartFile to a File

Learn how to Convert a Spring MultipartFile to a File. We'll cover various ways of converting a Spring MultipartFile to a File.

1. Overview

In this quick tutorial, we'll cover various ways of converting a Spring MultipartFile to a File.

2. MultipartFile#getBytes

MultipartFile has a getBytes() method that returns a byte array of the file's contents. We can use this method to write the bytes to a file:

MultipartFile multipartFile = new MockMultipartFile("sourceFile.tmp", "Hello World".getBytes());

File file = new File("src/main/resources/targetFile.tmp");

try (OutputStream os = new FileOutputStream(file)) {
    os.write(multipartFile.getBytes());
}

assertThat(FileUtils.readFileToString(new File("src/main/resources/targetFile.tmp"), "UTF-8"))
  .isEqualTo("Hello World");

The getBytes() method is useful for instances where we want to perform additional operations on the file before writing to disk, like computing a file hash.

3. MultipartFile#getInputStream

Next, let's look at MultipartFile_‘s _getInputStream() method:

MultipartFile multipartFile = new MockMultipartFile("sourceFile.tmp", "Hello World".getBytes());

InputStream initialStream = multipartFile.getInputStream();
byte[] buffer = new byte[initialStream.available()];
initialStream.read(buffer);

File targetFile = new File("src/main/resources/targetFile.tmp");

try (OutputStream outStream = new FileOutputStream(targetFile)) {
    outStream.write(buffer);
}

assertThat(FileUtils.readFileToString(new File("src/main/resources/targetFile.tmp"), "UTF-8"))
  .isEqualTo("Hello World");

Here we're using the getInputStream() method to get the InputStream, read the bytes from the InputStream, and store them in the byte[] buffer. Then we create a File and OutputStream to write the buffer contents.

The getInputStream() approach is useful in instances where we need to wrap the InputStream in another InputStream, say for example a GZipInputStream if the uploaded file was gzipped.

Spring Spring Web MultipartFile

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Spring Web Flux UI with Spring Security

Take a look at how to construct an application using Spring Web Flux and Thymeleaf, with Spring security, a gateway services, and other microservices.

whatsapp web-w app web-webs whatsapp »

whatsapp web-w app web-webs whatsapp-web.whatsapp.com-wsp web-web.whatsapp.com qr-whats up online-whatsappwebsite

Set up Web App with Spring Boot and Spring Security

Download the Spring Boot Web App Example Project. Run the Initial Web App. Add Project Dependencies for Your Spring Boot + Spring Security Web App. Understand Your Spring Boot App. Set Up Okta for OAuth 2.0 Single Sign-On. Configure Your Spring Boot App for Single SignOn (SSO) Refine Our Permissions

Spring Web MVC Tutorial | Spring MVC Tutorial | Spring Tutorial

Spring web MVC Tutorial will help you understand the process of integration of MVC with the help of hands-on.

Learn Spring Framework and Spring Boot - Build a Spring Web Application

Learn Spring Framework and Spring Boot - Build a Spring Web Application: If you're new to the Spring Framework, this is the course you want to start with. This course covers the core of the Spring Framework, the foundation which all of the other Spring Framework projects are built from.