Servlet color verification code

xiaoxiao2021-03-06  14

Servlet code, code content written in session Srand:

Package CMCC;

Import java.io. *;

Import java.awt.color;

Import java.awt.font;

Import java.awt.graphics;

Import java.awt.graphicsenvironment;

Import java.awt.image.bufferedImage;

Import java.io.ioException;

Import java.util.random;

Import javax.imageio.iioImage;

Import javax.imageio.imageio;

Import javax.imageio.Imagewriter;

Import javax.imageio.plugins.jpeg.jpegimagewriteparam;

Import javax.imageio.stream.imageoutputstream;

Import javax.servlet. *;

Import javax.servlet.http. *;

/ **

*

* @Author Vlua

* Todo

* 2004-12-22 16:02:52

* /

Public Class VerifyCodeServlet Extends httpservlet

{

PRIVATETIC FINAL INT Image_WIDTH = 48;

Private static final int image_height = 18;

Private static final int font_height = 16;

Private static final string svg_source1 = ""

"

Image_WIDTH

"/" height = / ""

Image_height

"/" xmlns = / "http://www.w3.org/2000/svg/"> "

"

Image_height

"/" font-family = / "arial /" font-size = / ""

Image_Height "/" Fill = / "Black /"> ";

Private static final string svg_source2 = " ";

/ *

* If Graphic Environment Not Avaliable (EG: Run Under Linux Console), USE

* SVG INSTEAD OF JPEG.

* /

Private boolean svgmode = false;

Private printwriter out = null;

/ *

* (non-javadoc)

*

* @see javax.servlet.http.httpservlet # service (javax.servlet.http.httpservletRequest, * javax.servlet.http.httpservletResponse)

* /

Protected void service (httpservletRequest arg0, httpservletresponse arg1)

Throws ServleTexception, IOException

{

IF (SN == Null)

Return;

String vcode = sn2vcode ();

Arg0.GetSession (). SetAttribute ("SRAND", Vcode); // Code content writes in session

IF (svgmode)

Outsvg (vcode, arg1);

Else

Outjpeg (Vcode, Arg1);

}

Private Void Outsvg (String Vcode, HTTPSERVLETRESPONSE RESP)

Throws oException

{

Resp.setContentType ("Image / SVG XML");

Resp.getOutputStream (). Print (svg_source1 vcode svg_source2);

}

Private void Outjpeg (String Vcode, HttpservletResponse Resp)

Throws oException

{

Resp.setContentType ("Image / JPEG");

BufferedImage Image = New BufferedImage (image_width, image_height,

BufferedImage.Type_INT_RGB);

Random Random = new random ();

Graphics g = image.getgraphics ();

G.SetColor (GetrandColor (200, 250));

G.FillRect (0, 0, image_width, image_height);

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

G.SetColor (GetrandColor (160, 200));

For (int i = 0; i <155; i )

{

INT x = random.nextint (image_width);

INT Y = random.nextint (image_height);

INT XL = random.nextint (12);

INT YL = random.nextint (12);

g.drawline (X, Y, X XL, Y YL);

}

For (INT i = 1; i <= 4; i )

{

String rand = vcode.substring (i - 1, i);

G.SetColor (New Color (20 Random.Nextint (110), 20 Random

.nextint (110), 20 random.nextint (110)));

g.drawstring (rand, 13 * (i - 1) 0, 16);

}

Imagewriter Writer = (ImageWriter) Imageio.GetImageWritersbyFormatName ("JPEG"). Next ();

JPEGIMAGEWRITEPARAM params = new JPEGIMAGEWRITEPARAM (NULL);

ImageOutputStream iOS = Imageio.createImageOutputStream (respon

.getOutputStream ());

Writer.setput (iOS);

Writer.write (NULL, New IIOIMAGE (Image, Null, Null), Params

Writer.dispose ();

iOS.Close ();

}

Static string Sn2vcode ()

{

String SRAND = "";

Random Random = new random ();

For (int i = 0; i <4; i )

{

String Rand = String.Valueof (random.nextint (10));

SRAND = Rand;

}

Return SRAND;

}

/ *

* (non-javadoc)

*

* @see javax.servlet.GenericServlet # init ()

* /

Public void init () THROWS servletexception

{

Try

{

Graphicsenvironment.getlocalgraphicsenvironment ();

}

Catch (throwable e)

{

// Run Under Linux Console???

svgmode = true;

}

}

/ **

* Get random colors

* @Param FC

* @Param BC

* @Return

* /

Public Color GetrandColor (int FC, INT BC)

{

Random Random = new random ();

IF (fc> 255)

Fc = 255;

IF (BC> 255)

BC = 255;

INT R = fc random.nextint (BC - FC);

INT g = fc random.nextint (BC - FC);

INT b = fc random.nextint (BC - FC);

Return New Color (R, G, B);

}

}

Add to Web.xml

VerifyCodeServlet cmcc.verifycodeServlet verifycodeservlet / verifycodeservlet / * Call this servlet with http: // hostname / verifycodeServlet / you can! Call

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

New Post(0)