package com.admin.utils;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Verify_Code_Action extends HttpServlet {
private static final long serialVersionUID = 1L;
private Font getFont() {
Random random = new Random();
Font font[] = new Font[4];
font[0] = new Font("Microsoft YaHei", Font.BOLD, 24);
font[1] = new Font("Comic Sans MS", Font.BOLD, 25);
font[2] = new Font("Microsoft YaHei", Font.BOLD, 24);
font[3] = new Font("Courier New", Font.BOLD, 23);
return font[random.nextInt(4)];
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("image/jpeg");
resp.setHeader("Pragma", "No-cache");
resp.setHeader("Cache-Control", "No-cache");
resp.setDateHeader("Expires", 0);
OutputStream os = resp.getOutputStream();
int width = 98, height = 33;
BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Color c = g.getColor();
g.fillRect(0, 0, width, height);
Random random = new Random();
// 随机产生160条干扰线,使图象中的认证码不易被其它程序探测到。
g.setColor(Color.pink);
for (int i = 0; i < 88; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(12);
int y1 = random.nextInt(12);
g.drawLine(x, y, x + x1, y + y1);
}
char[] ch = "ABCDEFGHJKMNPQRSTUVWXYZ2345678".toCharArray();
int length = ch.length;
String sRand = "";
for (int i = 0; i < 4; i++) {
g.setFont(getFont());
String rand = new Character(ch[random.nextInt(length)]).toString();
sRand += rand;
g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
g.drawString(rand, 20 * i + 6, 25);
}
for (int i = 0; i < 20; i++) {
int x1 = random.nextInt(width);
int y1 = random.nextInt(height);
g.drawOval(x1, y1, 2, 2);
}
g.setColor(c);
g.dispose();
req.getSession().setAttribute("verify_code", sRand);
System.out.println("verifycode----"+req.getSession().getAttribute("verify_code"));
ImageIO.write(image, "JPEG", os);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Verify_Code_Action extends HttpServlet {
private static final long serialVersionUID = 1L;
private Font getFont() {
Random random = new Random();
Font font[] = new Font[4];
font[0] = new Font("Microsoft YaHei", Font.BOLD, 24);
font[1] = new Font("Comic Sans MS", Font.BOLD, 25);
font[2] = new Font("Microsoft YaHei", Font.BOLD, 24);
font[3] = new Font("Courier New", Font.BOLD, 23);
return font[random.nextInt(4)];
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("image/jpeg");
resp.setHeader("Pragma", "No-cache");
resp.setHeader("Cache-Control", "No-cache");
resp.setDateHeader("Expires", 0);
OutputStream os = resp.getOutputStream();
int width = 98, height = 33;
BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Color c = g.getColor();
g.fillRect(0, 0, width, height);
Random random = new Random();
// 随机产生160条干扰线,使图象中的认证码不易被其它程序探测到。
g.setColor(Color.pink);
for (int i = 0; i < 88; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(12);
int y1 = random.nextInt(12);
g.drawLine(x, y, x + x1, y + y1);
}
char[] ch = "ABCDEFGHJKMNPQRSTUVWXYZ2345678".toCharArray();
int length = ch.length;
String sRand = "";
for (int i = 0; i < 4; i++) {
g.setFont(getFont());
String rand = new Character(ch[random.nextInt(length)]).toString();
sRand += rand;
g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
g.drawString(rand, 20 * i + 6, 25);
}
for (int i = 0; i < 20; i++) {
int x1 = random.nextInt(width);
int y1 = random.nextInt(height);
g.drawOval(x1, y1, 2, 2);
}
g.setColor(c);
g.dispose();
req.getSession().setAttribute("verify_code", sRand);
System.out.println("verifycode----"+req.getSession().getAttribute("verify_code"));
ImageIO.write(image, "JPEG", os);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}