Problems about random generation verification code

xiaoxiao2021-03-06  50

<% @ page contenttype = "image / jpeg" import = "java.awt. *, java.awt.image. *, java.util. *, javax.imageio. *"%>

<%

/ / Create image in memory

INT width = 60, height = 20;

BufferedImage Image = New BufferedImage (width, height, bufferedimage.type_int_rgb);

/ / Get the graphical context

Graphics g = image.getgraphics ();

// graphics2d graphics = image.creategraphics ();

// Set the background color

g.setcolor (color.white);

g.fillRect (0, 0, Width, Height);

// Sea Box

g.setcolor (color.ble);

g.drawRect (0, 0, Width-1, Height-1);

// Take the randomly generated authentication code (4 digits)

String Rand = Request.getParameter ("Rand");

Rand = rand.substring (0, rand.indexof ("."));

Switch (rand.Length ()) {

Case 1:

Rand = "000" rand;

Break;

Case 2:

RAND = "00" rand;

Break;

Case 3:

RAND = "0" rand;

Break;

DEFAULT:

Rand = rand.substring (0, 4);

Break;

}

// Deposit the authentication code into the session

Session.SetaTRibute ("rand", rand);

// Display the authentication code to the image

g.setcolor (color.ble);

G.SetFont (New Font ("Times New Roman", Font.Plain, 18));

g.drawstring (rand, 10, 15);

// randomly generate 88 interference points, so that the authentication code in the image is not easy to be probed by other programs

Random Random = new random ();

For (int i = 0; i <88; i ) {

INT x = random.nextint (width);

INT Y = random.nextint (height);

g.drawline (x, y, x, y);

}

// Image take effect

g.dispose ();

// Output image to page

Imageio.write (Image, "JPEG", response.getOutputStream ());

%>

转载请注明原文地址:https://www.9cbs.com/read-73451.html

New Post(0)