AlejoDev
AlejoDev

Reputation: 3202

How to convert byte[ ] to CommonsMultipartFile

I have a following part of code in my Spring Boot App:

the base64str variable has a encode base 64 image,

byte[] data = Base64.decode(base64str);

But I need to convert this array byte to org.springframework.web.multipart.commons.CommonsMultipartFile

to then use this method (CommonsMultipartFile is an implementation of MultipartFile. That is why I used it).

AmazonClient.uploadFile(folder, MultipartFile)

Many thanks!

Upvotes: 1

Views: 7698

Answers (1)

ced-b
ced-b

Reputation: 4065

There could be a few solutions:

  1. I would check if there is not an overload to the uploadFile method that accepts something else than a MultipartFile or in case this is a custom class, maybe it could be modified, to accept something else.

  2. You can write the ByteArray to the file system using Files.write() and then use the CommonsMultipartFile as intended. Don't forget to clean up the file later on or use a temp file mechanism.

  3. It would actually be fairly easy to implement your own version of MultipartFile. The interface has only a few methods which can be easily created if you have a byte array.

  4. There is actually a MockMultipartFile which also implements MultipartFile and just takes a byte array in the constructor. This is a bit of a dirty hack as it is meant for testing purposes only.

Upvotes: 2

Related Questions