1 /* 2 * To change this template, choose Tools | Templates 3 * and open the template in the editor. 4 */ 5 package com.fleming.servlet; 6 7 import java.awt.Color; 8 import java.awt.Font; 9 import java.awt.Graphics; 10 import java.awt.image.BufferedImage; 11 import java.io.IOException; 12 import java.io.PrintWriter; 13 import java.util.Random; 14 import javax.imageio.ImageIO; 15 import javax.servlet.ServletException; 16 import javax.servlet.ServletOutputStream; 17 import javax.servlet.http.HttpServlet; 18 import javax.servlet.http.HttpServletRequest; 19 import javax.servlet.http.HttpServletResponse; 20 import javax.servlet.http.HttpSession; 21 22 /** 23 * 24 * @author Fleming Start 25 */ 26 public class RandomCodeImageServlet extends HttpServlet { 27 28 private final int width = 60; 29 private final int height = 20; 30 private final String charset = "1234567890abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; 31 32 public RandomCodeImageServlet() { 33 super(); 34 } 35 36 /** 37 * Processes requests for both HTTP 38 * <code>GET</code> and 39 * <code>POST</code> methods. 40 * 41 * @param request servlet request 42 * @param response servlet response 43 * @throws ServletException if a servlet-specific error occurs 44 * @throws IOException if an I/O error occurs 45 */ 46 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 47 throws ServletException, IOException { 48 response.setContentType("text/html;charset=UTF-8"); 49 // PrintWriter out = response.getWriter(); 50 try { 51 52 BufferedImage bufferedImage = new BufferedImage(width, height, 53 BufferedImage.TYPE_INT_RGB); 54 Graphics graphics = bufferedImage.getGraphics(); 55 56 Random random = new Random(); 57 58 graphics.setColor(Color.WHITE); 59 graphics.fillRect(0, 0, width, height); // 填充背景 60 61 graphics.setColor(Color.GRAY); 62 graphics.drawRect(0, 0, width - 1, height - 1); // 绘制边框 63 64 // 生成干扰线 65 graphics.setColor(new Color(169, 169, 169)); 66 for (int i = 1; i <= 30; i++) { 67 68 int x = random.nextInt(width); 69 int y = random.nextInt(height); 70 int xwidth = random.nextInt(20); 71 int yheight = random.nextInt(20); 72 73 graphics.drawLine(x, y, xwidth, yheight); 74 } 75 76 Font font = new Font("Courier New", Font.BOLD, 18); 77 graphics.setFont(font); 78 int red; 79 int green; 80 int blue; 81 82 char[] codeSource = charset.toCharArray(); // 验证码源 83 int length = codeSource.length; 84 85 StringBuffer randomCode = new StringBuffer(); // 用于存储随机字符 86 for (int i = 0; i < 5; i++) { 87 88 String code = String 89 .valueOf(codeSource[random.nextInt(length)]); 90 red = random.nextInt(110); 91 green = random.nextInt(50); 92 blue = random.nextInt(50); 93 graphics.setColor(new Color(red, green, blue)); 94 graphics.drawString(code, 10 * i + 8, 16); 95 randomCode.append(code); 96 } 97 98 // 将验证码字符串写入session 99 HttpSession session = request.getSession(); 100 session.setAttribute("randomCode", randomCode.toString()); 101 graphics.dispose(); 102 103 // 禁止图像缓存 104 response.setHeader("Pragma", "no-cache"); 105 response.setHeader("Cache-Control", "no-cache"); 106 response.setDateHeader("Expires", 0); 107 108 response.setContentType("image/jpeg"); 109 ServletOutputStream sos = response.getOutputStream(); 110 ImageIO.write(bufferedImage, "jpeg", sos); 111 112 } finally { 113 // out.close(); 114 } 115 } 116 117 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> 118 /** 119 * Handles the HTTP 120 * <code>GET</code> method. 121 * 122 * @param request servlet request 123 * @param response servlet response 124 * @throws ServletException if a servlet-specific error occurs 125 * @throws IOException if an I/O error occurs 126 */ 127 @Override 128 protected void doGet(HttpServletRequest request, HttpServletResponse response) 129 throws ServletException, IOException { 130 processRequest(request, response); 131 } 132 133 /** 134 * Handles the HTTP 135 * <code>POST</code> method. 136 * 137 * @param request servlet request 138 * @param response servlet response 139 * @throws ServletException if a servlet-specific error occurs 140 * @throws IOException if an I/O error occurs 141 */ 142 @Override 143 protected void doPost(HttpServletRequest request, HttpServletResponse response) 144 throws ServletException, IOException { 145 processRequest(request, response); 146 } 147 148 /** 149 * Returns a short description of the servlet. 150 * 151 * @return a String containing servlet description 152 */ 153 @Override 154 public String getServletInfo() { 155 return "Short description"; 156 }// </editor-fold> 157 }