1

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 ?

Swathi
  • 156
  • 1
  • 2
  • 11
  • 1
    maybe this helps you out: https://stackoverflow.com/questions/2469451/upload-files-from-java-client-to-a-http-server – roediGERhard Sep 07 '20 at 09:09
  • 1
    Does this answer your question? [Upload files from Java client to a HTTP server](https://stackoverflow.com/questions/2469451/upload-files-from-java-client-to-a-http-server) – vanje Sep 07 '20 at 09:56
  • All the solutions are expecting to write to local storage before processing , sadly I do not have that option. – Swathi Sep 07 '20 at 10:28
  • 1
    Files.copy(File,OutputStream) copies the filecontent to the request. You should be able to use the code from the link above. Just replace the File.copy with something else filling the request OutputStream? – roediGERhard Sep 07 '20 at 10:50
  • 1
    "image is not getting sent properly to the third party server", could you please describe that a little bit more in detail? E.g. is the file uploaded incompletely, does the server respond with an error, ...? – Marcono1234 Sep 07 '20 at 11:06

0 Answers0