Create a gist now

Instantly share code, notes, and snippets.

Servlet for decode base64 of images. You would change content type for others type.
package cl.puas.web.servlet;
import sun.misc.BASE64Decoder;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
public class Base64Servlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String imageBase64 = req.getParameter("base64");
OutputStream out = resp.getOutputStream();
writeOutputStream(imageBase64, out);
resp.setHeader("Pragma", "");
resp.setHeader("Cache-Control", "");
resp.setHeader("Content-Disposition", "inline; fileName=image.png");
private void writeOutputStream(String value, OutputStream outputStream) throws IOException {
BASE64Decoder decoder = new BASE64Decoder();
byte[] imgBytes = decoder.decodeBuffer(value);
BufferedImage bufImg = ByteArrayInputStream(imgBytes));
ImageIO.write(bufImg, "png", outputStream);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment