国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

JSP任意長度驗證碼

2024-09-05 00:19:18
字體:
來源:轉載
供稿:網友
中國最大的web開發(fā)資源網站及技術社區(qū),

<%@page import="java.io.*,java.util.*,java.awt.*,java.awt.image.*,javax.imageio.*"%>
<%
/*
使用方法:在需要顯示驗證碼的html代碼中使用<img p">
  在需判斷session的時候判斷session.getattribute("rand")
*/
  int codelength=55;//驗證碼長度
  int mixtimes=0;//模糊程度參數(shù)
  color bgcolor=getrandcolor(200, 250);//背景顏色
  color bfcolor=new color(0,0,0);//字體顏色
  boolean ifrandomcolor=true;//單個字符是否顏色隨機
  boolean ifmixcolor=false;//模糊線是否顏色隨機


  //設置頁面不緩存
  response.setheader("pragma", "no-cache");
  response.setheader("cache-control", "no-cache");
  response.setdateheader("expires", 0);
  // 在內存中創(chuàng)建圖象
  int width = 13*codelength+6, height = 20;
  bufferedimage image = new bufferedimage(width, height,bufferedimage.type_int_rgb);
  // 獲取圖形上下文
  graphics g = image.getgraphics();
  // 設定背景色
  g.setcolor(bgcolor);
  g.fillrect(0, 0, width, height);
  //設定字體
  g.setfont(new font("times new roman", font.plain, 18));
  //畫邊框
  g.setcolor(new color(33,66,99));
  g.drawrect(0,0,width-1,height-1);
  // 隨機產生干擾線,使圖象中的認證碼不易被其它程序探測到
  g.setcolor(getrandcolor(160, 200));
  for (int i = 0; i < mixtimes*codelength/10; i++) {
 if(ifmixcolor)
 {
  g.setcolor(getrandcolor(160, 200));
 }
 int x = random.nextint(width);
    int y = random.nextint(height);
    int xl = random.nextint(12);
    int yl = random.nextint(12);
    g.drawline(x, y, x + xl, y + yl);
  }
  // 取隨機產生的認證碼(4位數(shù)字)
  string srand = "";
  for (int i = 0; i < codelength; i++) {
    string rand = string.valueof(random.nextint(10));
    srand += rand;
    // 將認證碼顯示到圖象中
 if(ifrandomcolor)
  g.setcolor(getrandcolor(20,110,0));
 else
  g.setcolor(bfcolor);
 //調用函數(shù)出來的顏色相同,可能是因為種子太接近,所以只能直接生成
    g.drawstring(rand, 13 * i + 6, 16);
  }
  // 將認證碼存入session
  session.setattribute("rand", srand);
  // 圖象生效
  g.dispose();
  // 輸出圖象到頁面
  imageio.write(image, "png", response.getoutputstream());
%>
<%!
//給定范圍獲得隨機顏色
private static random random=new random();
private color getrandcolor(int fc, int bc) {
  return getrandcolor(fc, bc, fc);
}
private color getrandcolor(int fc, int bc,int interval) {
  if (fc > 255) {
    fc = 255;
  }
  if (bc > 255) {
    bc = 255;
  }
  int r = fc + random.nextint(bc - interval);
  int g = fc + random.nextint(bc - interval);
  int b = fc + random.nextint(bc - interval);
  return new color(r, g, b);
}
%>

 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 秦安县| 克拉玛依市| 怀柔区| 福州市| 南澳县| 徐汇区| 武汉市| 隆化县| 吐鲁番市| 离岛区| 惠水县| 吴桥县| 嘉祥县| 新和县| 绥棱县| 静安区| 潼关县| 白玉县| 广东省| 乾安县| 鹿泉市| 自贡市| 广东省| 迭部县| 桓台县| 聂荣县| 葵青区| 靖远县| 二连浩特市| 沙雅县| 拉萨市| 台北县| 望都县| 卢湾区| 永年县| 贵德县| 义乌市| 富民县| 巴林右旗| 泸溪县| 皋兰县|