How do I convert Base64 string to image file?

In the previous example, How do I convert an image file to a Base64 string?, you’ve seen how to convert image file to base64 string.

In this example, you will see how you can convert a base64 string back into an image file. Below are examples of how to do this in Java, using the Java 8 native java.util.Base64 class.

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

public class Base64ToImage {
    public static void main(String[] args) throws Exception {
        // this is your Base64 encoded string
        String base64String = "iVBORw0...";

        byte[] decodedBytes = Base64.getDecoder().decode(base64String);
        Files.write(Paths.get("/path/to/your/outputimage.png"), decodedBytes);

Just replace “/path/to/your/outputimage.png” with the actual path where you want to save the image.

This code will decode the base64 string back into a byte array, and then it will write this byte array into an image file. Be careful with the format of the image (PNG, JPG, etc.) as the format of the output file should match the format of the original base64-encoded image.

How do I decode a Base64 encoded binary?

package org.kodejava.commons.codec;

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

import java.util.Arrays;

public class Base64Decode {
    public static void main(String[] args) {
        String hello = "SGVsbG8gV29ybGQ=";

        // Decode a previously encoded string using decodeBase64 method and
        // passing the byte[] of the encoded string.
        byte[] decoded = Base64.decodeBase64(hello.getBytes());

        // Print the decoded array

        // Convert the decoded byte[] back to the original string and print
        // the result.
        String decodedString = new String(decoded);
        System.out.println(hello + " = " + decodedString);

The result of our code is:

[72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]
SGVsbG8gV29ybGQ= = Hello World

Maven Dependencies


Maven Central

How do I decode string to integer?

The static Integer.decode() method can be used to convert a string representation of a number into an Integer object. Under the cover this method call the Integer.valueOf(String s, int radix).

The string can start with the optional negative sign followed with radix specified such as 0x, 0X, # for hexadecimal value, 0 (zero) for octal number.

package org.kodejava.lang;

public class IntegerDecode {
    public static void main(String[] args) {
        String decimal = "10"; // Decimal
        String hex = "0XFF"; // Hex
        String octal = "077"; // Octal

        Integer number = Integer.decode(decimal);
        System.out.println("String [" + decimal + "] = " + number);

        number = Integer.decode(hex);
        System.out.println("String [" + hex + "] = " + number);

        number = Integer.decode(octal);
        System.out.println("String [" + octal + "] = " + number);

The result of the code snippet above:

String [10] = 10
String [0XFF] = 255
String [077] = 63