Skip to content

Instantly share code, notes, and snippets.

@guozi
Created April 6, 2022 03:35
Show Gist options
  • Save guozi/f7a229c418671cc55d90172639ef3d54 to your computer and use it in GitHub Desktop.
Save guozi/f7a229c418671cc55d90172639ef3d54 to your computer and use it in GitHub Desktop.
Image to Base64 String Conversion
public class Convert {
public static void main(String[] args) {
// Convert Image File to Base64 String
byte[] fileContent = FileUtils.readFileToByteArray(new File(filePath));
String encodedString = Base64.getEncoder().encodeToString(fileContent);
// Convert Base64 String to Image File
byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
FileUtils.writeByteArrayToFile(new File(outputFileName), decodedBytes);
}
}
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment