2

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.

6
  • Note that your condition content != "" will always be true (even if content is null or an empty string), unless you have explicitly assigned content = "". Commented Dec 27, 2013 at 11:50
  • @MarkoTopolnik : Yes, my condition will always be true. If it is false, I am breaking this flow and processing something else. Commented Dec 27, 2013 at 11:51
  • You want your condition to be false for empty strings, don't you? Well, it is not what you are getting... Commented Dec 27, 2013 at 11:52
  • I want the bytestream to be encoded by some means in java before the ajax POST request so that byte stream will be passed inside my json object. And the same will be decoded in my API. Commented Dec 27, 2013 at 11:53
  • Can I use base64 encoding? or is there any other way? Commented Dec 27, 2013 at 12:04

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

There is a public Base64 codec in JAXB, which is included with the JDK.
@Developer SuRu - Im using jdk 1.6. I think this package is not supported. Can you please suggest the right one for 1.6?
bugs.sun.com/bugdatabase/view_bug.do?bug_id=4808229 see this bug report and use jdk1.7 or copy that file from jdk1.7 src to your project.
You can also use the Base64 provided by Apache Commons Codec, see this link with an example: myjeeva.com/…

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.