Created
December 5, 2014 16:36
-
-
Save wallace7souza/4456dccad1e3d5d0a564 to your computer and use it in GitHub Desktop.
Java base64 image text to PNG file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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