-- 用于生成验证码
- public class RandomNumUtil {
-
- private ByteArrayInputStream p_w_picpath;
-
- private String str;
-
- public RandomNumUtil() {
- init();
- }
-
- private void init() {
-
- int width = 85, height = 20;
- BufferedImage p_w_picpath = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
-
-
- Graphics g = p_w_picpath.getGraphics();
-
-
- Random random = new Random();
-
-
- g.setColor(getRandColor(200, 250));
- g.fillRect(0, 0, width, height);
-
-
- g.setFont(new Font("Times New Roman", Font.PLAIN, 12));
-
-
- g.setColor(getRandColor(160, 200));
- for (int i = 0; i < 155; i++) {
- int x = random.nextInt(width);
- int y = random.nextInt(height);
- int xl = random.nextInt(12);
- int yl = random.nextInt(12);
- g.drawLine(x, y, x + xl, y + yl);
- }
-
- String sRand = "";
- for (int i = 0; i < 6; i++) {
- String rand = getVerify(random.nextInt(3));
- sRand += rand;
-
- g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
-
- g.drawString(rand, 13 * i + 6, random.nextInt(10) + 10);
- }
-
- this.str = sRand;
-
-
- g.dispose();
- ByteArrayInputStream input = null;
- ByteArrayOutputStream output = new ByteArrayOutputStream();
- try {
- ImageOutputStream p_w_picpathOut = ImageIO.createImageOutputStream(output);
- ImageIO.write(p_w_picpath, "JPEG", p_w_picpathOut);
- p_w_picpathOut.close();
- input = new ByteArrayInputStream(output.toByteArray());
- } catch (Exception e) {
- System.out.println("验证码图片产生出现错误:" + e.toString());
- }
-
- this.p_w_picpath = input;
- }
-
-
- private 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);
- }
-
-
-
-
-
-
-
-
- private String getVerify(int i){
- String str = "";
- if(i == 0){
- char c = 'a';
- c=(char)(c+(int)(Math.random()*26));
- str = c + "";
- }else if(i == 1){
- char c='A';
- c=(char)(c+(int)(Math.random()*26));
- str = c + "";
- }else if(i == 2){
- str = new Random().nextInt(10) + "";
- }
- return str;
- }
-
- public ByteArrayInputStream getImage() {
- return p_w_picpath;
- }
-
- public void setImage(ByteArrayInputStream p_w_picpath) {
- this.p_w_picpath = p_w_picpath;
- }
-
- public String getStr() {
- return str;
- }
-
- public void setStr(String str) {
- this.str = str;
- }
-- Struts2
- public class UtilAction extends ActionSupport {
-
- private ByteArrayInputStream inputStream;
-
- @Override
- public String execute() throws Exception {
-
- RandomNumUtil rdnu = new RandomNumUtil();
- this.setInputStream(rdnu.getImage());
- ActionContext.getContext().getSession().put("validateCode", rdnu.getStr());
- return SUCCESS;
- }
-
- public ByteArrayInputStream getInputStream() {
- return inputStream;
- }
-
- public void setInputStream(ByteArrayInputStream inputStream) {
- this.inputStream = inputStream;
- }
- }
-- JQuery
- $(function() {
- $("#randomCode").attr("src","verify");
- $("#refresh").click(
- function() {
- $("#randomCode").attr("src","verify");
- });
- })
-- Struts2 xml配置
- <action name="verify" class="verify.UtilAction">
- <result type="stream">
- <param name="contentType">p_w_picpath/jpeg</param>
- <param name="inputName">inputStream</param>
- </result>
- </action>
-- jsp页面
- <body>
- <img src="" width=150 height="50" alt="验证码图片" id="randomCode" />
- <span id="refresh" style="cursor: pointer;">换张图片</span>
- </body>
--效果如图
