How to transform byte array to a MultipartFile resource in Java?

How to transform byte array to a MultipartFile resource in Java?
Photo by Andrey Metelev / Unsplash

Let's assume you have a backend api and it accept MultipartFile as the request body. How can the client send a MultipartFile object in its body if client only has the bytes rather than a Resource?

implement MultipartFile interface

A representation of an uploaded file received in a multipart request.
The file contents are either stored in memory or temporarily on disk. In either case, the user is responsible for copying file contents to a session-level or persistent store as and if desired. The temporary storage will be cleared at the end of request processing.

The below sample code implement a PDF MultipartFile.

public class PdfMultipartFile implements MultipartFile {
    private byte[] input;
    private String name;

    public PdfMultipartFile(byte[] input, String name) {
        this.input = input;
        this.name = name;
    }

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

    @Override
    public String getOriginalFilename() {
        return name + ".pdf";
    }

    @Override
    public String getContentType() {
        return MediaType.APPLICATION_PDF_VALUE;
    }

    @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);
        }
    }

}

MultipartFile.#transferTo(java.io.File)

Transfer the received file to the given destination file.
This may either move the file in the filesystem, copy the file in the filesystem, or save memory-held contents to the destination file. If the destination file already exists, it will be deleted first.
If the target file has been moved in the filesystem, this operation cannot be invoked again afterwards. Therefore, call this method just once in order to work with any storage mechanism.
NOTE: Depending on the underlying provider, temporary storage may be container-dependent, including the base directory for relative destinations specified here (e.g. with Servlet multipart handling). For absolute destinations, the target file may get renamed/moved from its temporary location or newly copied, even if a temporary copy already exists.

The good part with MultipartFile is you can verify the bytes your received or send out is the exactly file you want to transmit by transfer the bytes to a MultipartFile instance. with the help of MultipartFile::transferTo method, you can save the bytes into a file.

Subscribe to Post, Code and Quiet Time.

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe