public HttpEntity<?> card(MultipartFile multipartFile, String name) {
try {
String jsonData = String.format("{\"active\": true, \"name\": \"%s\", \"watch_lists\": [\"%s\"]}",
name, workerWatchList);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", token);
HttpEntity<String> addCardEntity = new HttpEntity<>(jsonData, headers);
ResponseEntity<String> addCardResponse = new RestTemplate().exchange(
baseUrl + "/cardurl",
HttpMethod.POST,
addCardEntity,
String.class
);
if (addCardResponse.getStatusCode().is2xxSuccessful()) {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(addCardResponse.getBody());
// Get the value associated with the "id" key
String id = String.valueOf(rootNode.get("id"));
System.out.println(id);
HttpHeaders faceHeaders = new HttpHeaders();
faceHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
faceHeaders.set("Authorization", token);
MultiValueMap<String, Object> faceBody = new LinkedMultiValueMap<>();
faceBody.add("source_photo", multipartFile.getResource());
faceBody.add("card", id);
faceBody.add("mf_selector", "biggest");
HttpEntity<MultiValueMap<String, Object>> addFaceEntity = new HttpEntity<>(faceBody, faceHeaders);
ResponseEntity<MultiValueMap> addFaceResponse = new RestTemplate().exchange(
baseUrl + "/faceurl",
HttpMethod.POST,
addFaceEntity,
MultiValueMap.class);
if (addFaceResponse.getStatusCode().is2xxSuccessful()) {
return ResponseEntity.ok(id);
} else {
return new ResponseEntity<>(addFaceResponse.getBody(), HttpStatus.INTERNAL_SERVER_ERROR);
}
} else {
return new ResponseEntity<>(addCardResponse.getBody(), HttpStatus.INTERNAL_SERVER_ERROR);
}
} catch (Exception e) {
// Handle any other exceptions
return ResponseEntity.ok("Error: " + e.getMessage());
}
}
My program sometimes works and sometimes stops working. There is an error in adding multipart file I get the following error when I run this program: java.nio.file.NoSuchFileException: C:\Users\Acer\AppData\Local\Temp\tomcat.8080.12753633009760934525\work\Tomcat\localhost\ROOT\upload_7761731e_ac13_4cc4_8cda_e9c930bf8a50_00000000.tmp