Convert Bytearray to Multipartfile without using the Mockmultipartfile class

Asked

Viewed 32 times

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 ?

  • 1

    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.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.