Skip to content

Instantly share code, notes, and snippets.

@wallace7souza
Created December 5, 2014 16:36
Show Gist options
  • Save wallace7souza/4456dccad1e3d5d0a564 to your computer and use it in GitHub Desktop.
Save wallace7souza/4456dccad1e3d5d0a564 to your computer and use it in GitHub Desktop.
Java base64 image text to PNG file
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.apache.commons.codec.binary.Base64;
public class Base64FileToImage {
public static void main(String[] args) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get("/Temp/base64-img.txt"));
String ct = new String(encoded);
String imageDataBytes = ct.substring(ct.indexOf(",")+1);
// Note preferred way of declaring an array variable
byte[] data = Base64.decodeBase64(imageDataBytes);
try (
OutputStream stream = new FileOutputStream("/Temp/base64.png")) {
stream.write(data);
stream.flush();
stream.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment