Required request part ‘file‘ is not present
本人遇到问题是错误信息如下:org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not presentat org.springframework.web.method.annotation.RequestParamMethodAr
·
本人遇到问题是错误信息如下:
org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present
at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:199) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:114) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879) ~[spring-webmvc-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) ~[spring-webmvc-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at
前台Postman请求如图
headers:
Body:
后台接受方式:
@PostMapping(value = "/importData")
@Operation(method = "导入")
public String importData(@RequestParam("file") MultipartFile fileNew){
ResponseResult<Integer> responseResult = new ResponseResult<>();
return outJsonString(responseResult);
}
解决过程中,各种尝试,将参数换成字符串,只要使用了 @RequestParam
标签,都会抛出如上异常,因为赶工期,没深入研究,后来使用项目中的其他用例,曲线救国,获取fileNew的方式如下:
@PostMapping(value = "/importData)
@Operation(method = "导入")
public String importData(HttpServletRequest request){
ResponseResult<Integer> responseResult = new ResponseResult<>();
try {
MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(request);
MultipartFile fileNew = multipartRequest.getFile("file");
} catch (Exception e) {
responseResult.setSuccess(false);
e.printStackTrace();
}
return outJsonString(responseResult);
}
绕不过去的坑,始终要面对,后来简单写了个测试,发现 使用@RequestParam
标签可以接受参数,后来来才看见,数据放在了 body中, @RequestParam
是接收url参数中的数据,body需要使用 @RequestBody
接收。特此梳理。
更多推荐
已为社区贡献4条内容
所有评论(0)