Sample code for 30+ languages & platforms
Java

Extract data:image/png;base64 from HTML

See more Base64 Examples

Demonstrates how to extract base64 image data from HTMl and save to files.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        BinData bd = new BinData();
        StringBuilder sbFilename = new StringBuilder();
        int index = 1;

        sb.loadFile("qa_data/html/oresp body.txt", "utf-8");
        // Assume success.
        System.out.println("length = " + sb.getLength());

        boolean mayHaveMore = true;
        while (mayHaveMore) {
            // Get the base64 between the 1st occurrence "data:image/png;base64," and "'"
            String sBase64 = sb.getBetween("data:image/png;base64,", "'");

            // If nothing is found, then we'll exit the loop because there are no more.
            mayHaveMore = sb.getLastMethodSuccess();

            if (sb.getLastMethodSuccess()) {
                // Found something.
                // Load into bd and save.
                bd.loadEncoded(sBase64, "base64");

                sbFilename.setString("qa_output/png_");
                sbFilename.appendInt(index);
                sbFilename.append(".png");

                bd.writeFile(sbFilename.getAsString());

                // Replace "data:image/png;base64" with "data:image-png;base64" so the next iteration finds the next occurrence.
                sb.replaceFirst("data:image/png;base64", "data:image-png;base64");
            }

            index = index + 1;
        }

        // Restore our replacements..
        int count = sb.replace("data:image-png;base64", "data:image/png;base64");

        System.out.println("All done.");
    }
}