问题描述:
org.springframework.web.multipart.MultipartException: Current request is not a multipart request
问题分析:
1、后端接口参数为MultipartFile[] files支持上传多个文件时,使用Swagger调试报错。
解决办法:使用PostMan调试。
2、后端接口请求方式为Get请求,Content-Type为application/json,导致请求接口报错。
解决办法:将接口的请求方式改为POST即可。
3、本地编写单元测试调用上传接口附件,传参放入了body里面导致报错。
上传接口:
@ApiOperation(value = "初始化上传单个附件")
@PostMapping("/initUploadSingeAttachment")
public Result<String> initUploadSingeAttachment(@RequestParam("file") MultipartFile file){
}
单元测试代码:
private String uploadAttachment(File attachment) {
Map<String, Object> map = new HashMap<>();
map.put("file", attachment);
String url = "/initUploadSingeAttachment";
HttpResponse response = HttpUtil.createPost(url)
.header("Content-Type", MediaType.APPLICATION_JSON_VALUE)
.header("authorization", "Bearer 200cfc5a-f5a6-4120-8134-5630eb701b90")
.body(JSONObject.toJSONString(map)).execute();
JSONObject body = JSONObject.parseObject(response.body());
return (String) body.get("datas");
}
解决办法:
将参数放入form里面。
private String uploadAttachment(File attachment) {
Map<String, Object> map = new HashMap<>();
map.put("file", attachment);
String url = "/initUploadSingeAttachment";
HttpResponse response = HttpUtil.createPost(url)
.header("Content-Type", MediaType.APPLICATION_JSON_VALUE)
.header("authorization", "Bearer 200cfc5a-f5a6-4120-8134-5630eb701b90")
.form(map).execute();
JSONObject body = JSONObject.parseObject(response.body());
return (String) body.get("datas");
}