我正在尝试在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]
由DecoderHttpMessageReader
提供。
在发布之前,我还看了一下:
MultipartAutoConfiguration#multipartResolver matched
Content-Transfer-Encoding: binary
并没有改变任何内容。我的理解是Spring Web 5.0使用了一个新的请求解码器系统,因为我在spring 4 spring引导应用程序中找不到这些类,并且还没有任何DecoderHttpMessageReader
处理multipart文件
我错过了什么 ?或者我应该等一个实施?
答案 0 :(得分:1)
好的,这似乎暂时没有实现,因为它目前存在此功能的拉取请求:Add reactive multipart request support #1201
应该先检查一下......
[编辑]:问题已经解决并合并到Spring master分支中。应该不再是问题了。
答案 1 :(得分:1)
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
int main(){
int size = 20;
int i = 0;
string responses[size];
string categories[size];
string line1, line2;
ifstream infile("responses.txt");
if(infile.is_open())
{
//input
while(getline(infile, line1) && getline(infile, line2))
{
//cout << line1 << endl;
//cout << line2 << endl
getline(infile, line1) >> responses[i];
getline(infile, line2) >> categories[i];
cout << responses[i] << endl;
cout << categories[i] << endl;
++i;
}
infile.close();
}else
{
cout << "File not open" << '\n';
}
return 0;
}
请注意,这是一个开发版,但这就是我设法做到的方式。