Poppy
Poppy

Reputation: 3092

How to send image byte array in JSON POST in Java

I am sending an image file along with other form elements in my java application. Before sending my content to API, in JS ajax call, I am using

if (content != "") {
    sendData = JSON.parse(content);
   }

since the byte array contains \, "" and other characters, JSON parsing fails here. Is there any way to encode the byte array and decode it in service layer? Please help me. Thanks in advance.

Upvotes: 2

Views: 8615

Answers (1)

SuRu
SuRu

Reputation: 739

You can convert the bitmap (or any binary data) to text using base64 (which makes it a String) I wouldn't use one of the Base64 classes in the JVM unless you are fully aware that they are for internal use. (may not be available on all JDKs and could change in future versions)

You could copy java.util.prefs.Base64 if you don't have one in a library already.

Upvotes: 2

Related Questions