0
I have a Bytearray type attribute (byte[]) and would like to convert it to a Multipartfile. After that I would like to take this multipartFile and send it to a function that will perform a request for another api. As the example below:
fun sendRequest(file: ByteArray) {
val multipartFile: MultipartFile = convert(file)
sendRequest(multipartFile)
}
fun convert(file: ByteArray): MultipartFile {
// convert and return multipartfile
}
@PostMapping(path = ["path"], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
fun sendRequest(
@RequestParam(name = "file") file: MultipartFile)
The problem is that I’ve taken a lot of examples, and what I’ve actually achieved is using MockMultipartFile
. But to me it seems more that it is used in tests and not a code running in production (correct me if I’m wrong). Does anyone know a way to convert this bytearray without using this class MockMultipartFile
?
Jonathan, there is no problem using Mockmultipartfile, it is a common Spring library and can be used without fear. If you really want to do this more manually take a look at Inputstream based on its Bytearray.
– Gabriel Muniz