0

In my java servlet program i am getting images from android application.I want to encode again decode image. If i got two images from android: [abc,def].It is decoded using MyHelper.decodeImage1(a). Now problem comes in next step. I want to encode decoded image again. Yes it do encoding. But result is in a single string abcdef. How can i separate two images to send it back to android? How should i manage loop or array in loop. I have tried alot but can't differentiate abc and def. Please help

 String imageString=request.getParameter("image");
 // image comees in format [abc,def,efg... and son on]

        imageString=imageString.replace("[", "");
        imageString=imageString.replace("]", "");
        imageString=imageString.replace(" " , "");

        String[] r = imageString.split(",");

for(int i=0; i<r.length;i++){

                String a=r[i];
                System.out.println("In loop = "+i);

                byte[] imageByteArray = MyHelper.decodeImage1(a);

            String encodedimageByteArray=Base64.encodeBytes(imageByteArray) ;
            ArrayList<String> StringImages =  new ArrayList<String>();
            StringImages.add(encodedimageByteArray);
            response.getWriter().write(encodedimageByteArray);


                }
6
  • Well what do you want the response to look like? You'll need to work out some separator to insert between each pair of images. (It's not clear why you're creating a list of images on each iteration of the loop, by the way.) Commented Mar 4, 2014 at 7:05
  • @JonSkeet [abc,def]..String should be this Commented Mar 4, 2014 at 7:07
  • or abc,def. How can i separate abcdef. Two strings abc and def concatenate in a single string abcdef Commented Mar 4, 2014 at 7:08
  • @JonSkeet i have added code for clarification Commented Mar 4, 2014 at 7:12
  • Well you call write to write a string... so you can write whatever you want. For example, at the start of the loop: if (i != 0) { response.getWriter.write(",")) }. If you want to put it in square brackets you'll need a little bit more work, but fundamentally you're just writing strings to a writer. Commented Mar 4, 2014 at 7:12

1 Answer 1

0

All you need to do is call write appropriately with the text you want to write. For example:

Writer writer = response.getWriter();
writer.write("[");
for (int i = 0; i < r.length; i++) {
     if (i != 0) {
         writer.write(","); // Separate this item from the previous one
     }
     byte[] bytes = MyHelper.decodeImage1(r[i]);
     String base64 = Base64.encodeBytes(bytes);
     writer.write(base64);
}
writer.write("]");
Sign up to request clarification or add additional context in comments.

5 Comments

no need for square brackets...abc,def is ok. But i am not getting it. , should be added in encodedimageByteArray string, after iterating each image.
i want to use only 1 writer.write(base64). base64 string should be [abc,def] or abc,def
@user3118495: You can't do that, because , isn't part of base64 encoding. [abc,def] simply isn't a valid base64 string. Why would you only want to call write once?
@yes once only please. I can make my string abc,def...and so on.then i write the string to app.
@user3118495: But that's not a base64 string. "Yes" doesn't answer my question about why you don't want to call write more than once. What do you think will go wrong? Yes, you could create a StringBuilder, build everything in that and then call write(builder.toString()) but it's pointless.

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.