我正在嘗試使用帶有以下代碼的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]
使用Spring Rest模板+ Spring Web MVC分段上傳文件
慕的地8271018
2019-12-27 15:36:04