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 › java › java io › 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.

🌐
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.
🌐
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
🌐
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 } }
🌐
Spring
docs.spring.io › spring-framework › docs › 5.0.8.RELEASE_to_5.1.0.RC1 › Spring Framework 5.1.0.RC1 › org › springframework › web › multipart › MultipartFile.html
MultipartFile
Return the contents of the file as an array of bytes. ... Return an InputStream to read the contents of the file from. The user is responsible for closing the returned stream. ... Return a Resource representation of this MultipartFile.
Find elsewhere
🌐
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 page › code › java › org.springframework.web.multipart.multipartfile
org.springframework.web.multipart.MultipartFile.getBytes java code examples | Tabnine
public MultipartFile readMultipartFile(MultipartFile multipartFile) throws IOException { return new UploadedMultipartFile(multipartFile.getBytes(), multipartFile.getContentType(), multipartFile.getName(), multipartFile.getOriginalFilename()); } Return the contents of the file as an array of bytes. ... Return the content type of the file. ... Transfer the received file to the given destination file.The default implementation simply copies th
🌐
Twitter
twitter.com › baeldung › status › 1652258944377651204
New Post: Convert byte[] to MultipartFile in Java
April 29, 2023 - JavaScript is not available · We’ve detected that JavaScript is disabled in this browser. Please enable JavaScript or switch to a supported browser to continue using twitter.com. You can see a list of supported browsers in our Help Center · Help Center · Terms of Service Privacy Policy ...
🌐
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
🌐
Morioh
morioh.com › p › 5e3a3ac598ca
How to Convert a Spring MultipartFile to a File
morioh.com is your first and best source for all of the information you’re looking for. From general topics to more of what you would expect to find here, morioh.com has it all. We hope you find what you are searching for!
🌐
Medium
medium.com › @dicksonadevotus › why-its-necessary-to-convert-a-multipartfile-to-a-byte-array-before-processing-it-asynchronously-db6cd1e4c1bc
Why it’s necessary to convert a MultipartFile to a byte array before processing it asynchronously in Spring | by Adeweb | Medium
May 31, 2025 - Here are two common ways can be use: ... Read the file’s content into a byte array while still in the main thread byte[] fileBytes = file.getBytes(); So instead, you pass the byte array to the asynchronous method.
🌐
Baeldung
baeldung.com › home › spring › spring web › converting a spring multipartfile to a file
Converting a Spring MultipartFile to a File | Baeldung
October 23, 2025 - MultipartFile has a getBytes() method that returns a byte array of the file’s contents.
🌐
Java Tips
javatips.net › api › org.springframework.web.multipart.multipartfile
Java Examples for org.springframework.web.multipart.MultipartFile
* @see org.springframework.web.multipart.MultipartFile */ public static byte[][] convert(final MultipartFile... files) throws IOException { if (files != null) { final List<Resource> resources = new ArrayList<Resource>(files.length); for (final MultipartFile file : files) { resources.add(new MultipartFileResourceAdapter(file)); } return convert(resources.toArray(new Resource[resources.size()])); } return new byte[0][]; }
🌐
Stack Overflow
stackoverflow.com › questions › linked › 18381928
Recently active linked questions - Stack Overflow
Firstly,I captured image using Phonegap 3.0 which gives me BASE64 encoded String and then I have converted BASE64 string into MultipartFile object using How to convert byte array to MultipartFile link....
🌐
CodingTechRoom
codingtechroom.com › tutorial › java-java-convert-byte-array-to-multipartfile
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.