亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java驗(yàn)證碼圖片生成代碼

 更新時(shí)間:2017年03月06日 08:37:38   作者:J倉(cāng)怕加x  
這篇文章主要為大家詳細(xì)介紹了Java驗(yàn)證碼圖片生成代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Java生成驗(yàn)證碼圖片的具體代碼,供大家參考,具體內(nèi)容如下

1、首先新建一各專(zhuān)門(mén)生成驗(yàn)證碼圖片的類(lèi)VerifyCode:

public class VerifyCode {


private int w=70;
private int h=35;
private Random r=new Random();
//宋體,華文楷書(shū),黑體,華文新魏,華文隸書(shū),微軟雅黑,楷體_GB2312,Times New Roman
private String [] fontNames={"宋體","華文楷書(shū)","黑體","華文新魏","微軟雅黑","華文隸書(shū)","楷體_GB2312","Times New Roman"};
//可選的字符
private String codes="23456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";
//背景色
private Color bgColor=new Color(255,255,255);
//驗(yàn)證碼上的文本
private String text;

//生成隨機(jī)顏色
private Color randomColor(){
 int red=r.nextInt(150);
 int green=r.nextInt(150);
 int blue=r.nextInt(150);
 return new Color(red,green,blue);
}

//生成隨機(jī)字體
private Font randomFont(){
int index=r.nextInt(fontNames.length);//獲取下標(biāo)
String fontName=fontNames[index];//生成隨機(jī)的字體名稱
int style=r.nextInt(4);//獲取隨機(jī)樣式,0表示無(wú)樣式,1表示粗體,2表示斜體,3表示粗體加斜體
int size=r.nextInt(5)+24;//生成隨機(jī)字號(hào),24~28
return new Font(fontName,style,size);
}

//畫(huà)干擾線
private void drawLine(BufferedImage image){
int num=3;//一共畫(huà)3條
Graphics2D g2=(Graphics2D) image.getGraphics();
for(int i=0;i<num;i++){//隨機(jī)生成坐標(biāo),即4個(gè)值
int x1=r.nextInt(w);
int y1=r.nextInt(h);
int x2=r.nextInt(w);
int y2=r.nextInt(h);
g2.setStroke(new BasicStroke(1.5F));//設(shè)置筆畫(huà)的寬度
  g2.setColor(Color.blue);//干擾線顏色
  g2.drawLine(x1, y1, x2, y2);//畫(huà)線
}
}

//隨機(jī)生成一個(gè)字符
private char randomChar(){
int index=r.nextInt(codes.length());
return codes.charAt(index);
}

//創(chuàng)建BufferedImage
private BufferedImage createImage(){
BufferedImage image=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
Graphics2D g2=(Graphics2D) image.getGraphics();
g2.setColor(this.bgColor);
g2.fillRect(0, 0, w, h);
return image;
}

//外界調(diào)用這個(gè)方法得到驗(yàn)證碼
public BufferedImage getImage(){
BufferedImage image=createImage();//創(chuàng)建圖片緩沖區(qū)
Graphics2D g2=(Graphics2D)image.getGraphics();//得到繪制環(huán)境
StringBuilder sb=new StringBuilder();//用來(lái)裝載生成的驗(yàn)證碼文本
//向圖中畫(huà)4個(gè)字符
for(int i=0;i<4;i++){//循環(huán)4次
String s=randomChar()+"";//隨機(jī)生成一個(gè)字符
sb.append(s);//把字母添加到sb中
double x=i*1.0*w/4;//設(shè)置當(dāng)前字符的x軸坐標(biāo)
g2.setFont(randomFont());//設(shè)置隨機(jī)字體
g2.setColor(randomColor());//設(shè)置隨機(jī)顏色
g2.drawString(s, (int)x, h-5);
}
this.text=sb.toString();//把生成的字符串賦給了this.text
drawLine(image);//添加干擾線
return image;
}

//返回驗(yàn)證碼圖片上面的文本
public String getText(){
return text;
}

//保存圖片到指定的輸出流
public static void output(BufferedImage image,OutputStream out) throws IOException{
ImageIO.write(image, "JPEG", out);
}
}

2、在jsp頁(yè)面上應(yīng)用:

其核心代碼如下:

<form action="/test/LoginServlet" method="post">
   用戶名:<input type="text" name="username" value="<%=uname %>"><br>
   密 碼:<input type="password" name="password"><br>
   驗(yàn)證碼:<input type="text" name="verifyCode">
  <img id="img" src="/test/VerifyCodeServlet">
  <a href="JavaScript:_change()" rel="external nofollow" >看不清換一張</a><br>
  <input type="submit" value="登錄">
 </form>

看不清換一張使用javascript實(shí)現(xiàn),代碼如下:

   <script type="text/javascript">
      function _change(){
        /**
        *1.得到image圖片
        *2.修改其src為/test/VerifyCodeServlet 
        */
        var imgEle=document.getElementById("img");
        imgEle.src="/test/VerifyCodeServlet?a="+new Date().getTime();

      }
  </script>

 +new Date().getTime()這條語(yǔ)句可以欺騙瀏覽器,以為請(qǐng)求是新的,實(shí)現(xiàn)驗(yàn)證碼的改變。

3、在第2步中,VerifyCodeServlet的實(shí)現(xiàn)生成一張驗(yàn)證碼,并且通過(guò)session域保存驗(yàn)證碼圖片對(duì)應(yīng)的文本內(nèi)容,為后面的登錄驗(yàn)證驗(yàn)證碼是否正確做準(zhǔn)備。核心代碼如下:

public class VerifyCodeServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


/*
* 1.生成圖片
* 2.保存圖片上的文本到session域中
* 3.把圖片響應(yīng)給客戶端
*/
VerifyCode vc=new VerifyCode();
BufferedImage image=vc.getImage();
//保存圖片上的文本到session域
request.getSession().setAttribute("session_code", vc.getText());

VerifyCode.output(image, response.getOutputStream());

  }
}

4、最后一步,實(shí)現(xiàn)登錄驗(yàn)證驗(yàn)證碼是否正確,其核心代碼如下:

public class LoginServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


/*
* 校驗(yàn)驗(yàn)證碼
* 1.從session中獲取正取的驗(yàn)證碼
* 2.從表單中獲取用戶填寫(xiě)的驗(yàn)證碼
* 3.進(jìn)行比較
* 4.如果相同,向下運(yùn)行,否則保存錯(cuò)誤信息到request域,轉(zhuǎn)發(fā)到login.jsp
*/
String sessionCode=(String)                    

 request.getSession().getAttribute("session_code");//獲得的驗(yàn)證碼里面對(duì)應(yīng)的文本內(nèi)容
String paramCode=request.getParameter("verifyCode");//用戶輸入文本框里面的內(nèi)容
if(!paramCode.equalsIgnoreCase(sessionCode)){
request.setAttribute("msg", "驗(yàn)證碼錯(cuò)誤!");
request.getRequestDispatcher("/login.jsp").forward(request, response);//打回到登錄界面。
return;//返回,不繼續(xù)執(zhí)行下面的語(yǔ)句。
}
}
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論