4
List<Map<String, Object>> lists = baseDao.getJdbcTemplate().queryForList(queryForImages, productId);
        **System.out.println("size----" + lists.size());**
        ArrayList<String> arrayList = new ArrayList<String>();
        for (Map<String, Object> base64 : lists) {
            byte[] imgData = (byte[]) base64.get("images");
            String base64Encoded = new String(imgData, StandardCharsets.UTF_8);
            // byte[] encodedImage = Base64.encodeBase64(imgData);
            System.out.println("encoded---2" + base64Encoded);
        }

i retrieve list of images from db and convert byte array to base64 string ,if i sysout only the last image in the list prints in sysout , even sysout list.size under the method is not printing .

4
  • Is there any exception and where does the code stuck? Commented Jun 20, 2016 at 13:00
  • 1
    Another option to convert byte array to base64 string is Base64.encode(imgData ) Commented Jun 20, 2016 at 13:06
  • hello kevin , i dont get any exception , from db i get list of images ,if i sysout only the last image in the list prints ,and why sysout (lists.size()); is not printing , the only thing i get is last image gets converted to base64 , i need to all images in the list . Commented Jun 20, 2016 at 17:02
  • please watch this video youtube.com/watch?v=t7g06nzlkjo Commented Jun 21, 2016 at 4:26

4 Answers 4

14

To start with, this is wrong:

String base64Encoded = new String(imgData, StandardCharsets.UTF_8);

The image bytes you have do not represent UTF-8 encoded text, so it makes no sense to create a String out of the bytes, pretending that this is UTF-8 encoded text.

If you are using Java 8, then use the Base64 class available in the standard library:

import java.util.Base64;

// ...

byte[] imgData = (byte[]) base64.get("images");

String base64Encoded = Base64.getEncoder().encodeToString(imgData);

If you are not using Java 8, you can use a third-party library, for example Apache Commons Codec, which includes classes for Base64 encoding and decoding.

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

2 Comments

i use java 1.7 , Base64.getEncoder() this method is not available in import org.apache.commons.codec.binary.Base64 please tell me what is jar needed .
youtube.com/watch?v=t7g06nzlkjo , please watch this videos because still i have problem
13

In android, you can use Base64 (android.util.Base64) to encoded byte[] to String;

String encodedString = Base64.encodeToString(byteArray, Base64.DEFAULT);

and

Decoding part as like following;

byte[] byteArray = org.apache.commons.codec.binary.Base64.decodeBase64(encodedString);

Usually this technique used in android app to send binary file/data to Server, using Web Service;

Comments

4

If you are working with Android you can use the following code by importing the Android version of the Base64 class:

Base64.encodeToString(payload, Base64.DEFAULT);

From Android API 26, you can use java.util.Base64 as well.

Comments

3

Prior to java 1.8 use apache commons library

String base64 = new String(Base64.encodeBase64(imgData))

Comments

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.