Java tutorial
/****************************************************************************** * @Copyright Notice: * Copyright (c) 2012 SYNNEX Corporation. All Rights Reserved. * This software is published under the terms of the SYNNEX Software * License version 1.0, a copy of which has been included with this * distribution in the LICENSE.txt file. * *****************************************************************************/ package com.synnex.saas.platform.core.servlet; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.commons.lang.RandomStringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * @author stevenl * @datetime 2015-3-3 ?1:39:05 */ public class CaptchaServlet extends HttpServlet { private static final long serialVersionUID = -5002751698069912752L; private static Logger logger = LoggerFactory.getLogger(CaptchaServlet.class); public CaptchaServlet() { super(); } public void destroy() { super.destroy(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { int width = 50; int height = 18; String captchaCode = RandomStringUtils.random(4, true, true); HttpSession session = request.getSession(true); session.setAttribute("captchaCode", captchaCode); response.setContentType("images/jpeg"); response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); ServletOutputStream out = response.getOutputStream(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); g.setColor(getRandColor(200, 250)); g.fillRect(0, 0, width, height); Font mFont = new Font("Times New Roman", Font.BOLD, 18); g.setFont(mFont); g.setColor(getRandColor(160, 200)); Random random = new Random(); for (int i = 0; i < 155; i++) { int x2 = random.nextInt(width); int y2 = random.nextInt(height); int x3 = random.nextInt(12); int y3 = random.nextInt(12); g.drawLine(x2, y2, x2 + x3, y2 + y3); } g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110))); g.drawString(captchaCode, 2, 16); g.dispose(); ImageIO.write((BufferedImage) image, "JPEG", out); out.close(); } catch (Exception e) { logger.error("Generate captcha failed.", e); } } private Color getRandColor(int fc, int bc) { Random random = new Random(); if (fc > 255) { fc = 255; } if (bc > 255) { bc = 255; } int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public void init() throws ServletException { } }