欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

简单实现Java验证码功能

程序员文章站 2024-02-23 18:05:34
今晚看到网上有关验证码的实现的代码,很早就想写一个了,感觉验证码挺有意思的,于是就写了一个,然而后来一直加载不出图片。尝试了很多方法,后来终于解决了,原来是我项目里面的 w...

今晚看到网上有关验证码的实现的代码,很早就想写一个了,感觉验证码挺有意思的,于是就写了一个,然而后来一直加载不出图片。尝试了很多方法,后来终于解决了,原来是我项目里面的 web.xml中没有部署servlet的映射,web.xml如下图:

简单实现Java验证码功能

运行效果如下:

简单实现Java验证码功能

代码如下:

package model; 
 
import javax.imageio.imageio; 
import java.awt.*; 
import java.awt.image.bufferedimage; 
import java.io.ioexception; 
import java.io.outputstream; 
import java.util.random; 
 
/** 
 * created by petty on 2017/5/4. 
 */ 
public class vcode { 
 
 private int w; //图片宽 
 private int h;//图片高 
 private color bgcolor = new color(240,240,240);//背景色 
 private random random = new random();//随机对象 
 //设置字体范围 
 private string[] fontnames = {"宋体","华文楷体","黑体","华文新魏","华文隶书","微软雅黑","楷体"}; 
 //设置字体样式范围 
 private int[] fontstyle = {0,1,2,3}; 
 //设置字号范围 
 private int[] fontsizes = {24,25,26,27,28}; 
 //设置所有字符串范围 
 private string codes="abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789"; 
 
 
 //无参数构造函数 
 public vcode(){ 
 
 } 
 
 //带宽和高的构造函数 
 public vcode(int w,int h){ 
  this.w = w; 
  this.h = h; 
 } 
 
 //返回一张背景图片 
 private bufferedimage createimage(){ 
  /** 
   * 1.创建图片 2.设置背景色 
   */ 
  //创建图片 
  bufferedimage img = new bufferedimage(w,h,bufferedimage.type_int_rgb); 
  //设置背景色 
  graphics g = img.getgraphics(); 
  g.setcolor(bgcolor); 
  g.fillrect(0,0,w,h); 
  return img; 
 } 
 
 
 // 随机返回字体颜色 
 private color randomcolor() { 
     int r = random.nextint(256); 
     int g = random.nextint(256); 
     int b = random.nextint(256); 
   return new color(r, g, b); 
   } 
 
 
 //随机返回字体样式 
 private font randomfont(){ 
  //随机生成字体下标,随机从给定的范围内获取一个字体 
  int index = random.nextint(fontnames.length); 
  string name = fontnames[index]; 
 
  //随机生成字体样式下标,随即从给定的范围内获取一个字体样式 
  index = random.nextint(fontstyle.length); 
  int style = fontstyle[index]; 
 
  //随机生成字体大小下标,随机从给定的范围内获取一个字体大小 
  index = random.nextint(fontsizes.length); 
  int size = fontsizes[index]; 
 
  return new font(name,style,size); 
 } 
 
 
 //随机返回字体内容 
 private string randomchar(){ 
  int index = random.nextint(codes.length()); 
 
  return codes.charat(index)+""; 
 } 
 
 
 //随机返回几条干扰线 
 private void getline(bufferedimage img){ 
  //设置干扰线的宽度为1.5倍宽,随机画五条 
  graphics2d g =(graphics2d) img.getgraphics(); 
  g.setcolor(color.black); 
  g.setstroke(new basicstroke(1.5f)); 
  for(int i=0;i<5;i++){ 
   int x1 = random.nextint(w); 
   int y1 = random.nextint(h); 
   int x2 = random.nextint(w); 
   int y2 = random.nextint(h); 
   g.drawline(x1,y1,x2,y2); 
  } 
 } 
 
 
 //用户调用该方法获取图片 
 public bufferedimage getimage(){ 
  /** 
   * 随机生成字符0-9a-za-z,设置字体,字号,是否粗体,字符颜色,都是随机的 
   */ 
  bufferedimage img = createimage(); 
 
  this.getline(img); 
 
  //获取画笔 
  graphics g = img.getgraphics(); 
 
  //画内容 
  for(int i=0;i<4;i++){ 
   g.setcolor(this.randomcolor());//获取随机颜色 
   g.setfont(this.randomfont());//获取随机字体 
   g.drawstring(this.randomchar(),w/4*i,h-5);//获取字符串随机内容 
  } 
  return img; 
 } 
 
 
 //用户调用该方法保存图片到本地 
 public void saveimage(bufferedimage img, outputstream ous){ 
 
  try { 
   imageio.write(img,"jpeg",ous); 
  } catch (ioexception e) { 
   e.printstacktrace(); 
  } 
 } 
}

package model; 
 
import javax.servlet.servletexception; 
import javax.servlet.annotation.webservlet; 
import javax.servlet.http.httpservlet; 
import javax.servlet.http.httpservletrequest; 
import javax.servlet.http.httpservletresponse; 
import java.awt.image.bufferedimage; 
import java.io.ioexception; 
 
/** 
 * created by petty on 2017/5/4. 
 */ 
@webservlet(name = "bservlet") 
public class bservlet extends httpservlet { 
 public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { 
  vcode v = new vcode(70,35); 
  bufferedimage img = v.getimage(); 
  v.saveimage(img,response.getoutputstream()); 
 } 
 
 public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { 
  vcode v = new vcode(70,35 ); 
  bufferedimage img = v.getimage(); 
  v.saveimage(img,response.getoutputstream()); 
 } 
} 

<%-- 
 created by intellij idea. 
 user: petty 
 date: 2017/5/4 
 time: 22:28 
 to change this template use file | settings | file templates. 
--%> 
<%@ page contenttype="text/html;charset=utf-8" language="java" %> 
<html> 
 <head> 
 <title>一次性验证码</title> 
 </head> 
 <body> 
 <form action="" method="get"> 
  <table align="center"> 
   <tr> 
    <td><img id="img" alt="" src="servlet/bservlet" onclick="changenext()"></td> 
   </tr> 
  </table> 
 </form> 
 </body> 
</html> 
<script type="text/javascript"> 
 function changenext(){ 
  var a=document.getelementbyid("img"); 
  a.src="servlet/bservlet?a="+new date().gettime(); 
 } 
</script>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。