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

前端 未结 2 1389
北海茫月
北海茫月 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

    Okay, It seems this is just not implemented for now as it currently exists a pull request for this feature : Add reactive multipart request support #1201

    Should have check this earlier...

    [EDIT] : The issue has been solved and merged into Spring master branch. Should no longer be an issue.

    0 讨论(0)
  • 2021-01-14 23:50
    @PutMapping(value="/{..}",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Mono<Void> save(@RequestPart("file") FilePart multipartFormData,@RequestParam("fileName") String fileName,@PathVariable("..") String ..) throws IOException {        
            List<ByteBuffer> 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.

    0 讨论(0)
提交回复
热议问题