Convert byte array to Base64 String in Java with examples

In this post, I will be sharing how to convert byte array to Base64 String in Java with examples. There are 4 ways to convert byte array to Base64 String in Java:

1. Using java.util.Base64 class (java 8+)

2. Using Apache Commons Codec (< java 8)

3. Using Base64Utils (Spring)

4. Using android.util.Base64 (android)

Let's dive deep into the topic:

Convert byte array to Base64 String in Java

1. Using java.util.Base64 class (java 8+)


java.util.Base64 class was introduced in Java 8 to provide Base64 functionalities. You can use Base64 class's getEncoder() method to convert byte array to Base64 String in Java as shown below in the example.

 import java.util.Base64;

public class ByteArrayToBase64String {
    public static void main(String args[]) {
      // using encode() method     
      byte[] encoded = Base64.getEncoder().encode("AliveisAwesome".getBytes());
      String output = new String(encoded);
      System.out.println(output);
      
      // One liner
      String encodedString = Base64.getEncoder().encodeToString("AliveisAwesome".getBytes());
      System.out.println(encodedString);
    }
}


Output:
QWxpdmVpc0F3ZXNvbWU=
QWxpdmVpc0F3ZXNvbWU=

2. Using Apache Commons Codec (< java 8)


Before Java 8, i.e. Java 7 and below versions do not have any direct utility class to convert byte array to Base64 String in Java. You can easily achieve the same by using Apache commons codec's Base64 class. Add the below dependency to your pom.xml.

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.15</version>
</dependency>


 import org.apache.commons.codec.binary.Base64;

public class ByteArrayToBase64String2 {
    public static void main(String args[]) {
      Base64 codec = new Base64();    
      // using Base64 class encode() method     
      byte[] encoded = codec.encode("AliveisAwesome".getBytes());
      String output = new String(encoded);
      System.out.println(output);
      
      // One liner using encodeBase64String() 
      String encodedString = codec.encodeBase64String("AliveisAwesome".getBytes());
      System.out.println(encodedString);
    }
}


Output:
QWxpdmVpc0F3ZXNvbWU=
QWxpdmVpc0F3ZXNvbWU=

3. Using Base64Utils (Spring)


For the Spring project, there is another way to convert the byte array to Base64 String. We can achieve our goal by using Base64Utils class. Base64Utils class has the same functions as java.util.Base64. Add the below dependency to your pom.xml.

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.3.23</version>
</dependency>


 import org.springframework.util.Base64Utils;

public class ByteArrayToBase64String3 {
    public static void main(String args[]) {
      // using spring Base64Utils class encode() method     
      byte[] encoded = Base64Utils.encode("AliveisAwesome".getBytes());
      String output = new String(encoded);
      System.out.println(output);
      
      // One liner using encodeToString() 
      String encodedString = Base64Utils.encodeToString("AliveisAwesome".getBytes());
      System.out.println(encodedString);
    }
}


Output:
QWxpdmVpc0F3ZXNvbWU=
QWxpdmVpc0F3ZXNvbWU=

4. Using android.util.Base64 (android)


Convert byte array to String in android using android.util.Base64 class is shown below in the example.

 import android.util.Base64;

public class ByteArrayToBase64String4 {
    public static void main(String args[]) {
      // using android Base64 class encode() method     
      byte[] encoded = Base64.getEncoder().encode("AliveisAwesome".getBytes());
      String output = new String(encoded);
      System.out.println(output);
      
      // One liner using encodeToString() 
      String encodedString = Base64.getEncoder().encodeToString("AliveisAwesome".getBytes());
      System.out.println(encodedString);
    }
}


Output:
QWxpdmVpc0F3ZXNvbWU=
QWxpdmVpc0F3ZXNvbWU=

That's all for today, please mention in the comments if you have any questions related to how to convert byte array to Base64 String in Java with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry