import
java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.image. * ;
import java.util.Random;
public class ImageServlet extends HttpServlet ... {
public ImageServlet() ...{
super();
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException ...{
Random r = new Random(); //创建随机数产生器
res.setContentType("image/jpeg"); //设置内容类型为image/jpeg
int width = 200,height = 200;
//创建缓冲图
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.white);
g.fillRect(0,0,width,height);
Polygon poly = new Polygon(); //创建多边形对象
for(int i = 0; i < 5; i ++)
...{
poly.addPoint(r.nextInt(width), r.nextInt(height)); //随机生成多边形
}
g.setColor(Color.cyan); //设置颜色
g.fillPolygon(poly); //填充
g.dispose(); //销毁
//输出
ServletOutputStream sos = res.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
encoder.encode(image);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
doPost(request,response);
}
}
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.image. * ;
import java.util.Random;
public class ImageServlet extends HttpServlet ... {
public ImageServlet() ...{
super();
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException ...{
Random r = new Random(); //创建随机数产生器
res.setContentType("image/jpeg"); //设置内容类型为image/jpeg
int width = 200,height = 200;
//创建缓冲图
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.white);
g.fillRect(0,0,width,height);
Polygon poly = new Polygon(); //创建多边形对象
for(int i = 0; i < 5; i ++)
...{
poly.addPoint(r.nextInt(width), r.nextInt(height)); //随机生成多边形
}
g.setColor(Color.cyan); //设置颜色
g.fillPolygon(poly); //填充
g.dispose(); //销毁
//输出
ServletOutputStream sos = res.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
encoder.encode(image);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
doPost(request,response);
}
}