package com.xmyself.model; import java.awt.image.BufferedImage; public class VerifyCode { PRivate String code; private BufferedImage image; public VerifyCode(String code, BufferedImage image) { this.code = code; this.image = image; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public BufferedImage getImage() { return image; } public void setImage(BufferedImage image) { this.image = image; }}生成隨機驗證碼和圖片的類VerifyCodeFactory.java,這并不是靜態工廠,只是一個名稱而已
package com.xmyself.factory; import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.util.Random; import com.xmyself.model.VerifyCode; public class VerifyCodeFactory { public static VerifyCode create() { String code = getRandomString(4); return new VerifyCode(code, drawImage(code)); } private static String getRandomString(int length) { if (length < 0 || length > 8) length = 4; String scope = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; StringBuffer code = new StringBuffer(); Random random = new Random(); for (int i = 0; i < length; i++) { code.append(scope.charAt(random.nextInt(scope.length() - 1))); } return code.toString(); } private static Color getRandomColor() { Random random = new Random(); int r = random.nextInt(255); int g = random.nextInt(255); int b = random.nextInt(255); return new Color(r, g, b); } private static BufferedImage drawImage(String code) { int width = 80; int height = 34; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //畫邊框和背景 Graphics brush = image.getGraphics(); brush.setColor(getRandomColor()); brush.fillRect(0, 0, width - 1, height - 1); brush.drawRect(0, 0, width - 1, height - 1); //畫驗證碼 brush.setColor(getRandomColor()); brush.setFont(new Font("Times New Roman", Font.ITALIC, 28)); brush.drawString(code, 7, 22); brush.dispose(); return image; }}創建servlet類VerifyCodeServlet.javapackage com.xmyself.servlet; import java.io.IOException; import javax.imageio.ImageIO;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import com.xmyself.factory.VerifyCodeFactory;import com.xmyself.model.VerifyCode; public class VerifyCodeServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void init(ServletConfig config) throws ServletException { } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { } public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { VerifyCode vc = VerifyCodeFactory.create(); request.getsession().setAttribute("verifyCode", vc.getCode()); ImageIO.write(vc.getImage(), "JPEG", response.getOutputStream()); } public void destroy() { }}配置web.xml<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>servlet</display-name> <servlet> <servlet-name>test</servlet-name> <servlet-class>com.xmyself.servlet.VerifyCodeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>test</servlet-name> <url-pattern>/test</url-pattern> </servlet-mapping></web-app>啟動web工程,訪問以下鏈接即可看到驗證碼圖片http://localhost:8080/servlet/test
新聞熱點
疑難解答