struts下的生成验证图片

本文介绍了一个用于生成包含字母和数字的验证码图片流的Java类。该类可以自定义验证码图片的尺寸、字体样式等属性,并在图片上绘制随机线条及指定长度的验证码字符。文章还展示了如何在Struts框架中使用此类生成验证码图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 生成图片流
package  com.bo;

import  java.awt.Color;
import  java.awt.Font;
import  java.awt.Graphics;
import  java.awt.image.BufferedImage;
import  java.io.IOException;
import  java.io.OutputStream;
import  java.util.Random;

import  javax.imageio.ImageIO;

public   class  ImageOP  {
    
    
private Random random;
    
private BufferedImage image;
    
private Font font;
    
private int distance;
    
    
public ImageOP(int width,int heigh,String fontStr,int sytle,int size,int distance){
        random 
= new Random();
        image 
= new BufferedImage(width,heigh,BufferedImage.TYPE_3BYTE_BGR);
        font 
= new Font(fontStr,sytle,size);
        
this.distance = distance;
    }

    //默认格式
    
public ImageOP(){
        
this(75,25,"宋体",Font.BOLD,25,20);
    }

    
    
public String getCheckCodeImage(String str,int show,OutputStream output) throws IOException{
        Graphics d 
= image.getGraphics();
        d.setColor(Color.WHITE);
        d.fillRect(
0,0,image.getWidth(),image.getHeight());
        d.setColor(
new Color(random.nextInt(100)+100,random.nextInt(100)+100,random.nextInt(100)+100));
        
for (int i = 0; i < 10; i++{
            d.drawLine(random.nextInt(image.getWidth()),random.nextInt(image.getHeight()),random.nextInt(image.getWidth()),random.nextInt(image.getHeight()));
        }

        d.setColor(Color.BLACK);
        d.setFont(font);
        String checkCode 
= "";
        
char tmp = 0;
        
int x = -distance;
        
for (int i = 0; i < show; i++{
            tmp 
= str.charAt(random.nextInt(str.length()-1));
            checkCode 
= checkCode + tmp;
            x 
= x + distance;
            d.setColor(
new Color(random.nextInt(100)+50,random.nextInt(100)+50,random.nextInt(100)+50));
            d.drawString(tmp
+"",x,random.nextInt(image.getHeight()-(font.getSize()/2))+(font.getSize()/2));
        }

        d.dispose();
        ImageIO.write(image,
"jpeg",output);
        
return checkCode;
    }

    
}

action调用的方法

 

public  ActionForward getCheckCode(ActionMapping mapping,
              ActionForm form,
              HttpServletRequest request,
              HttpServletResponse response)
{
        
try {
            ImageOP op 
= new ImageOP();
            HttpSession session 
= request.getSession();
            session.setAttribute(
"checkCode",op.getCheckCodeImage("abcdefghigklmnopqrst0123456789",4,response.getOutputStream()));
        }
 catch (IOException e) {
            e.printStackTrace();
        }

        
return null;
    }

 

struts-config.xml配置

 

   < action  path ="/checkImage"
          type
="com.action.LogonAction"<!--  action调用上面的方法 -->
          name
=""
          scope
="request"
          input
=""
          parameter
="opName"
          validate
="false" >
  
</ action >

 

页面加载

 

< html >
< script  language ="JavaScript" >
    
function open_window() {
    window.location.reload();
    }

</ script >
  
< head >
    
< base  href ="<%=basePath%>" >
    
    
< title > My JSP 'son.jsp' starting page </ title >
    
    
< meta  http-equiv ="pragma"  content ="no-cache" >
    
< meta  http-equiv ="cache-control"  content ="no-cache" >
    
< meta  http-equiv ="expires"  content ="0" >
    
< meta  http-equiv ="keywords"  content ="keyword1,keyword2,keyword3" >
    
< meta  http-equiv ="description"  content ="This is my page" >
    
    
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    
-->
  
</ head >
  
  
< body >
            
< href ="#"  onclick ="open_window();"  class ="global" >< img  src ="checkImage.do?opName=getCheckCode"  border ="0" ></ a ><!-- 点击图片能够刷新的图片 -->
  
</ body >
</ html >
欢迎交流
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值