如何将base64解码后的图像以byte[]的形式发送到java中的httppost

lstz6jyr  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(213)

我在json密钥file中以base64编码字符串的形式接收图像。

{
    "fileName":"Feedback_image.jpg",
    "file":" // image data being sent from android "
}

我使用decode函数在服务器端解码base64编码的字符串。

byte[] imageByteArray = Base64.getDecoder().decode(imageUploadDTO.getFile());

我甚至可以将图像写入windows,并且成功地创建了图像。现在我面临的问题是,我必须将图像作为一个多部分实体发送到第三部分postapi。我试图使用以下代码形成httppost,但是图像没有正确地发送到第三方服务器。

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;
        }

有人能告诉我如何从base64编码字符串中正确检索图像内容并发送到httppost吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题