Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

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.

As usual, all code examples can be found over on GitHub.

Spring bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> THE COURSE
Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
guest
0 Comments
Inline Feedbacks
View all comments