Contents

图像与base64字符串转换

1. 概述

在这个快速教程中,我们将介绍如何将图像文件编码为 Base64string,然后使用 Apache Common IO 和 Java 8 原生 Base64 功能对其进行解码以检索原始图像。

此操作可应用于任何二进制文件或二进制数组。当我们需要以 JSON 格式传输二进制内容(例如从移动应用程序到 REST 端点)时,它很有用。

有关 Base64 转换的更多信息,请在此处查看本文

2. Maven依赖

让我们将以下依赖项添加到pom.xml文件中:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>

您可以在Maven Central 上找到最新版本的 Apache Commons IO 。

3. 将图像文件转换为 Base64string

首先,让我们将文件内容读取到一个字节数组中,并使用 Java 8 Base64类对其进行编码:

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

encodedString是*A-Za-z0-9+/*集合中的 string,解码器拒绝该集合之外的任何字符。

4. 将Base64 string转换为图像文件

现在我们有一个 Base64 String,让我们将其解码回二进制内容并写入一个新文件:

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

5. 测试我们的代码

最后,我们可以通过读取文件、将其编码为 Base64 String并将其解码回新文件来验证代码是否正常工作:

public class FileToBase64StringConversionUnitTest {
    private String inputFilePath = "test_image.jpg";
    private String outputFilePath = "test_image_copy.jpg";
    @Test
    public void fileToBase64StringConversion() throws IOException {
        // load file from /src/test/resources
        ClassLoader classLoader = getClass().getClassLoader();
        File inputFile = new File(classLoader
          .getResource(inputFilePath)
          .getFile());
        byte[] fileContent = FileUtils.readFileToByteArray(inputFile);
        String encodedString = Base64
          .getEncoder()
          .encodeToString(fileContent);
        // create output file
        File outputFile = new File(inputFile
          .getParentFile()
          .getAbsolutePath() + File.pathSeparator + outputFilePath);
        // decode the string and write to file
        byte[] decodedBytes = Base64
          .getDecoder()
          .decode(encodedString);
        FileUtils.writeByteArrayToFile(outputFile, decodedBytes);
        assertTrue(FileUtils.contentEquals(inputFile, outputFile));
    }
}