& lt% @頁content type = " image/JPEG;charset = utf-8 " % & gt;
& lt%@page import="java.util.*,java.awt.*,java.awt.image.*,javax.imageio. * " % & gt
& lt% @ page import = " Java . io . output stream " % & gt;
& lthtml & gt
& ltbody & gt
& lt%!
Color getRandColor(int fc,int bc)
{
Random rd = new Random();
if(fc & gt;255)fc = 255;
if(BC & gt;255)BC = 255;
int red = fc+rd . nextint(BC-fc);
int green = fc+rd . nextint(BC-fc);
int blue = fc+rd . nextint(BC-fc);
返回新顏色(紅、綠、藍);
}
% & gt
& lt%
Random r = new Random();
response.addHeader("Pragma ","無緩存");
response . add header(" Cache-Control "," no-Cache ");
response . adddate header(" expires ",0);
int width = 90
int height = 23
BufferedImage pic = new buffered image(寬度,高度,buffered image。TYPE _ INT _ RGB);
graphics GC = pic . get graphics();
gc.setColor(getRandColor(200,250));
gc.fillRect(0,0,width,height);
String[] rNum ={"0 "," 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "," 9 "," a "," b "," c "," d "," e "," f ",
" g "," h "," I "," j "," k "," l "," m "," n "," o "," p "," q "," r "," s "," t "," u "," v "," w ",
“x”,“y”,“z”,“A”,“B”,“C”,“D”,“E”,“F”,“G”,“H”,“I”,“J”,“K”,“L”,“M”,“N”,
“O”、“P”、“Q”、“R”、“S”、“T”、“U”、“V”、“W”、“X”、“Y”、“Z”};
int[] style = {Font。普通字體。粗體,字體。斜體,字體。素色+字體。大膽,
字體。粗體+字體。斜體,字體。素色+字體。斜體,字體。素色+字體。粗體+字體。斜體};
gc.setColor(顏色。白色);
gc.drawLine(0,30,90,10);
GC . set color(getRandColor(160,200));
for(int I = 0;我& lt50;i++)
{
int x = r.nextInt(寬度);
int y = r . nextint(height);
int XL = r . nextint(10);
int yl = r . nextint(10);
gc.drawLine(x,y,x+xl,y+yl);
}
gc.setColor(getRandColor(60,150));
字符串rt =
for(int I = 0;我& lt4;i++){
string temp = rNum[r . nextint(62)];
rt = rt+temp;
GC . set Font(New Font(" Times New Roman ",style[r.nextInt(7)],15));
gc.drawString(temp,5+I * 15+r . nextint(10),10+r . nextint(10));
}
GC . dispose();
session.setAttribute("randNum ",rt);
output stream OS = response . get output stream();
ImageIO.write(pic,“JPEG”,OS);
System.out.println("當前驗證碼為:"+session . get attribute(" rand num "));
OS . flush();
OS . close();
os = null
response . flush buffer();
out . clear();
out = pagecontext . push body();
% & gt
& lt/body & gt;
& lt/html & gt;