⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 verifycode.java

📁 中国移动管理系统
💻 JAVA
字号:
package com.chinamobile.servlet;

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

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class verifyCode extends HttpServlet {

	private int xx = 15;
	private int fontHeight = 22;
	private int codeY = 20;
	private int width = 66;
	private int height = 26;
	private int codeNum = 5;

	public void init() throws ServletException {
	}
	
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, java.io.IOException {

		try {
			BufferedImage buffImg = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);
			Graphics2D g = buffImg.createGraphics();
			Random random = new Random();
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, width, height);
			Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);
			g.setFont(font);
			g.setColor(Color.BLACK);
			g.drawRect(0, 0, width - 1, height - 1);
			g.setColor(Color.BLACK);
			for (int i = 0; i < 8; i++) {
				int x = random.nextInt(width);
				int y = random.nextInt(height);
				int xl = random.nextInt(12);
				int yl = random.nextInt(12);
				g.drawLine(x, y, x + xl, y + yl);
			}
			String str = (String)req.getSession().getAttribute("rand");

			int red = 0, green = 0, blue = 0;
			for (int i = 0; i < 4; i++) {
				red = random.nextInt(255);
				green = random.nextInt(255);
				blue = random.nextInt(255);
				g.setColor(new Color(red, green, blue));
				g.drawString(str.charAt(i)+"", i * xx, codeY);
			}
			HttpSession session = req.getSession(false);
			session.setAttribute("servvali", str);
			resp.setHeader("Pragma", "no-cache");
			resp.setHeader("Cache-Control", "no-cache");
			resp.setDateHeader("Expires", 0);
			resp.setContentType("image/jpeg");
			ServletOutputStream sos = resp.getOutputStream();
			ImageIO.write(buffImg, "jpeg", sos);
			sos.close();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -