當前位置:名人名言大全網 - 端午節短信 - Java編程驗證碼發送到手機

Java編程驗證碼發送到手機

& lt% @ page content type = " image/JPEG " import = " Java . awt . *,java.awt.image.*,java.util.*,javax.imageio. * " % & gt

& lt%!

Color get randcolor (int fc,int BC){//獲取給定範圍內的隨機顏色。

Random Random = new Random();

if(fc & gt;255)fc = 255;

if(BC & gt;255)BC = 255;

int r = fc+random . nextint(BC-fc);

int g = fc+random . nextint(BC-fc);

int b = fc+random . nextint(BC-fc);

返回新顏色(r,g,b);

}

% & gt

& lt%

//設置頁面不被緩存

response.setHeader("Pragma ","無緩存");

response . set header(" Cache-Control "," no-Cache ");

response . setdate header(" Expires ",0);

//在內存中創建圖像

int width=60,height = 20

BufferedImage image = new BufferedImage(寬度,高度,BufferedImage。TYPE _ INT _ RGB);

//獲取圖形上下文

graphics g = image . get graphics();

//生成隨機類

Random Random = new Random();

//設置背景顏色

g.setColor(getRandColor(200,250));

g.fillRect(0,0,width,height);

//設置字體

g.setFont(新字體(“Times New Roman”,字體。平原,18));

//繪制邊框

//g . set Color(new Color());

//g.drawRect(0,0,width-1,height-1);

//155幹擾線隨機生成,使得圖像中的認證碼很難被其他程序檢測到。

g . set color(getRandColor(160,200));

for(int I = 0;我& lt155;i++)

{

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位)

//String rand = request . getparameter(" rand ");

//rand = rand.substring(0,rand.indexOf(" . "));

String sRand =

for(int I = 0;我& lt4;i++){

string rand = string . value of(random . nextint(10));

sRand+= rand;

//在圖像中顯示驗證碼。

g . set Color(new Color(20+random . nextint(110),20+random . nextint(110),20+random . nextint(110)));//調用函數顏色都壹樣,可能是因為種子太近,所以只能直接生成。

g .抽繩(蘭特,13*i+6,16);

}

//在會話中存儲身份驗證代碼。

session.setAttribute("ccode ",sRand);

//圖像生效

g . dispose();

//將圖像輸出到頁面

ImageIO.write(image," JPEG ",response . get output stream());

% & gt