Spring webflux MultipartFile方式上传文件报错

2025-10-09 15:49:00

1、以下是错误代码:

@PostMapping( consumes = MediaType.MULTIPART_FORM_DATA_VALUE ) 

public Mono<Void> save(@RequestPart("file")MultipartFile file) {    

    log.info("Storing a new file. Recieved by Controller");    

    this.storageService.store(file);//保存文件的方法,请自己实现

    return Mono.empty(); 

}

2、上述代码会报错如下:

org.springframework.web.server.UnsupportedMediaTypeStatusException: Response status 415 with reason "Content type 'image/png' not supported" 

3、正确的方式是:使用@RequestPart("file") Mono<FilePart> file或者@RequestPart("file") Flux<FilePart>替换@RequestPart("file") MultipartFile file

如下为正确代码:

@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)    

public Mono<Void> save(@RequestPart("file") Mono<FilePart> file) {

    log.info("Storing a new file. Recieved by Controller");        

    this.storageService.store(file);  //保存文件的方法,请自己实现     

    return Mono.empty();    

}

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢