Manish Bansal
Manish Bansal

Reputation: 889

convert filepart to byteArray

In my spring application, I am getting FilePart object and need to convert it to byteArray.

Below is my code.

val byteArray: ByteArray = file.content().map { it -> it.asInputStream().readBytes() }.blockLast()!!

But it is giving me error as

java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-kqueue-3

Upvotes: 1

Views: 2928

Answers (1)

Dmitry Kokora
Dmitry Kokora

Reputation: 837

Take a look at DataBufferUtils, the join(...) method offers a safe and efficient way to aggregate a data buffer stream into a single data buffer

Mono<byte[]> getByteArray(FilePart filePart) {
    return DataBufferUtils.join(filePart.content())
                          .map(dataBuffer -> dataBuffer.asByteBuffer().array());
}

Upvotes: 3

Related Questions