How to Convert InputStream to Base64 String – 如何将InputStream转换为Base64字符串

最后修改: 2022年 6月 30日

1. Overview


Base64 is a text encoding scheme that provides portability for binary data between applications and platforms. Base64 can be used to store binary data in a database string column, thereby avoiding messy file operations. When combined with the data URI scheme, Base64 can be used to embed images in web pages and emails, in conformance with the HTML and Multipurpose Internet Mail Extensions (MIME) standards.


In this brief tutorial, we’ll demonstrate Java Streaming IO functions and the built-in Java Base64 class to load binary data as an InputStream and then convert it to a String.

在这个简短的教程中,我们将演示Java Streaming IO函数和内置的Java Base64类,以加载二进制数据作为InputStream,然后将其转换成String

2. Setup


Let’s look at the dependencies and the test data we’ll need for the code.


2.1. Dependencies

2.1. 依赖性

We’ll use the Apache IOUtils library for convenient access to testing data files by adding its dependency to our pom.xml:

我们将使用Apache IOUtils库,通过将其依赖性添加到我们的pom.xml中,以方便访问测试数据文件。


2.2. Test Data

2.2 测试数据

A binary test data file is needed here. So we’ll add a logo.png image file to our standard src/test/resources folder.


3. Converting InputStream to Base64 String


Java has built-in support for Base64 encoding and decoding in the java.util.Base64 class. So we’ll be using the static methods from there to do the heavy lifting. 


Base64.encode() method expects a byte array, and our image is in a file. Therefore, we need to first convert the file to an InputStream and then read the stream, byte-by-byte, into an array.


We’re using the IOUtils.toByteArray() method from the Apache commons-io package as a convenient alternative to the otherwise verbose Java-only approach.

我们使用Apache commons-io包中的IOUtils.toByteArray()方法,以方便替代只用Java的繁琐方法。

First, we’ll write a simple method to generate a ‘poor man’s’ checksum:

首先,我们将写一个简单的方法来生成一个 “穷人 “的校验和。

int calculateChecksum(byte[] bytes) {
    int checksum = 0; 
    for (int index = 0; index < bytes.length; index++) {
        checksum += bytes[index]; 
    return checksum; 

We’ll use it to compare the two arrays, verifying that they match.


The next lines open the file, convert it to a byte array, and then Base64 encode it to a String:


InputStream sourceStream  = getClass().getClassLoader().getResourceAsStream("logo.png");
byte[] sourceBytes = IOUtils.toByteArray(sourceStream);

String encodedString = Base64.getEncoder().encodeToString(sourceBytes); 

The String looks like a block of random characters. In fact, it is not random, as we see in the verification steps:


byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
assertTrue(decodedBytes.length == sourceBytes.length);
assertTrue(calculateChecksum(decodedBytes) == calculateChecksum(sourceBytes));

4. Conclusion


In this article, we’ve demonstrated the encoding of a InputStream to a Base64 string and the successful decoding of that string back into a binary array.


As always, the code presented in this article is available over on GitHub.