How to Use Base64 Encoding and Decoding in Java 7

Struggling with Base64 encoding and decoding in Java 7? Don't worry. Our step-by-step guide using Apache Commons Codec will help you out!
On this page

How to Use Base64 Encoding and Decoding in Java 7

Excerpt

Struggling with Base64 encoding and decoding in Java 7? Don’t worry. Our step-by-step guide using Apache Commons Codec will help you out!

Introduction

Although Java 8 introduced the java.util.Base64 class to perform these operations, there are still many projects that depend on Java 7.

Apache Commons Codec provides a Base64 class that is easy to use in Java 7. Apache Commons Codec is a practical Java library for handling encodings such as Base64 and hexadecimal. It provides a simple API for encoding and decoding various formats of data, including Base64.

The library is widely used in the industry, incubated by well-known open source organization Apache for over a decade, making it an excellent choice for developers. In this article, we will step by step introduce how to use Apache Commons Codec Base64 to perform Base64 encoding and decoding in Java 7.

Base64 Encoding in Java 7

Below is a step-by-step tutorial on how to use Apache Commons Codec for Base64 encoding.

Importing the Apache Commons Codec library

The first step is to import the Apache Commons Codec library into your project. You can download the latest version of the library from the official website. Once you’ve downloaded the library, extract it and add commons-codec-1.x.jar to your project’s classpath.

Importing Apache Commons Codec with Maven

Alternatively, you can use Maven to import the Apache Commons Codec library. In your project’s pom.xml file, add the following dependency:

1<dependency>
2  <groupId>commons-codec</groupId>
3  <artifactId>commons-codec</artifactId>
4  <version>1.15</version>
5</dependency>

Encoding a String using the Base64 class

 1import org.apache.commons.codec.binary.Base64;
 2
 3public class Base64EncodingExample {
 4	public static void main(String[] args) {
 5		String originalString = "Hello World!";
 6		byte[] encodedBytes = Base64.encodeBase64(originalString.getBytes());
 7		String encodedString = new String(encodedBytes);
 8		System.out.println("Encoded string: " + encodedString);
 9	}
10}

In this example, we first create a string that we want to encode. We then convert this string into a byte array and pass it to the Base64.encodeBase64() method provided by Apache Commons Codec. This method returns a byte array containing the encoded data. Finally, we create a new string from this byte array and print it out.

Encoding binary data using the Base64 class

You can also use the Base64.encodeBase64() method to encode binary data, such as images or audio files. Here’s an example:

 1import java.io.File;
 2import java.io.FileInputStream;
 3import java.io.IOException;
 4import org.apache.commons.codec.binary.Base64;
 5
 6public class BinaryDataEncodingExample {
 7    public static void main(String[] args) throws IOException {
 8        File file = new File("image.png");
 9        FileInputStream inputStream = new FileInputStream(file);
10        byte[] bytes = new byte[(int) file.length()];
11        inputStream.read(bytes);
12
13        byte[] encodedBytes = Base64.encodeBase64(bytes);
14        String encodedString = new String(encodedBytes);
15
16        System.out.println("Encoded string: " + encodedString);
17    }
18}

In this example, we read in an image file as a binary stream and store its contents in a byte array. We then pass this byte array to Base64.encodeBase64() method provided by Apache Commons Codec to get back an encoded byte array containing our binary data in base-64 encoding format.

The free Base64 Encode verification tool is as follows:

Base64 Decoding in Java 7

Base64 decoding is the process of converting a Base64 encoded data back to its original form. In Java 8, this can be achieved using the java.util.Base64 class. However, for those using Java 7 or lower versions, there is no built-in support for Base64 decoding.

Step-by-Step Guide

To perform Base64 decoding in Java 7, we will make use of the Apache Commons Codec library. Follow these steps to get started:

Importing the Apache Commons Codec library

Firstly, we need to download and import the Apache Commons Codec library into our project. You can download it from the official website and add it to your project’s classpath.

Importing Apache Commons Codec with Maven

Alternatively, you can use Maven to import the Apache Commons Codec library. In your project’s pom.xml file, add the following dependency:

1<dependency>
2  <groupId>commons-codec</groupId>
3  <artifactId>commons-codec</artifactId>
4  <version>1.15</version>
5</dependency>

Decoding a String using the Base64 class

To decode a Base64 encoded string in Java 7, we will use the org.apache.commons.codec.binary.Base64 class from the Apache Commons Codec library. Here’s an example code snippet:

 1import org.apache.commons.codec.binary.Base64;
 2
 3public class Base64Decoder {
 4    public static void main(String[] args) {
 5        String encodedString = "SGVsbG8gV29ybGQh";
 6
 7        byte[] decodedBytes = Base64.[decodeBase64](https: //documentation.xojo.com/api/text/encoding_text/decodebase64.html)(encodedString);
 8            String decodedString = new String(decodedBytes);
 9
10            System.out.println("Decoded string: " + decodedString);
11        }
12    }

In this example, we have a Base64 encoded string SGVsbG8gV29ybGQh which translates to Hello World! after decoding. We first decode the string using Base64.decodeBase64() method which returns a byte array. We then convert this byte array back to a string using new String() constructor.

Decoding binary data using the Base64 class

Apart from strings, we can also decode binary data such as images or audio files using the org.apache.commons.codec.binary.Base64 class in Java 7. Here’s an example code snippet:

 1import org.apache.commons.codec.binary.Base64;
 2import java.io.FileOutputStream;
 3import java.io.IOException;
 4
 5public class BinaryDataDecoder {
 6    public static void main(String[] args) throws IOException {
 7        String encodedData = "/9j/4AAQSkZJRgABAQEASABIAAD...";
 8
 9        byte[] decodedBytes = Base64.decodeBase64(encodedData);
10
11        FileOutputStream fos = new FileOutputStream("image.jpg");
12        fos.write(decodedBytes);
13        fos.close();
14    }
15}

In this example, we have a Base64 encoded image data stored in a string variable encodedData. We first decode this data into a byte array using Base64.decodeBase64() method. We then write this byte array to an output file stream which saves it as an image file “image.jpg”.

With these steps, you can easily perform Base64 decoding in Java 7 using Apache Commons Codec library.

The free Base64 Decode verification tool is as follows: