Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

java - Signed S3 Link Updload Via RestTemplate Adds Unwanted Headers To File

I want to be able to upload a txt file from java to a serice via a returend s3 link. I am currently able to upload the file but it adds some headers to the text file which is not wanted. Currently I have a string "hello world" which is converted to a byte[] to send. The uploaded text file is

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

hello world
--HvoqYHn26clflZUPY0bFJmQVB6hNRsF5_H--

My restTemplate code is

    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);

Any ideas on why the uploaded txt file is not just hello world?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...