package test.web.home.servlet; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.FileOutputStream; import java.io.OutputStream; import java.io.PrintStream; import java.util.Random; import javax.imageio.ImageIO; public abstract class RandomGraphic { private static int wordHeight = 10; private static int wordWidth = 15; private static int fontSize = 20; private static final int MAX_CHARCOUNT = 16; private static final int initypos = 5; private static final Color[] CHAR_COLOR = { Color.RED, Color.BLUE, Color.GREEN, Color.MAGENTA }; public static String GRAPHIC_JPEG = "JPEG"; public static String GRAPHIC_PNG = "PNG"; public static String draw(String charValue, String graphicFormat, OutputStream out) throws Exception { int charCount = charValue.length(); if ((charCount < 1) || (charCount > 16)) { throw new Exception("Invalid parameter charCount,charCount should between in 1 and 16"); } int w = (charCount + 2) * wordWidth; int h = wordHeight * 3; BufferedImage bi = new BufferedImage(w, h, 5); Graphics2D g = bi.createGraphics(); Color backColor = Color.WHITE; g.setBackground(backColor); g.fillRect(0, 0, w, h); g.setFont(new Font(null, 1, fontSize)); for (int i = 0; i < charCount; i++) { String c = charValue.substring(i, i + 1); Color color = CHAR_COLOR[randomInt(0, CHAR_COLOR.length)]; g.setColor(color); int xpos = (i + 1) * wordWidth; int ypos = randomInt(5 + wordHeight, 5 + wordHeight * 2); g.drawString(c, xpos, ypos); } g.dispose(); bi.flush(); ImageIO.write(bi, graphicFormat, out); out.close(); return charValue; } public static String randNumber(int charCount) { String charValue = ""; for (int i = 0; i < charCount; i++) { charValue = charValue + String.valueOf(randomInt(0, 10)); } return charValue; } public static String randAlpha(int charCount) { String charValue = ""; for (int i = 0; i < charCount; i++) { char c = (char)(randomInt(0, 26) + 97); charValue = charValue + String.valueOf(c); } return charValue; } protected static int randomInt(int from, int to) { Random r = new Random(); return from + r.nextInt(to - from); } public static void main(String[] args) throws Exception { System.out.println(draw(randAlpha(4), GRAPHIC_PNG, new FileOutputStream("c:/myimg.png"))); } }
package test; import test.LogProxy; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import test.web.boot.api.ModuleBaseServlet; import test.web.utils.http.ResultCode; import test.web.utils.http.ResultProxy; public class AuthCode extends ModuleBaseServlet { private static final long serialVersionUID = 1L; private LogProxy logg = LogProxy.getProxy(getClass().getName()); public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { try { String randomCode = RandomGraphic.randAlpha(4); this.logg.info("###> 【验证码】doGet==> randomCode:" + randomCode, new Object[0]); request.getSession().setAttribute("SESSION_KEY_AUTHCODE", randomCode); RandomGraphic.draw(randomCode, RandomGraphic.GRAPHIC_PNG, response.getOutputStream()); } catch (Exception e) { e.printStackTrace(); ResultProxy.getJSONResult().setCode(ResultCode.ERROR).send(response); } } }