I am receiving the image as a base64encoded string in JSON key named file.
{
"fileName":"Feedback_image.jpg",
"file":" // image data being sent from android "
}
I am decoding the base64 encoded string on the server-side using the decode function.
byte[] imageByteArray = Base64.getDecoder().decode(imageUploadDTO.getFile());
I can even write the image to windows and the image gets created successfully. Now the problem I am facing is I have to send the image as a multipart entity to the third part POst API. I am trying to form the HttpPost using the following code but the image is not getting sent properly to the third party server.
public HttpPost getHttpPostRequest(String requestUrl, byte[] imageByteArray ,String fileName) throws Exception {
HttpPost httpPostRequest = new HttpPost(requestUrl);
httpPostRequest.addHeader(CRGERaagaAppConstants.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
ContentBody cd = new InputStreamBody(new ByteArrayInputStream(imageByteArray), fileName);
builder.addPart("fileName", cd);
HttpEntity entity = builder.build();
httpPostRequest.setEntity(entity);
return httpPostRequest;
}
Could someone please tell me how to retrieve the image content correctly from the base64encoded string and send to HttpPost ?