为了账号安全,请及时绑定邮箱和手机立即绑定

使用Spring Rest模板+ Spring Web MVC分段上传文件

使用Spring Rest模板+ Spring Web MVC分段上传文件

慕的地8271018 2019-12-27 15:36:04
我正在尝试使用带有以下代码的RestTemplate上传文件。   MultiValueMap<String, Object> multipartMap = new LinkedMultiValueMap<>();   multipartMap.add("file", new ClassPathResource(file));   HttpHeaders headers = new HttpHeaders();   headers.setContentType(new MediaType("multipart", "form-data"));   HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<MultiValueMap<String, Object>>(multipartMap, headers);   System.out.println("Request for File Upload : " + request);   ResponseEntity<byte[]> result = template.get().exchange(                    contextPath.get() + path, HttpMethod.POST, request,                    byte[].class);我有MultipartResolverbean,控制器代码是@RequestMapping(value = "/{id}/image", method = RequestMethod.POST)@ResponseStatus(HttpStatus.NO_CONTENT)@Transactional(rollbackFor = Exception.class)public byte[] setImage(@PathVariable("id") Long userId,        @RequestParam("file") MultipartFile file) throws IOException {    // Upload logic}我得到以下异常 org.springframework.web.bind.MissingServletRequestParameterException: Required MultipartFile parameter 'file' is not present        at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:255) ~[spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]        at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:95) ~[spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]        at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:79) ~[spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]        at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157) [spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
查看完整描述

3 回答

  • 3 回答
  • 0 关注
  • 701 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信