java实现随机验证码的图片
生活随笔
收集整理的這篇文章主要介紹了
java实现随机验证码的图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
鏈接地址:http://blog.sina.com.cn/s/blog_407a68fc010006qo.html
1、一共需要2個常用java文件(RandomCode.java和RandomCodeCtrl.java): (a、)RandomCode.java是個普通的java文件;內容如下: import java.awt.Color;import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; public class RandomCode {
???/**
????* 隨機取得一個字體
????* @param Random random ?隨機數
????* @return Font 返回一個新字體
????*/?
??private Font getsFont(Random random){
???return new Font("Fixedsys",Font.CENTER_BASELINE,18);
??}
???/**
????* 返回一個隨機顏色
????* @param int fc ?隨機數
????* @param int bc ?隨機數
????* @param Random random ?隨機數
????* @return Color 返回一個新顏色
????*/?
??private Color getRandColor(int fc,int bc,Random random){
????????if(fc>255) fc=255;
????????if(bc>255) bc=255;
????????int r=fc+random.nextInt(bc-fc-6);
????????int g=fc+random.nextInt(bc-fc-4);
????????int b=fc+random.nextInt(bc-fc-8);
????????return new Color(r,g,b);
????}
???/**
????* 生成隨機數圖片
????*/?
??public void getRandcode(HttpServletRequest request,HttpServletResponse response)throws Exception{
???System.setProperty("java.awt.headless","true");
???HttpSession session = request.getSession();
???int width=80, height=22;//設置圖片大小
???BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
???Graphics g = image.getGraphics();
???Random random = new Random();
???g.fillRect(1, 1, width, height);//設定邊框
???g.setFont(new Font("Times New Roman",Font.ROMAN_BASELINE,18));
???g.setColor(getRandColor(111,133,random));
???//產生隨機線
???for (int i=0;i<11;i++){
????int x = random.nextInt(width);
????int y = random.nextInt(height);
????int xl = random.nextInt(13);
????int yl = random.nextInt(15);
????g.drawLine(x,y,x+xl,y+yl);
???}
???//產生隨機點
???g.setColor(getRandColor(130,150,random));
???//產生5個隨機數
???String sRand="";
???for (int i=0;i<5;i++){
???????g.setFont(getsFont(random));
???????g.setColor(new Color(random.nextInt(101),random.nextInt(111),random.nextInt(121)));
???????String rand=String.valueOf(getRandomString(random.nextInt(36)));
???????sRand+=rand;
???????g.translate(random.nextInt(3),random.nextInt(3));
???????g.drawString(rand,13*i,16);
???}
???session.removeAttribute("Rand");
???session.setAttribute("Rand",sRand);
???g.dispose();
???ImageIO.write(image, "JPEG", response.getOutputStream());
??}
??
??private String getRandomString(int num){
???String randstring = "0123456788ABCDEFGHIJKLMNOPQRSTUVWXYZ";
???return String.valueOf(randstring.charAt(num));
??}
?
?} (b、) RandomCodeCtrl.java是個servlet的java文件;內容如下: import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class RandomCodeCtrl extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest req,
??????HttpServletResponse resp) throws ServletException, IOException {
??resp.setContentType("image/jpeg");
??resp.setHeader("Pragma","No-cache");
??resp.setHeader("Cache-Control","no-cache");
??resp.setDateHeader("Expires", 0);
??RandomCode rc = new RandomCode();
??try{
???rc.getRandcode(req,resp);
??}catch(Exception e){
???System.err.println(e);
??}
?} public void doPost(HttpServletRequest request, HttpServletResponse response)
???throws ServletException, IOException {
??doGet(request, response);
?} } 2、前臺頁面調用;舉例: <img src="http://127.0.0.1:8080/RandomCodeCtrl" /> 3、驗證輸入信息和隨機生成的圖片顯示的內容相同: 在RandomCode.java代碼中隨機生成的圖片之前,就把隨機生成的圖片顯示的內容放在session中;所以只需判斷session中getAttribute("Rand")的值和用戶頁面輸入的驗證碼值相等即可。
轉載于:https://www.cnblogs.com/wvqusrtg/p/5105362.html
總結
以上是生活随笔為你收集整理的java实现随机验证码的图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript中Promises/
- 下一篇: python 解析 配置文件