我正在尝试使用带有以下代码的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]
添加回答
举报
0/150
提交
取消