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);
    	}
    }
    
    
    每个人都是在努力的路上,别因为别人的误解而放弃,,术业有专攻,如是而已。
  • 相关阅读:
    Javascript中字符串转数字
    Discover a powerful and suitable Javascript Automatic Testing Toolkit
    Homework Exercises 1
    Javascript实现关联数据(Linked Data)查询
    Javascript查询DBpedia小应用
    jQuery ajax —— 将类AJAX方法包装起来
    jQuery ajax —— 一些细节以及主函数扩展出来的方法
    无阻塞脚本加载方案
    SpringLayout的使用
    swing控件的一些操作
  • 原文地址:https://www.cnblogs.com/16699qq/p/13548661.html
Copyright © 2011-2022 走看看