• 生成验证码



    public
    class ValidataCode extends HttpServlet { private static final long serialVersionUID = -4144854995411893258L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { getValidataCode(request,response); } private final int WIDTH = 110; private final int HEIGHT = 40;//ctrl+shift+x转化大写 ctrl+shift+y转化小写 private void getValidataCode(HttpServletRequest request,HttpServletResponse response){ BufferedImage bufImg = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D graphics =bufImg.createGraphics(); graphics.setColor(new Color(250,250,210)); graphics.fillRect(0, 0, WIDTH, HEIGHT); graphics.setColor(new Color(233,150,122)); graphics.setFont(new Font("Consolas",Font.BOLD,22)); String validataStr = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"; Random random = new Random(); String validataCode=""; for(int i=0;i<4;i++){ char c = validataStr.charAt(random.nextInt(validataStr.length())); validataCode += c; int width = (i+1)*20; int radian = random.nextInt(61) - 30; graphics.rotate(radian*Math.PI/180, width, 20); graphics.drawString(c+"", width, 20); graphics.rotate(-radian*Math.PI/180, width, 20); graphics.drawLine(width, random.nextInt(HEIGHT), random.nextInt(WIDTH), random.nextInt(HEIGHT)); } graphics.drawLine(random.nextInt(WIDTH), random.nextInt(HEIGHT), random.nextInt(WIDTH), random.nextInt(HEIGHT)); HttpSession session =request.getSession();//获取session session.setAttribute("VALIDATACODE", validataCode);//设置session值 try { OutputStream out = response.getOutputStream(); ImageIO.write(bufImg, "png", out); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } }
  • 相关阅读:
    linux-CPU和核概念
    Nginx学习——location+proxy_pass左斜杠问题彻底弄清
    es概念一句话简介和注意点
    es-字段类型整理(6.x及以上)
    Nginx学习——location和rewrite
    Nginx学习——proxy_pass
    Nginx学习——简介及常用命令
    第三方接口调用异常补偿机制实现实例记录
    postgres日志爆盘处理方案-转自DBA汪x
    Xshell连接本地 Virtualbo Ubuntu
  • 原文地址:https://www.cnblogs.com/cpstart/p/6085328.html
Copyright © 2020-2023  润新知