源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

java生成验证码步骤归纳总结

  • 时间:2020-03-11 22:48 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:java生成验证码步骤归纳总结
[b]1、serialVersionUID[/b] [code]    private static final long serialVersionUID = -8501285780349046114L;[/code]     Java的序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的。     相当于java类的身份证。主要用于版本控制。 [b]2、BufferedImage类[/b]     ——BufferedImage 子类描述具有可访问图像数据缓冲区的 Image。      TYPE_INT_RGB            表示一个图像,它具有合成整数像素的 8 位 RGB 颜色分量。     ——getGraphics()            此方法返回 Graphics2D,但此处是出于向后兼容性的考虑。 [b]3、request.getParameter()[/b] request.getParameter() 方法传递的数据,会从Web客户端传到Web服务器端,代表HTTP请求数据。 request.getParameter()方法返回String类型的数据。 [b]4、String... [/b]excludeProperty表示不定参数,也就是调用这个方法的时候这里可以传入多个String对象(可变参数:适用于参数个数不确定,类型确定的情况,java把可变参数当做数组处理。 注意:可变参数必须位于最后一项eg: private String drawRandomNum(Graphics2D g,String... createTypeFlag)) [b]5、Graphics类[/b]    Graphics 类是所有图形上下文的抽象基类,允许应用程序在组件(已经在各种设备上实现)以及闭屏图像上进行绘制。 [b]【步骤】:[/b] [b]    一、在内存中创建一张图片; [/b]        [code] BufferedImage bi = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);[/code]   ——BufferedImage 子类描述具有可访问图像数据缓冲区的 Image。               TYPE_INT_RGB   表示一个图像,它具有合成整数像素的 8 位 RGB 颜色分量。  ——getGraphics()           此方法返回 Graphics2D,但此处是出于向后兼容性的考虑。   [b] 二、得到图片;[/b]         Graphics g = bi.getGraphics();     三、设置图片的背景色;         setBackGround(g);     四、设置图片的边框;         setBorder(g);     五、在图片上画干扰线;         drawRandomLine(g);     六、写在图片上随机数;         String random = drawRandomNum((Graphics2D) g,createTypeFlag);//根据客户端传递的createTypeFlag     七、将随机数存在session中;         request.getSession().setAttribute("checkcode",random);     八、设置响应头通知浏览器以图片的形式打开;         response.setContentType("image/jpeg");//等同于res[onse.setHeader("Content-Type","image/jpeg");     九、设置响应头控制浏览器不要缓存;
  response.setDateHeader("expries",-1);
  response.setHeader("Cache-Control","no-cache");
  response.setHeader("Pragma","no-cache");
        ——setDateHeader          [code]  public void setDateHeader(java.lang.String name,long date)[/code]              Parameters:                    name - the name of the header to set                     date - the assigned date value         ——setHeader [code]            public void setHeader(java.lang.String name, java.lang.String value)[/code]             Parameters:                  name - the name of the header                 value - the header value If it contains octet string, it should be encoded according to RFC 2047 ([url=http://www.ietf.org/rfc/rfc2047.txt]http://www.ietf.org/rfc/rfc2047.txt[/url]) [b]    十、将图片写给浏览器;[/b] [code]        ImageIO.write(bi,"jpg",response.getOutputStream());[/code]            ——参数:                 im - 要写入的 RenderedImage。                 formatName - 包含格式非正式名称的 String。                 output - 将在其中写入数据的 OutputStream。                     抛出:                  IllegalArgumentException - 如果任何参数为 null。                  IOException - 如果在写入过程中发生错误。      希望本篇文章对您有所帮助
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部