分享
 
 
 

servlet彩色验证码

王朝java/jsp·作者佚名  2006-01-31
窄屏简体版  字體: |||超大  

servlet代码,代码内容写入session sRand中:

servlet代码,代码内容写入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

{

private static 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 = "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">"

+ "<svg width=\""

+ IMAGE_WIDTH

+ "\" height=\""

+ IMAGE_HEIGHT

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

+ "<text x=\"0\" y=\""

+ IMAGE_HEIGHT

+ "\" font-family=\"Arial\" font-size=\""

+ IMAGE_HEIGHT + "\" fill=\"black\">";

private static final String SVG_SOURCE2 = "</text></svg>";

/*

* 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) ;//代码内容写入session中

if (svgMode)

outSVG(vcode, arg1);

else

outJPEG(vcode, arg1);

}

private void outSVG(String vcode, HttpServletResponse resp)

throws IOException

{

resp.setContentType("image/svg+xml");

resp.getOutputStream().print(SVG_SOURCE1 + vcode + SVG_SOURCE2);

}

private void outJPEG(String vcode, HttpServletResponse resp)

throws IOException

{

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(resp

.getOutputStream());

writer.setOutput(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;

}

}

/**

* 获得随机颜色

* @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);

}

}

在web.xml 中加入

<servlet>

<servlet-name>VerifyCodeServlet</servlet-name>

<servlet-class>CMCC.VerifyCodeServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>VerifyCodeServlet</servlet-name>

<url-pattern>/VerifyCodeServlet/*</url-pattern>

</servlet-mapping>

调用该servlet用http://hostname/VerifyCodeServlet/即可!

调用<img src=http://hostname/VerifyCodeServlet>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有