Encode a String into Base64 using Base64.encodeBase64String() with Apache Commons Codec

Tags: Apache Commons Apache Commons Codec Java Base64

Java Code Examples for org.apache.commons.codec.binary.Base64.encodeBase64String()

This static method to encode input String into a Base64 format String.

Add Apache Commons Codec Dependency to Java project

To use Apache Commons Codec library in the Gradle build project, add the following dependency into the build.gradle file.

implementation group: 'commons-codec', name: 'commons-codec', version: '1.15'

To use the Apache Commons Codec library in the Maven build project, add the following dependency into the pom.xml file.

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

package simplesolution.dev;

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

public class Base64EncodeBase64StringExamples {

    public static void main(String... args) {
        String valueToEncode = "https://simplesolution.dev/";
        String encodedString = Base64.encodeBase64String(valueToEncode.getBytes());

        System.out.println("Encode output: ");
        System.out.println(encodedString);
    }
}

Happy Coding 😊

Java Base64 Encoding and Decoding with Apache Commons Codec

How to encode byte array into Base64 format

How to encode a String into Base64 format

How to encode byte array into Base64 format

Java Apache Commons Codec Tutorial