最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

spring boot - How to send a post request Multipartform-data (restTemplate) in Java returns insufficient data written - Stack Ove

matteradmin3PV0评论

I'm trying to add an attachment (PDF) to a card to Leankit. The request worked before an update from the server. After searching I saw (when using Postman) there's:

----WebKitFormBoundary

in the Content-Type.

I thought it might be the issue, but I don't know how to add it to my headers.

The code was working before an update Leankit side.

Here is my code:

public void attachmentUpload(String filename, String path, String cardId)throws JsonProcessingException
{ 
String url = leankitBaseUrl+"/card/"+cardId+"/attachment"; 
String filePath = path+filename; 
String description = "Dessin"; 
RestTemplate restTemplate = new RestTemplate();

    MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
    body.add("description", description);
    body.add("file", new FileSystemResource(new File(filePath)));

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
   
    headers.set("Authorization",leankitAPIToken);
    headers.set("User-Agent", "Mozilla/5.0");
  headers.setContentLength(objectMapper.writeValueAsString(body).getBytes().length);
    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);

    ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);

    System.out.println("Response status: " + response.getStatusCode());
    System.out.println("Response body: " + response.getBody());
}

And the error:

java.io.IOException: insufficient data written at java.base/sun.www.protocol.http.HttpURLConnection$StreamingOutputStream.close(HttpURLConnection.java:3887) ~[na:na] at .springframework.http.client.SimpleClientHttpRequest.executeInternal(SimpleClientHttpRequest.java:84) ~[spring-web-6.1.14.jar:6.1.14] at .springframework.http.client.AbstractStreamingClientHttpRequest.executeInternal(AbstractStreamingClientHttpRequest.java:70) ~[spring-web-6.1.14.jar:6.1.14] at .springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:66) ~[spring-web-6.1.14.jar:6.1.14] at .springframework.web.client.RestTemplate.doExecute(RestTemplate.java:889) ~[spring-web-6.1.14.jar:6.1.14] at .springframework.web.client.RestTemplate.execute(RestTemplate.java:790) ~[spring-web-6.1.14.jar:6.1.14] at .springframework.web.client.RestTemplate.exchange(RestTemplate.java:672) ~[spring-web-6.1.14.jar:6.1.14]

My idea might not be the solution I'm open to any idea if it can solve my problem.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far