struts.xml配置
<package name="example" namespace="/" extends="struts-default">
<action name="validatecode" class="ValidateCodeAction">
<result type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">inputStream</param>
</result>
</action>
</package>
Java代码
public class ValidateCodeAction {
private ByteArrayInputStream inputStream;
public ByteArrayInputStream getInputStream() {
return inputStream;
}
public void setInputStream(ByteArrayInputStream inputStream) {
this.inputStream = inputStream;
}
public String execute() {
try {
this.setInputStream(generateImage());
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
/*
* 画验证码
*/
private ByteArrayInputStream generateImage() throws IOException {
BufferedImage image = new BufferedImage(100, 20,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, 100, 20);
drawbg(g);
drawValidateCode(g);
ByteArrayInputStream input = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(image, "JPEG", bos);
byte[] buf = bos.toByteArray();
input = new ByteArrayInputStream(buf);
return input;
}
private void drawbg(Graphics g) {
Random rand = new Random();
int randx;
int randy;
for (int i = 0; i < rand.nextInt(100) + 500; i++) {
g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand
.nextInt(255)));
randx = rand.nextInt(100);
randy = rand.nextInt(20);
g.drawLine(randx, randy, randx, randy);
}
}
private void drawValidateCode(Graphics g) {
String code = generateCode();
Random rand = new Random();
int x = 0;
Font font = new Font("Times New Roman", Font.PLAIN, 18);
g.setFont(font);
for (int i = 0; i < code.length(); i++) {
int y = 20 - rand.nextInt(4);
g.setColor(new Color(rand.nextInt(150), rand.nextInt(150), rand
.nextInt(150)));
g.drawString(code.substring(i, i + 1), x, y);
x += 20;
}
}
/*
* 生成验证码
*/
private String generateCode() {
Random rand = new Random();
StringBuffer sbr = new StringBuffer(
"ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789");
int codeLen = 5;
StringBuffer codeSbr = new StringBuffer();
for (int i = 0; i < codeLen; i++) {
int select = rand.nextInt(sbr.length());
codeSbr.append(sbr.charAt(select));
sbr.deleteCharAt(select);
}
ServletActionContext.getRequest().getSession().setAttribute(
"ValidateCode", codeSbr.toString());
System.out.println(codeSbr.toString());
return codeSbr.toString();
}
}
分享到:
相关推荐
java生成验证码图片,资源包括自定义图片尺寸和验证码长度,每个字符随机偏移角度,字符平滑边缘,含干扰线、噪点和背景扭曲。
java生成验证码源码包括彩色验证码 包括jsp生成验证码和彩色验证码
java生成验证码的工具类,这个现在有依赖可以直接引入调用,但这个工具类没有引入依赖,大家也容易看懂代码,理解其含义。也可直接去工具类中调整生成验证码的宽度、高度、颜色、背景颜色、生成的随机数数量、干扰线...
是一个完整的例子!生成的验证码有中文的,英文的,数字的
java生成验证码的参考资料(中英文验证码),这是网络资料的收集,大家可以试一下,效果应该不错,感谢网友的提供。
java生成验证码代码 主要:一个生成验证码的servlet,一个jsp页面,另外还需要配置下web.xml即可
java生成验证码图片的文件已完成测试
使用Java生成验证码的工具类,使用说明:http://blog.csdn.net/wqh8522/article/details/72791218
java生成验证码
Java生成验证码(包含gif动画验证码)
用JAVA生成验证码的操作,与大家分享下
自己写的验证码,不仅能够实现加干扰条,还能实现字符倾斜,是数字和字母的混合
验证码是每个网站必不可少的要素...此代码就是用servlet生成随机生成验证码
这是个Java Web验证码示例,用于生成验证码(位数可以自己通过验证码类修改),并进行jsp页面展示和验证,可以直接附加到myeclipse下运行,上传上来与Java技术开发朋友分享。
java 生成图片验证码 带干扰线等