验证码(jsp的形式)

来源:互联网 发布:如何联系淘宝客服 编辑:程序博客网 时间:2024/06/13 21:50
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ page import="java.util.Random"%><%@ page import="java.io.OutputStream"%><%@ page import="java.awt.Color"%><%@ page import="java.awt.Font"%><%@ page import="java.awt.Graphics"%><%@ page import="java.awt.image.BufferedImage"%><%@ page import="javax.imageio.ImageIO"%><%int width = 80;int height = 32;//create the imageBufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics g = image.getGraphics();// set the background colorg.setColor(new Color(0xDCDCDC));g.fillRect(0, 0, width, height);// draw the borderg.setColor(Color.black);g.drawRect(0, 0, width - 1, height - 1);// create a random instance to generate the codesRandom rdm = new Random();//转成16进制数据String hash1 = Integer.toHexString(rdm.nextInt());// make some confusionfor (int i = 0; i < 50; i++) {int x = rdm.nextInt(width);int y = rdm.nextInt(height);g.drawOval(x, y, 0, 0);}// generate a random codeString capstr = hash1.substring(0, 4);session.setAttribute("key", capstr);g.setColor(new Color(0, 100, 0));g.setFont(new Font("Candara", Font.BOLD, 24));g.drawString(capstr, 8, 24);g.dispose();response.setContentType("image/jpeg");out.clear();out = pageContext.pushBody();OutputStream strm = response.getOutputStream();ImageIO.write(image, "jpeg", strm);strm.close();%>

原创粉丝点击