How to use Java 8 Encode (Decode) an Image to Base64

For some purpose like transfering an image through RestfulAPI or saving an image to a DataBase, We need Encoder (Decode) the image to Base64. In the tutorial, JavaSampleApproach will guide you how to use Java 8 for converting.

Related post: Java 8 Base64

I. Technologies for Java 8 Encode Decode an Image to Base64 tutorial

– Eclipse
– Java 8

Note: Prepare an Image at folder C:\\base64\\image.jpg

II. Java 8 Encode an Image to Base64


public static String encoder(String imagePath) {
	String base64Image = "";
	File file = new File(imagePath);
	try (FileInputStream imageInFile = new FileInputStream(file)) {
		// Reading a Image file from file system
		byte imageData[] = new byte[(int) file.length()];
		imageInFile.read(imageData);
		base64Image = Base64.getEncoder().encodeToString(imageData);
	} catch (FileNotFoundException e) {
		System.out.println("Image not found" + e);
	} catch (IOException ioe) {
		System.out.println("Exception while reading the Image " + ioe);
	}
	return base64Image;
}

III. Java 8 Decode an Base64 String to an Image


public static void decoder(String base64Image, String pathFile) {
	try (FileOutputStream imageOutFile = new FileOutputStream(pathFile)) {
		// Converting a Base64 String into Image byte array
		byte[] imageByteArray = Base64.getDecoder().decode(base64Image);
		imageOutFile.write(imageByteArray);
	} catch (FileNotFoundException e) {
		System.out.println("Image not found" + e);
	} catch (IOException ioe) {
		System.out.println("Exception while reading the Image " + ioe);
	}
}

IV. Full Sample


package com.javasampleapproach.base64image;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;

public class Java8Base64Image {
	public static void main(String[] args) {
		String imagePath = "C:\\base64\\image.jpg";
		System.out.println("=================Encoder Image to Base 64!=================");
		String base64ImageString = encoder(imagePath);
		System.out.println("Base64ImageString = " + base64ImageString);

		System.out.println("=================Decoder Base64ImageString to Image!=================");
		decoder(base64ImageString, "C:\\base64\\decoderimage.jpg");

		System.out.println("DONE!");

	}

	public static String encoder(String imagePath) {
		String base64Image = "";
		File file = new File(imagePath);
		try (FileInputStream imageInFile = new FileInputStream(file)) {
			// Reading a Image file from file system
			byte imageData[] = new byte[(int) file.length()];
			imageInFile.read(imageData);
			base64Image = Base64.getEncoder().encodeToString(imageData);
		} catch (FileNotFoundException e) {
			System.out.println("Image not found" + e);
		} catch (IOException ioe) {
			System.out.println("Exception while reading the Image " + ioe);
		}
		return base64Image;
	}

	public static void decoder(String base64Image, String pathFile) {
		try (FileOutputStream imageOutFile = new FileOutputStream(pathFile)) {
			// Converting a Base64 String into Image byte array
			byte[] imageByteArray = Base64.getDecoder().decode(base64Image);
			imageOutFile.write(imageByteArray);
		} catch (FileNotFoundException e) {
			System.out.println("Image not found" + e);
		} catch (IOException ioe) {
			System.out.println("Exception while reading the Image " + ioe);
		}
	}
}

Download Sourcecode
Java8Base64Image



By grokonez | January 13, 2017.

Last updated on April 22, 2021.



Related Posts


6 thoughts on “How to use Java 8 Encode (Decode) an Image to Base64”

  1. It is very usefull for me I was searching from few days for this code in c thanks for posting.
    but it is not printing the encoded base64 string for me
    String base64ImageString = encoder(imagePath);
    System.out.println(“Base64ImageString = ” + base64ImageString);
    this line is not working for me its not printing anything before this line.
    can u please help me with this

Got Something To Say:

Your email address will not be published. Required fields are marked *

*