Spring Webclient multipart / form-data请求

时间:2020-03-23 22:22:29

标签: spring-boot multipartform-data spring-webflux pre-signed-url spring-webclient

我是Java(Spring Boot)的新手,我正尝试向s3发送constructor(private changeDetectorRef: ChangeDetectorRef) {} openPopUp() { this.popUpChild.popUp.nativeElement.open= true; this.changeDetectorRef.detectChanges(); this.popUpchild.name.nativeElement.focus(); } POST请求以上传文件。

我设法使用spring的multipart/form-data来做到这一点:

RestTemplate

然后我尝试使用 public String uploadFile(byte[] file, Map<String, Object> fields, String url) throws URISyntaxException { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); MultiValueMap<String, Object> formData= new LinkedMultiValueMap<String, Object>(); for (Map.Entry<String, Object> entry : fields.entrySet()) { formData.add(entry.getKey(), entry.getValue()); } formData.add("file", file); HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<MultiValueMap<String, Object>>(formData, headers); String response = restTemplate.postForObject(new URI(url), request, String.class); return response; } 做同样的事情,但是我不能和AWS用webclient来回应

以下是使用webclient的代码:

The body of your POST request is not well-formed multipart/form-data.

有人可以指出我想念的是什么吗?

1 个答案:

答案 0 :(得分:0)

事实证明,webclient由于其流性质而不添加content-length头,因此S3 API需要发送该头。

我最终使用restTemplate将文件上传到S3。

相关问题