write Image via HttpServletResponse - Java 2D Graphics

Java examples for 2D Graphics:Image

Description

write Image via HttpServletResponse

Demo Code


import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

public class Main{
    public static void writeImage(HttpServletResponse response,
            BufferedImage bi) {/* www .ja  va  2  s  .  co m*/
        response.setHeader("Cache-Control", "private,no-cache,no-store");
        response.setContentType("image/png"); // PNGs allow for transparency. JPGs do not.
        try {
            writeImage(response.getOutputStream(), bi);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void writeImage(OutputStream os, BufferedImage bi) {
        try {
            ImageIO.write(bi, "png", os);
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related Tutorials