Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Convert FilePart to byte[] in Springboot using Webflux

I want to get the byte content of the File. Here is my controller:

@PostMapping("/files")
@ResponseStatus(HttpStatus.OK)
public List<UserFilesResponse> uploadFile(@RequestPart("file") FilePart file) {

// I want to get the bytes[] content of my file. How can I do it please ??
}
like image 941
user3648235 Avatar asked Apr 27 '26 00:04

user3648235


1 Answers

As M. Deinum mentioned, you should use content

I would also recommend https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/buffer/DataBufferUtils.html#join-org.reactivestreams.Publisher-

DataBufferUtils.join(filePart.content())
    .map(dataBuffer -> dataBuffer.asByteBuffer().array())
like image 81
Andrey Novikov Avatar answered Apr 28 '26 12:04

Andrey Novikov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!