java生成验证码图片
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); // 1.设置背景色 setBackGround(g); // 2.设置边框 setBorder(g); // 3.画干扰线 drawRandomLine(g); // 4.写随机数 drawRandomNum(g); // 5.图形写给浏览器 response.setContentType("image/jpeg"); ImageIO.write(image, "jpg", response.getOutputStream()); } private void drawRandomNum(Graphics g) { g.setColor(Color.RED); g.setFont(new Font("宋体",Font.BOLD,20)); //所有汉字区间 \u4e00 \u9fa5 for (int i = 0; i < 4; i++) { int r=new Random().nextInt(100); int r2=new Random().nextInt(100); g.drawString("好", r, r2); } } private void drawRandomLine(Graphics g) { g.setColor(Color.GREEN); for (int i = 0; i < 5; i++) { int x1 = new Random().nextInt(100); int y1 = new Random().nextInt(100); int x2 = new Random().nextInt(100); int y2 = new Random().nextInt(100); g.drawLine(x1, y1, x2, y2); } } private void setBorder(Graphics g) { g.setColor(Color.BLUE); g.drawRect(1, 1, 98, 98); } private void setBackGround(Graphics g) { g.setColor(Color.WHITE); g.fillRect(0, 0, 100, 100); }