-
-
Save soediro/38613f1e48d3e12450b2414321dbe9a8 to your computer and use it in GitHub Desktop.
Servlet for decode base64 of images. You would change content type for others type.
This file contains hidden or 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
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; | |
import java.io.ByteArrayInputStream; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
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.setContentType("image/png"); | |
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 = ImageIO.read(new 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