org.springframework.web.multipart.MultipartFile is an interface so firstly you are going to need to work with an implementation of this interface.

The only implementation that I can see for that interface that you can use out-of-the-box is org.springframework.web.multipart.commons.CommonsMultipartFile. The API for that implementation can be found here

Alternatively as org.springframework.web.multipart.MultipartFile is an interface, you could provide your own implementation and simply wrap your byte array. As a trivial example:

/*
*<p>
* Trivial implementation of the {@link MultipartFile} interface to wrap a byte[] decoded
* from a BASE64 encoded String
*</p>
*/
public class BASE64DecodedMultipartFile implements MultipartFile {
        private final byte[] imgContent;

        public BASE64DecodedMultipartFile(byte[] imgContent) {
            this.imgContent = imgContent;
        }

        @Override
        public String getName() {
            // TODO - implementation depends on your requirements 
            return null;
        }

        @Override
        public String getOriginalFilename() {
            // TODO - implementation depends on your requirements
            return null;
        }

        @Override
        public String getContentType() {
            // TODO - implementation depends on your requirements
            return null;
        }

        @Override
        public boolean isEmpty() {
            return imgContent == null || imgContent.length == 0;
        }

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

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

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

        @Override
        public void transferTo(File dest) throws IOException, IllegalStateException { 
            new FileOutputStream(dest).write(imgContent);
        }
    }
Answer from Rob Lockwood-Blake on Stack Overflow
🌐
Baeldung
baeldung.com › home › spring › spring web › convert byte[] to multipartfile in java
Convert byte[] to MultipartFile in Java | Baeldung
August 15, 2024 - We’ve successfully used the Spring-provided MockMultipartFile object to convert the byte array into a MultipartFile Object.
Top answer
1 of 2
69

org.springframework.web.multipart.MultipartFile is an interface so firstly you are going to need to work with an implementation of this interface.

The only implementation that I can see for that interface that you can use out-of-the-box is org.springframework.web.multipart.commons.CommonsMultipartFile. The API for that implementation can be found here

Alternatively as org.springframework.web.multipart.MultipartFile is an interface, you could provide your own implementation and simply wrap your byte array. As a trivial example:

/*
*<p>
* Trivial implementation of the {@link MultipartFile} interface to wrap a byte[] decoded
* from a BASE64 encoded String
*</p>
*/
public class BASE64DecodedMultipartFile implements MultipartFile {
        private final byte[] imgContent;

        public BASE64DecodedMultipartFile(byte[] imgContent) {
            this.imgContent = imgContent;
        }

        @Override
        public String getName() {
            // TODO - implementation depends on your requirements 
            return null;
        }

        @Override
        public String getOriginalFilename() {
            // TODO - implementation depends on your requirements
            return null;
        }

        @Override
        public String getContentType() {
            // TODO - implementation depends on your requirements
            return null;
        }

        @Override
        public boolean isEmpty() {
            return imgContent == null || imgContent.length == 0;
        }

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

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

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

        @Override
        public void transferTo(File dest) throws IOException, IllegalStateException { 
            new FileOutputStream(dest).write(imgContent);
        }
    }
2 of 2
13

This question has already been answered above. Recently i was working on the requirement to convert byte array object to multipartfile object. There are two ways to achieve this.

Approach 1:

Use the default CommonsMultipartFile where you to use the FileDiskItem object to create it.

Example:

FileItem fileItem = new DiskFileItem("fileData", "application/pdf",true, outputFile.getName(), 100000000, new java.io.File(System.getProperty("java.io.tmpdir")));              
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);

Approach 2:

Create your own custom multipart file object and convert the byte array to multipartfile.

public class CustomMultipartFile implements MultipartFile {

private final byte[] fileContent;

private String fileName;

private String contentType;

private File file;

private String destPath = System.getProperty("java.io.tmpdir");

private FileOutputStream fileOutputStream;

public CustomMultipartFile(byte[] fileData, String name) {
    this.fileContent = fileData;
    this.fileName = name;
    file = new File(destPath + fileName);

}

@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
    fileOutputStream = new FileOutputStream(dest);
    fileOutputStream.write(fileContent);
}

public void clearOutStreams() throws IOException {
if (null != fileOutputStream) {
        fileOutputStream.flush();
        fileOutputStream.close();
        file.deleteOnExit();
    }
}

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

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

This how you can use above CustomMultipartFile object.

String fileName = "intermediate.pdf";
CustomMultipartFile customMultipartFile = new CustomMultipartFile(bytea, fileName);
try {
customMultipartFile.transferTo(customMultipartFile.getFile());
        
} catch (IllegalStateException e) {
    log.info("IllegalStateException : " + e);
} catch (IOException e) {
    log.info("IOException : " + e);
}

This will create the required PDF and store that into

java.io.tmpdir with the name intermediate.pdf

Thanks.

🌐
Electro4u
electro4u.net › blog › convert-byte-to-multipartfile-in-java-1319
Convert Byte[] to MultipartFile in Java with electro4u.net
import org.springframework.mock.web.MockMultipartFile; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; public class ByteToMultipartFileConverter { public static MultipartFile convert(byte[] bytes, String fileName) throws IOException { // Assuming bytes is your byte[] array // Create a MockMultipartFile MultipartFile multipartFile = new MockMultipartFile( fileName, // Original file name fileName, // Desired file name "application/octet-stream", // Content type bytes // Byte[] array ); return multipartFile; } public static void main(String[] args) throws IOException { byte[] byteData = { /* Your byte data here */ }; String fileName = "example.txt"; MultipartFile multipartFile = convert(byteData, fileName); // Now you can use the multipartFile object in your code } }
🌐
YouTube
youtube.com › vlogize
How to Convert InputStream and Byte Array to MultipartFile in Java - YouTube
Disclaimer/Disclosure: Some of the content was synthetically produced using various Generative AI (artificial intelligence) tools; so, there may be inaccurac...
Published   July 5, 2024
Views   35
🌐
GitHub
gist.github.com › luanvuhlu › 9d833a832e7ffc2efb13775139d233b3
convert byte data to MultipartFile in Spring MVC · GitHub
convert byte data to MultipartFile in Spring MVC · Raw · BASE64DecodedMultipartFile.java · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
CodingTechRoom
codingtechroom.com › tutorial › java-how-to-convert-byte-array-to-multipartfile-in-java
Java Convert Byte Array To MultipartFile - CodingTechRoom
Q. Can I convert other types of data to MultipartFile? A. Yes, as long as you can represent the data as a byte array, you can convert it to MultipartFile using a similar approach.
Find elsewhere
🌐
Code Ease
codeease.net › programming › questions › how-to-convert-byte-array-to-multipartfile
How to convert byte array to MultipartFile | Code Ease
There are two ways to achieve this. ... Use the default CommonsMultipartFile where you to use the FileDiskItem object to create it. Example: ... Use the default CommonsMultipartFile where you to use the FileDiskItem object to create it.
🌐
Cacher
snippets.cacher.io › snippet › b641af03ef66ee4ce13e
convert byte data to MultipartFile in Spring MVC - Cacher Snippet
April 25, 2018 - import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.web.multipart.MultipartFile; public class BASE64DecodedMultipartFile implements MultipartFile { protected static final Logger log = LogManager.getLogger(BASE64DecodedMultipartFile.class); private byte[] imgContent; private String fileName; private String ext; public String getExt() { return ext; } @Override public String getName() { return
🌐
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
🌐
javathinking
javathinking.com › blog › convert-byte-to-multipartfile-in-java
Converting Byte to MultipartFile in Java — javathinking.com
Mocking File Uploads for Testing: When writing unit tests for controllers that handle file uploads, you can create a MultipartFile object from a byte array to simulate file uploads. Integrating with Third-Party APIs: Some third-party APIs may return file content as bytes. Converting these bytes to a MultipartFile can simplify the integration process by using existing Spring file handling code. The following is a Java code example that demonstrates how to convert a byte array to a MultipartFile object.
🌐
YouTube
youtube.com › hey delphi
Array : How to convert byte array to MultipartFile - YouTube
Array : How to convert byte array to MultipartFileTo Access My Live Chat Page, On Google, Search for "hows tech developer connect"I promised to reveal a secr...
Published   May 1, 2023
Views   1K
🌐
Javathinking
javathinking.com › blog › convert-bytes-to-multipartfile-java
Convert Bytes to MultipartFile in Java | JavaThinking.com
July 19, 2025 - Converting bytes to a MultipartFile in Java is a useful technique when dealing with file uploads and binary data. By creating a custom implementation of the MultipartFile interface, you can easily convert byte arrays to MultipartFile objects.
🌐
GitHub
github.com › OpenFeign › feign-form › blob › master › feign-form-spring › src › main › java › feign › form › spring › converter › ByteArrayMultipartFile.java
feign-form/feign-form-spring/src/main/java/feign/form/spring/converter/ByteArrayMultipartFile.java at master · OpenFeign/feign-form
December 31, 2024 - import java.io.InputStream; · import lombok.NonNull; import lombok.Value; import lombok.val; import org.springframework.web.multipart.MultipartFile; · /** * Straight-forward implementation of interface {@link MultipartFile} where the file · * data is held as a byte array in memory.
Author   OpenFeign
🌐
Programmersought
programmersought.com › article › 4396546673
Convert byte array to MultipartFile - Programmer Sought
byte[] pdfFile = new byte[1024]; InputStream inputStream = new ByteArrayInputStream(pdfFile); MultipartFile file = new MockMultipartFile("new file name","Original file name",ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);