java签名的s3链接通过resttemplate上传,会将不需要的头添加到文件中

hgc7kmma  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(220)

我想能够上传一个txt文件从java到一个服务通过returends3链接。我目前可以上传文件,但它添加了一些标题的文本文件,这是不想要的。目前我有一个字符串“hello world”,它被转换成一个字节[]来发送。上载的文本文件无效

--HvoqYHn26clflZUPY0bFJmQVB6hNRsF5_H
Content-Disposition: form-data; name="file"
Content-Type: application/octet-stream
Content-Length: 11

hello world
--HvoqYHn26clflZUPY0bFJmQVB6hNRsF5_H--

我的模板代码是

HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
    HttpEntity<String> request =
            new HttpEntity<>(object.toString(), headers);
    ResponseEntity<Documents> response = leapRestTemplate.postForEntity("https://uk-api.leap.services/api/v1/documents", request, Documents.class);
    Documents documents = response.getBody();
    String signedUrl = documents.signedUrl;

    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    HttpEntity<byte[]> entity = new HttpEntity<>("hello world".getBytes());
    MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
    body.add("file", entity);
    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
    leapRestTemplate.exchange(new URI(signedUrl), HttpMethod.PUT, requestEntity, String.class);

你知道为什么上传的txt文件不仅仅是helloworld吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题