Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this tutorial, we’ll take a look at how to convert a byte array to MultipartFile.

MutlipartFile is an interface provided by Spring to receive files in multiple request chunks, so we need some implementation to instantiate a MultipartFile object. Spring does not provide any default implementation for code, but it does provide one for testing purposes.

2. Implementing MultipartFile Interface

Let’s create our own implementation for the MultipartFile interface and wrap the input byte array:

public class CustomMultipartFile implements MultipartFile {

    private byte[] input;

    @Override
    public String getName() {
        return null;
    }

    @Override
    public String getOriginalFilename() {
        return null;
    }

    @Override
    public String getContentType() {
        return null;
    }
    //We've defined the rest of the interface methods in the next snippet
}

We’ve defined a byte array attribute in our class so that we can capture the value for the input. Additionally, we’ve overridden the methods from the interface above, which depend on implementation detail. Therefore the details about the file name or content type can be provided as custom logic. As a result, we’ve returned null here.

We’ve also provided our own implementation for the other required methods from the interface:

public class CustomMultipartFile implements MultipartFile {

    //previous methods
    @Override
    public boolean isEmpty() {
        return input == null || input.length == 0;
    }

    @Override
    public long getSize() {
        return input.length;
    }

    @Override
    public byte[] getBytes() throws IOException {
        return input;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(input);
    }

    @Override
    public void transferTo(File destination) throws IOException, IllegalStateException {
        try(FileOutputStream fos = new FileOutputStream(destination)) {
            fos.write(input);
        }
    }
}

These methods may not require any custom logic, so we’ve defined them in our class. There are several different ways to implement the transferTo(File destination) method. Let’s take a look at a few of them below:

We can use Java NIO:

@Override
public void transferTo(File destination) throws IOException, IllegalStateException {
    Path path = Paths.get(destination.getPath());
    Files.write(path, input);
}

Another option is adding the Apache commons IO dependency to our POM and using FileUtils class:

@Override
public void transferTo(File destination) throws IOException, IllegalStateException {
    FileUtils.writeByteArrayToFile(destination, input);
}

The transferTo(File destination) method is useful when the MultipartFile only needs to be written to a File, and there are several ways to write a MultipartFile to File.

Now that we’ve defined our class, let’s test this implementation with a small test case:

@Test
void whenProvidingByteArray_thenMultipartFileCreated() throws IOException {
    byte[] inputArray = "Test String".getBytes();
    CustomMultipartFile customMultipartFile = new CustomMultipartFile(inputArray);
    Assertions.assertFalse(customMultipartFile.isEmpty());
    Assertions.assertArrayEquals(inputArray, customMultipartFile.getBytes());
    Assertions.assertEquals(inputArray.length,customMultipartFile.getSize());
}

We’ve successfully converted our byte array to a MultipartFile instance in the test case above.

3. MockMultipartFile

Spring provides MockMultipartFile out of the box for testing purposes to access Multipart requests.

Let’s create a test to see how it works:

@Test
void whenProvidingByteArray_thenMockMultipartFileCreated() throws IOException {
    byte[] inputArray = "Test String".getBytes();
    MockMultipartFile mockMultipartFile = new MockMultipartFile("tempFileName",inputArray);
    Assertions.assertFalse(mockMultipartFile.isEmpty());
    Assertions.assertArrayEquals(inputArray, mockMultipartFile.getBytes());
    Assertions.assertEquals(inputArray.length,mockMultipartFile.getSize());
}

We’ve successfully used the Spring-provided MockMultipartFile object to convert the byte array into a MultipartFile Object.

4. Conclusion

In this tutorial, we covered how to convert a byte array into a MultipartFile object.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW