Java實現圖片驗證碼具體代碼_java

來源:互聯網
上載者:User

本文執行個體為大家分享了Java圖片驗證碼代碼,供大家參考,具體內容如下

網頁顯示效果:

index.jsp
使用兩種方式強製圖片更新:
1、設定圖片輸出時不緩衝。
2、用js改變請求的地址。

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><html> <head> <title>第一個web項目的首頁</title> <script type="text/javascript">  function changeImg(){   var _img=document.getElementById("_img");   var time=new Date().getTime();//瀏覽器認為相同的動作(被過濾)沒必要應答,所以用時間值改變src值(即請求)(事實上是同一請求)   _img.src="confirm?"+time;//用js改變請求的地址  } </script> </head> <body> <h2>首頁首頁</h2> <h2>湖南長沙</h2> <h3>發布方式一......</h3> <img src="images/1.jpg"/> <br/> <a href="jsps/a.jsp">去到子頁</a> <hr/> <form >  姓名:<input type="text" name="nmae"/><br/>   密碼:<input type="password" name="pwd"/> <br/>    確認密碼:<input type="text" name=pwd2/> <br/>    驗證碼:<input type="text" /><img id="_img" src="confirm"/>  <!-- 不可行   <a href="confirm">看不清</a> <br/>   -->   <a href="javascript:changeImg();">看不清</a> <br/>  <input type="submit" value="註冊"/>  </form> </body></html>

自動產生驗證碼的java代碼

關鍵類(以下類為基礎部分的內容)
BufferedImage
ImageIO
Graphics/Graphics2D

import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;import javax.imageio.ImageIO;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ImageServlet extends HttpServlet{ @Override protected void service(HttpServletRequest req, HttpServletResponse resp)   throws ServletException, IOException {//  System.out.println("jinlaile");  resp.setContentType("image/jpeg");//jpeg是圖片格式。設定響應內容的類型為jpeg的圖片  int width=64;  int height=40;  BufferedImage bImg=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  Graphics g=bImg.getGraphics();  //背景  g.setColor(Color.white);  g.fillRect(0, 0, width, height);  //字型顏色  g.setFont(new Font("aa", Font.BOLD,18));  //用隨機數產生驗證碼:4個0~9以內的整數  Random r=new Random();  for(int i=0;i<=4;i++){   int t=r.nextInt(10);//10以內的隨機整數   int y=10+r.nextInt(20);//上下位置:10~30   Color c=new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));   g.setColor(c);   g.drawString(""+t, i*16, y);  }  //畫幹擾線  for(int i=1;i<8;i++){   Color c=new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));   g.setColor(c);   g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));  }  //把圖形刷到bImg對象中  g.dispose();//相當於IO中的close()方法帶自動flush();  ImageIO.write(bImg,"JPEG", resp.getOutputStream());//通過resp擷取req的outputStream對象,發向用戶端的socket的封裝,即寫到用戶端 }}

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0"  xmlns="http://java.sun.com/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name>  <!-- 訪問安全目錄WEB-INF中的b.jsp檔案 --> <servlet> <servlet-name>bbbb</servlet-name> <jsp-file>/WEB-INF/b.jsp</jsp-file> </servlet> <!-- 訪問產生驗證碼的java代碼 --> <servlet> <servlet-name>img</servlet-name> <servlet-class>cn.hncu.servlet.ImageServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>bbbb</servlet-name> <url-pattern>/bb.asp</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>img</servlet-name> <url-pattern>/confirm</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.