java类代码
package com.iCloud.action;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Map;
import java.util.Random;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImageCodeAction extends BaseAction{
private InputStream is;
private Map<String, Object> session;
@Override
public String execute() {
try {
byte[] bs = generator();
is = new ByteArrayInputStream(bs);
} catch (Exception e) {
e.printStackTrace();
}
return super.execute();
}
private static int WIDTH = 200;
private static int HEIGHT = 80;
private static int NUM = 5;
private static char[] seq = { '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', '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9' };
/**
* 生成随机的验证码
*
* @return
* @throws Exception
*/
private byte[] generator() throws Exception {
Random r = new Random();
// 图片的内存映像
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
// 获得画笔对象
Graphics g = image.getGraphics();
g.setColor(randomColor(r));
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(new Color(0, 0, 0));
// 用于存储随机生成的验证码
StringBuffer number = new StringBuffer();
// 绘制验证码
for (int i = 0; i < NUM; i++) {
g.setColor(randomColor(r));
int h = (int) ((HEIGHT * 60 / 100) * r.nextDouble() + (HEIGHT * 30 / 100));
g.setFont(new Font(null, Font.BOLD | Font.ITALIC, h));
String ch = String.valueOf(seq[r.nextInt(seq.length)]);
number.append(ch);
g.drawString(ch, i * WIDTH / NUM * 90 / 100, h);
}
session.put("rand",number.toString()); //保存session
System.out.println("++++++++++++生成码:"+number.toString()); //打印值
// 绘制干扰线
for (int i = 0; i <= 12; i++) {
g.setColor(randomColor(r));
g.drawLine(r.nextInt(WIDTH), r.nextInt(HEIGHT), r.nextInt(WIDTH), r
.nextInt(HEIGHT));
}
// 压缩成jpeg格式
ByteArrayOutputStream os = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
// 把BufferedImage对象中的图像信息编码后
// 向创建该对象(encoder)时指定的输出流输出
encoder.encode(image);
return os.toByteArray();
}
private Color randomColor(Random r) {
return new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
}
public InputStream getIs() {
return is;
}
public void setIs(InputStream is) {
this.is = is;
}
public Map<String, Object> getSession() {
return session;
}
public void setSession(Map<String, Object> session) {
this.session = session;
}
}
struts配置------------------------------
<action name="codeaction" class="com.iCloud.action.ImageCodeAction">
<result name="success" type="stream">
<param name="inputname">is</param>
</result>
</action>
jsp页面-----------------------------------
<td class="input_checkcode">
<input class="input_3 required" id="codeName" name="codeName" onblur="checkwc9()" size="20" type="text">
<span ><img src="codeaction"
onclick="javascript:this.src='codeaction?id='+ Math.random();"
alt="看不清,换一个,请点我" id="imgId"></span>
<input id="hiddenCode" name="hiddenCode" type="text" value="<%=(String)session.getAttribute("rand")%>"></td>
</tr>
------------------------
我用SSH集成框架的。在js里验证 点击刷新验证码,取session值总是比显示的慢一步。
大神们都说用jquery同步验证局部刷新,可是没学过 在网上找了看了一天代码了,还是不懂jquery。有大神帮忙写下吗?谢谢。 或者发个简易比较全面带注释的jquery验证码案例好吗? 无限感激了. foryou2016@qq.com 。用尽最后27分了 - - 拜托了。在线等!!!