Table of Contents
In this article, we will see how to convert byte array to base64 String in java.
Using Base64 to Convert Byte Array to Base64 String in Java [ Java 8+]
Java 8 has finally introduced Base64 functionalities via java.util.Base64 class.
You can use Base64's encode()
method to convert Byte Array to Base64 String in Java. It encodes input without any line separation.
Here is simple code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org.arpit.java2blog; import java.util.Base64; public class ConvertByteArrayToBase64String { public static void main(String[] args) { // Java 8 // Using encode() byte[] encode = Base64.getEncoder().encode("Java2blog".getBytes()); String result = new String(encode); System.out.println(result); // Using encodeToString() to get String directly String encodeToString = Base64.getEncoder().encodeToString("Java2blog".getBytes()); System.out.println(encodeToString); } } |
Output:
SmF2YTJibG9n
In base64, length of encoded String should be multiple of 3. Sometimes, encode appends one or two padding characters to make it multiple of 3.
In case, you don’t want to append extra padding characters, you can skip padding using withPadding()
method. This is useful when you already know that you are not going to decode this String back.
Here is the code:
1 2 3 |
String encodeToString = Base64.getEncoder().withoutPadding().encodeToString("Java2blog".getBytes()); |
Using Apache Common Codec to Convert Byte Array to Base64 String in Java [ < Java 8]
If you are using Java 7 or below version. there is no direct utility class to convert Byte Array to Base64 String in Java.
You can use Apache common codec’s Base64 class to achieve the same.
You need to add following dependenices in your pom.xml.
1 2 3 4 5 6 7 |
<dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.15</version> </dependency> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package org.arpit.java2blog; import org.apache.commons.codec.binary.Base64; public class ConvertByteArrayToBase64StringApache { public static void main(String[] args) { Base64 codec = new Base64(); // Using encode() byte[] encode = codec.encode("Java2blog".getBytes()); String result = new String(encode); System.out.println(result); // Using encodeBase64String() to get String directly Base64 base64Codec = new Base64(); String base64String = base64Codec.encodeBase64String("Java2blog".getBytes()); System.out.println(base64String); } } |
Output:
SmF2YTJibG9n
Using Base64Utils to Convert Byte Array to Base64 String in Spring
if you are using Spring in your project, you can use Base64Utils
to convert Byte Array to Base64 String in java. It provides exact same functions as java.util.Base64
.
You need to add following dependenices in your pom.xml.
1 2 3 4 5 6 7 |
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.1.10.RELEASE</version> </dependency> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org.arpit.java2blog; import org.springframework.util.Base64Utils; public class ConvertByteArrayToBase64StringSpring { public static void main(String[] args) { // Spring // Using encode() byte[] encode = Base64Utils.encode("Java2blog".getBytes()); String result = new String(encode); System.out.println(result); // Using encodeToString() to get String directly String encodeToString = Base64Utils.encodeToString("Java2blog".getBytes()); System.out.println(encodeToString); } } |
Output:
SmF2YTJibG9n
Using android.util.Base64 to Convert Byte Array to Base64 String in Android
if you are using android in your project, you can use android.util.Base64
to convert Byte Array to Base64 String in java. It provides exact same functions as java.util.Base64
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org.arpit.java2blog; import android.util.Base64; public class ConvertByteArrayToBase64StringAndroid { public static void main(String[] args) { // Android // Using encode() byte[] encode = Base64.getEncoder().encode("Java2blog".getBytes()); String result = new String(encode); System.out.println(result); // Using encodeToString() to get String directly String encodeToString = Base64.getEncoder().encodeToString("Java2blog".getBytes()); System.out.println(encodeToString); } } |
Output:
SmF2YTJibG9n
That’s all about how to convert Byte Array to Base64 String in java.