0
 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

3
  • Can you elaborate more on what's happening in your code and what you are trying to acheive? Commented Mar 7, 2024 at 9:23
  • I'm trying to add a picture to the face list on another server, but I can't Commented Mar 9, 2024 at 4:56
  • Can you elaborate on that part of how you create MultiPart File ? In my guess, that is the part where you get the file not found exception . Commented Mar 21, 2024 at 9:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.