How to enable Spring Reactive Web MVC to handle Multipart-file?

前端 未结 2 1391
北海茫月
北海茫月 2021-01-14 23:02

I\'m trying to use the new reactive web-mvc implementation in a spring boot 2.0 application. I\'m trying to define a method which consume multipart file but do not succeed a

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-14 23:50

    @PutMapping(value="/{..}",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Mono save(@RequestPart("file") FilePart multipartFormData,@RequestParam("fileName") String fileName,@PathVariable("..") String ..) throws IOException {        
            List bytesList = new LinkedList<>();
    
            multipartFormData.content().
              subscribe(item->bytesList.add(item.asByteBuffer()));
    
            int totalBytes = bytesList.stream().mapToInt(item->item.capacity()).sum();
    
            ByteBuffer buffer =  ByteBuffer.allocate(totalBytes);
            bytesList.stream().forEach(byteBuff->buffer.put(byteBuff));
            baseImageHandler.saveImage(buffer, fileName, baseItemId);
            return Mono.empty();
        }
    

    Please note that it is a dev verison, but this is how I have managed to do it.

提交回复
热议问题