1. Overview
1.概述
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.
在这个快速教程中,我们将介绍如何将图像文件编码为Base64String,然后使用Apache Common IO和Java 8的本地Base64功能对其进行解码以检索原始图像。
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.
这个操作可以应用于任何二进制文件或二进制数组。当我们需要以JSON格式传输二进制内容时,例如从移动应用程序到REST终端,它很有用。
For more information about Base64 conversion, check out this article here.
关于Base64转换的更多信息,请查看这里的文章。
2. Maven Dependency
2.Maven的依赖性
Let’s add the following dependencies to the pom.xml file:
让我们在pom.xml文件中添加以下依赖项。
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
You can find the latest version of Apache Commons IO on Maven Central.
您可以在Maven Central上找到Apache Commons IO的最新版本。
3. Convert Image File to Base64 String
3.将图像文件转换为Base64字符串
First of all, let’s read the file content to a byte array and use Java 8 Base64 class to encode it:
首先,让我们把文件内容读成一个字节数组,并使用Java 8 Base64类对其进行编码。
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.
encodedString是String中的字符集A-Za-z0-9+/,解码器拒绝任何超出此集的字符。
4. Convert Base64 String to Image File
4.将Base64String转换为图像文件
Now we have a Base64 String, let’s decode it back to binary content and write to a new file:
现在我们有了一个Base64String,让我们把它解码成二进制内容并写入一个新文件。
byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
FileUtils.writeByteArrayToFile(new File(outputFileName), decodedBytes);
5. Testing Our Code
5.测试我们的代码
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:
最后,我们可以通过读取一个文件,将其编码为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));
}
}
6. Conclusion
6.结论
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.
这篇文章直截了当地解释了将任何文件内容编码为Base64String,以及将Base64String解码为字节数组并使用Apache Common IO和Java 8功能将其保存到文件的基本方法。
As always, code snippets can be found over on GitHub.
像往常一样,代码片段可以在GitHub上找到over。