package org.kodejava.example.commons.codec;
import org.apache.commons.codec.binary.Base64;
import java.util.Arrays;
public class Base64Encode {
public static void main(String[] args) {
String hello = "Hello World";
// The encodeBase64 method take a byte[] as the parameter. The byte[]
// can be from a simple string like in this example or it can be from
// an image file data.
byte[] encoded = Base64.encodeBase64(hello.getBytes());
// Print the encoded byte array
System.out.println(Arrays.toString(encoded));
// Print the encoded string
String encodedString = new String(encoded);
System.out.println(hello + " = " + encodedString);
}
}
The result of our program:
[83, 71, 86, 115, 98, 71, 56, 103, 86, 50, 57, 121, 98, 71, 81, 61]
Hello World = SGVsbG8gV29ybGQ=
Maven Dependencies
<!-- https://search.maven.org/remotecontent?filepath=commons-codec/commons-codec/1.12/commons-codec-1.12.jar -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.12</version>
</dependency>
Latest posts by Wayan (see all)
- How do I install Calibri font in Ubuntu? - January 24, 2021
- How do I create a generic class in Java? - January 1, 2021
- How do I convert java.util.TimeZone to java.time.ZoneId? - April 25, 2020