javaweb浏览器随机输出一张验证码图片

一.在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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>ResponseServlet</servlet-name>
<servlet-class>com.rjxy.model.ResponseServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ResponseServlet</servlet-name>
<url-pattern>/ResponseServlet</url-pattern>
</servlet-mapping>
</web-app>

二.创建一个ResponseServlet

package com.rjxy.model;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResponseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public static final int WIDTH = 120;
public static final int HEIGHT = 35;

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();

// 设置背景色
setBackGround(g);
// 设置边框
setBorder(g);
// 画干扰线
drawRandomLine(g);
// 写随机数
drawRandowNum((Graphics2D) g);
// 图形写给浏览器
response.setContentType("image/jpeg");
// 发头控制器不要缓存
response.setDateHeader("exprise", -1);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
ImageIO.write(image, "jpg", response.getOutputStream());

}

private void drawRandowNum(Graphics2D g) {
// 写随机字符,汉字在java中的实现是unicode存储,区间是0x4E00~0x9FA5
g.setColor(Color.RED);
g.setFont(new Font("宋体", Font.BOLD, 20));
String base = "\u7684\u4e00\u5e00\u6e00\u7e00\u8e00\u9e00";
int x = 10;
for (int i = 0; i < 4; i++) {
int degree = new Random().nextInt() % 30;// 30度范围内的旋转
String ch = base.charAt(new Random().nextInt(base.length())) + "";// 此处的双引号必不可少,缺少将被看做char而不是String

g.rotate(degree * Math.PI / 180, x, 20);// PI是圆周率
g.drawString(ch, x, 20);// x的初始值为10,表明第一个字的横坐标为10,然后执行x+10
g.rotate(-degree * Math.PI / 180, x, 20);// 表示第一次转的角度清零,不影响后续字体旋转
x += 30;

}
}

private void drawRandomLine(Graphics g) {
// 画干扰线,它的高小于HEIGHT,宽小于WIDTH
g.setColor(Color.green);
for (int i = 0; i < 4; i++) {

int x1 = new Random().nextInt(WIDTH);
int y1 = new Random().nextInt(HEIGHT);
int x2 = new Random().nextInt(WIDTH);
int y2 = new Random().nextInt(HEIGHT);
g.drawLine(x1, y1, x2, y2);
}

}

private void setBorder(Graphics g) {
// 设置边框颜色
g.setColor(Color.BLUE);
g.drawRect(1, 1, WIDTH - 2, HEIGHT - 2);

}

private void setBackGround(Graphics g) {
// 设置背景填充色
g.setColor(Color.yellow);
g.fillRect(0, 0, WIDTH, HEIGHT);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet(request, response);
}

}

上一篇:bzoj1143


下一篇:【NOIP 2015】Day2 T3 运输计划