0

I am sending multipart file data as byte array using ByteArrayResource with custom header for every form field. The source class used to wrap file data

public class VehicleInspectionPhotoUploadResource extends ByteArrayResource {

    private final String fileName;
    public VehicleInspectionPhotoUploadResource(byte[] byteArray,String fileName) {
        super(byteArray);
        this.fileName=fileName;
    }

    @Override
    public String getFilename() {
        return this.fileName;
    }
}

The code to form request of multitype/form-data with every part of form having headers

public MultiValueMap<String, Object> getVehicleInspectPhotoRequestMap(MultiValueMap<String, Part> fileParts) {
        return fileParts.entrySet().stream()
                .map(entry -> {
                    try {
                        VehicleInspectionPhoto vehicleInspectionPhoto = createVehicleInspectionPhoto(entry);
                        HttpHeaders httpHeaders=new HttpHeaders();
                        httpHeaders.set("Doctype","INSPECTION");
                        httpHeaders.setContentType(MediaType.valueOf(entry.getValue().get(0).getContentType()));
                        httpHeaders.setContentDisposition(ContentDisposition.formData().filename(vehicleInspectionPhoto.getFilename()).build());
                        VehicleInspectionPhotoUploadResource vehicleInspectionPhotoUploadResource=
                                new VehicleInspectionPhotoUploadResource(vehicleInspectionPhoto.getBytes(), vehicleInspectionPhoto.getFilename());
                        HttpEntity<VehicleInspectionPhotoUploadResource> httpEntity=new HttpEntity<>(vehicleInspectionPhotoUploadResource,httpHeaders);
                        return Map.entry(entry.getKey(), httpEntity);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }).collect(Collectors.toMap(entry -> entry.getKey(), entry -> List.of(entry.getValue()), (k1, k2) -> {
                    throw new RuntimeException("merging error");
                }, LinkedMultiValueMap::new));

    }

The rest template code where the above created param is passed

public void handlerAsync1(MultiValueMap<String,Object> multiValueMap) {

        HttpEntity<MultiValueMap<String,Object>> requestEntity = new HttpEntity<>(multiValueMap);
        try {
            ResponseEntity<String> responseEntity = restTemplate.exchange("http://localhost:8080/receiveMap", HttpMethod.POST, requestEntity, String.class);
            log.info(responseEntity.getBody());
            log.info(responseEntity.getStatusCode().toString());
        } catch (RestClientResponseException restClientResponseException) {
            log.error( "Error::{}",restClientResponseException);
            throw restClientResponseException;
        }
    }

Below is the piece of code to read the data,

@PostMapping(value = "/receiveMap")
    public ResponseEntity<String> receiveInspectionPhotosForm(@RequestParam MultiValueMap<String, Object> multiValueMap,
                                                          @RequestHeader HttpHeaders httpHeaders) {
        if (!multiValueMap.containsKey("abc")) {
            return ResponseEntity.badRequest().build();
        }

        return ResponseEntity.ok("received");
    }

The issue i am facing is that i am getting empty map above. Even when parameter is of type MultiValueMap<String,Part> the map is empty.Can someone please help me on this?

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.