生成JPEG所需的具体步骤
1.创建一个BufferedImage
int width = ...;
int height = ....;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
2.在BufferedImage上绘制内容
Graphics2D g2d = (Graphics2D)image.getGraphics();
g2d.setXXX(...);
g2d.fill(...);
g2d.draw(...);
3. 设置Content-Type响应报头
reponse.setContentType("image/jpeg");
4. 获取输出流
OutputStream out = response.getOutputStream();
5.以JPEG格式将BufferedImage发送到输出流
try{
ImageIO.writer(image, "jpg", out);
}catch(IOException ioe){
System.err.println("Error writing jpeg file: " + ioe);
}