Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

1. Overview

In this quick tutorial, we're going to cover how to encode image file to a Base64 String, then decode it to retrieve the original image using Apache Common IO and Java 8 native Base64 features.

This operation could be applied for any binary files or binary arrays. It's useful when we need to transfer binary content in JSON format such as from mobile app to REST endpoint.

For more information about Base64 conversion, check out this article here.

2. Maven Dependency

Let’s add the following dependencies to the pom.xml file:


You can find the latest version of Apache Commons IO on Maven Central.

3. Convert Image File to Base64 String

First of all, let's read the file content to a byte array and use Java 8 Base64 class to encode it:

byte[] fileContent = FileUtils.readFileToByteArray(new File(filePath));
String encodedString = Base64.getEncoder().encodeToString(fileContent);

The encodedString is a String of characters in the set of A-Za-z0-9+/, and the decoder rejects any characters outside of this set.

4. Convert Base64 String to Image File

Now we have a Base64 String, let’s decode it back to binary content and write to a new file:

byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
FileUtils.writeByteArrayToFile(new File(outputFileName), decodedBytes);

5. Testing Our Code

Finally, we can verify that the code is working correctly by reading a file, encoding it to a Base64 String, and decoding it back to a new file:

public class FileToBase64StringConversionUnitTest {

    private String inputFilePath = "test_image.jpg";
    private String outputFilePath = "test_image_copy.jpg";

    public void fileToBase64StringConversion() throws IOException {
        // load file from /src/test/resources
        ClassLoader classLoader = getClass().getClassLoader();
        File inputFile = new File(classLoader

        byte[] fileContent = FileUtils.readFileToByteArray(inputFile);
        String encodedString = Base64

        // create output file
        File outputFile = new File(inputFile
          .getAbsolutePath() + File.pathSeparator + outputFilePath);

        // decode the string and write to file
        byte[] decodedBytes = Base64
        FileUtils.writeByteArrayToFile(outputFile, decodedBytes);

        assertTrue(FileUtils.contentEquals(inputFile, outputFile));

6. Conclusion

This to-the-point article explains the basic of encoding any file's content to a Base64 String, and decoding a Base64 String to a byte array and save it to a file using Apache Common IO and Java 8 features.

As always, code snippets can be found over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Generic footer banner
Comments are closed on this article!