2

我正在尝试在 spring boot 2.0 应用程序中使用新的响应式 web-mvc 实现。我正在尝试定义一种使用多部分文件但未能成功使其工作的方法:( - 我总是收到 415 错误。

一方面,我有一个包含以下请求映射的控制器:

@RequestMapping(method = RequestMethod.POST, path = "/myPath/{param}/{param2}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseBody
public Mono<Void> postFile(
        @RequestBody MultipartFile data,
        @PathVariable("param") String param,
        @PathVariable("param2") String param2,
        @RequestHeader(name = HEADER_DATE, required = false)   @DateTimeFormat(pattern = DATE_FORMAT) Instant instant
){
    return fileService.handleData(Mono.just(data), param, param2, instant);
}

另一方面,我不得不在基本依赖项之上添加一个服务器,因为 netty 似乎不处理多部分文件。我因此添加了使应用程序自动配置能够匹配和满足的spring-boot-starter-tomcat依赖项。MultipartAutoConfiguration

使用 curl 调用发布内容时: curl 'Meta-Date: 20170101104532' --form "file=@file.bin" http://localhost:8082/myPath/foo/bar 激活调试日志时(logging.level.org.springframework.web=DEBUG)我得到了这个异常: org.springframework.web.server.UnsupportedMediaTypeStatusException: Request failure [status: 415, reason: "Content type 'multipart/form-data;boundary=------------------------58fa43b8f1a26de4' not supported"]

RequestBodyArgumentResolver具有以下受支持的媒体类型的会引发此错误:[*/*, text/xml, application/*+json;charset=UTF-8, application/xml, text/plain;charset=UTF-8, application/x-www-form-urlencoded, application/json;charset=UTF-8]由 9 提供DecoderHttpMessageReader

在发布之前,我还看了看:

我的理解是 Spring web 5.0 使用了一个新的请求解码器系统,因为我在 spring 4 spring boot 应用程序上找不到这些类,并且还没有DecoderHttpMessageReader处理多部分文件我错过了什么吗?还是我应该等待一个实施?

4

2 回答 2

1
@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();
    }

请注意,这是一个开发版本,但这是我设法做到的。

于 2018-11-06T13:19:01.760 回答
1

好的,这似乎只是暂时没有实现,因为它目前存在此功能的拉取请求:添加反应式多部分请求支持 #1201

应该早点检查...

[编辑]:问题已解决并合并到 Spring master 分支。应该不再是问题。

于 2017-03-28T11:13:08.547 回答