zoukankan      html  css  js  c++  java
  • 简易验证码

    package com.itcsl.web.servlet;
    
    import javax.imageio.ImageIO;
    import javax.servlet.ServletException;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.Random;
    
    @WebServlet("/CheckCodeServlet")
    public class CheckCodeServlet extends HttpServlet {
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		int width=100;
    		int height=50;
    		//1.创建一个对象,在内存中画图(验证码图片对象)
    		BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
    		//2.美化图片
    		//2.1填充背景色
    		Graphics graphics = image.getGraphics();//画笔对象
    		graphics.setColor(Color.pink);//设置画笔颜色
    		graphics.fillRect(0,0,width,height);
    		//2.2画边框
    		graphics.setColor(Color.BLUE);
    		graphics.drawRect(0,0,width-1,height-1);
    
    		String str="QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890";
    		//生成随机角标
    		Random random = new Random();
    		for (int  i=1;i<=4;i++){
    			int index = random.nextInt(str.length());
    			//获取字符
    			char ch = str.charAt(index);
    			//2.3写验证码
    			graphics.drawString(ch+"",width/5*i,height/2);
    		}
    		//2.4画干扰线
    		graphics.setColor(Color.GREEN);
    		//随机生成做坐标点
    		for (int i = 1; i <=10 ; i++) {
    			int x1 = random.nextInt(width);
    			int x2 = random.nextInt(width);
    
    			int y1 = random.nextInt(height);
    			int y2 = random.nextInt(height);
    			graphics.drawLine(x1,x2,y1,y2);
    		}
    		//3.将图片输出到页面展示
    		ImageIO.write(image,"jpg",response.getOutputStream());
    	}
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		this.doPost(request,response);
    	}
    }
    
    
    每个人都是在努力的路上,别因为别人的误解而放弃,,术业有专攻,如是而已。
  • 相关阅读:
    数据库索引概念与优化
    数据库查询效率分析
    C语言结构体与C++结构体之间的差异
    判断一个序列是否为栈的弹出序列
    C语言中的结构体
    C++ STL 中的 std::sort()
    Spring注入值到静态变量
    层次遍历二叉树
    计算二叉树的大小
    计算二叉树的高度
  • 原文地址:https://www.cnblogs.com/16699qq/p/13548661.html
Copyright © 2011-2022 走看看