导航:首页 > 编程语言 > java随机4位字符验证码

java随机4位字符验证码

发布时间:2025-09-17 15:33:27

java怎么生成验证码图片

以下仅供参考:

这是jsp页面,起个名字例如叫image.jsp:

<%@ page contentType="image/JPEG"
import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"
pageEncoding="GBK"%><%!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);
}%><%

response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);

int width = 60, height = 20;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);

Graphics g = image.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, 18));

g.setColor(getRandColor(160, 200));
for (int i = 0; i < 100; 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 < 4; i++) {
String rand = String.valueOf(random.nextInt(10));
sRand += rand;

g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
g.drawString(rand, 13 * i + 6, 16);
}

session.setAttribute("code", sRand);

g.dispose();

ImageIO.write(image, "JPEG", response.getOutputStream());
%>

在你需要引入的地方:
<img border=0 src="image.jsp">

就可以了。。。

⑵ 用java随机生成四位验证码,只求编程代码

不知道你问的是不是生成这种图片验证码?
如果只要一个随机四位数 那这行代码就够了(new Random().nextInt(9000) + 1000;),
如果是生成页面图片验证码就是下面的了:
//设定 响应模式
resp.setContentType("image/jpeg");

// 生成令牌环数据;
Integer token = new Random().nextInt(9000) + 1000;

// 保存令牌环数据到session中
req.getSession().setAttribute(IMAGE_TOKEN_NAME, token);

// 生成令牌环图片
ServletOutputStream out = resp.getOutputStream();
BufferedImage img = new BufferedImage(60, 20,
BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setColor(Color.YELLOW);
g.fillRect(0, 0, img.getWidth(), img.getHeight());
g.setColor(Color.BLUE);
g.setFont(new Font("", Font.BOLD, 18));
g.drawString(String.valueOf(token), 10, 16);
ImageIO.write(img, "jpg", out);
out.close();

⑶ java怎么实现随机4个带有数字和字母的验证码

importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
importjava.util.Random;

importjavax.imageio.ImageIO;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;

publicclassRandomValidateCode{

="RANDOMVALIDATECODEKEY";//放到session中的key
privateRandomrandom=newRandom();
privateStringrandString="";//随机产生的字符串

privateintwidth=80;//图片宽
privateintheight=26;//图片高
privateintlineSize=40;//干扰线数量
privateintstringNum=4;//随机产生字符数量
/*
*获得字体
*/
privateFontgetFont(){
returnnewFont("Fixedsys",Font.CENTER_BASELINE,18);
}
/*
*获得颜色
*/
privateColorgetRandColor(intfc,intbc){
if(fc>255)
fc=255;
if(bc>255)
bc=255;
intr=fc+random.nextInt(bc-fc-16);
intg=fc+random.nextInt(bc-fc-14);
intb=fc+random.nextInt(bc-fc-18);
returnnewColor(r,g,b);
}
/**
*生成随机图片
*/
publicvoidgetRandcode(HttpServletRequestrequest,
HttpServletResponseresponse){
HttpSessionsession=request.getSession();
//BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类
BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
Graphicsg=image.getGraphics();//产生Image对象的Graphics对象,改对象可以在图像上进行各种绘制操作
g.fillRect(0,0,width,height);
g.setFont(newFont("TimesNewRoman",Font.ROMAN_BASELINE,18));
g.setColor(getRandColor(110,133));
//绘制干扰线
for(inti=0;i<=lineSize;i++){
drowLine(g);
}
//绘制随机字符
StringrandomString="";
for(inti=1;i<=stringNum;i++){
randomString=drowString(g,randomString,i);
}
session.removeAttribute(RANDOMCODEKEY);
session.setAttribute(RANDOMCODEKEY,randomString);
System.out.println(randomString);
g.dispose();
try{
ImageIO.write(image,"JPEG",response.getOutputStream());//将内存中的图片通过流动形式输出到客户端
}catch(Exceptione){
e.printStackTrace();
}
}
/*
*绘制字符串
*/
privateStringdrowString(Graphicsg,StringrandomString,inti){
g.setFont(getFont());
g.setColor(newColor(random.nextInt(101),random.nextInt(111),random.nextInt(121)));
Stringrand=String.valueOf(getRandomString(random.nextInt(randString.length())));
randomString+=rand;
g.translate(random.nextInt(3),random.nextInt(3));
g.drawString(rand,13*i,16);
returnrandomString;
}
/*
*绘制干扰线
*/
privatevoiddrowLine(Graphicsg){
intx=random.nextInt(width);
inty=random.nextInt(height);
intxl=random.nextInt(13);
intyl=random.nextInt(15);
g.drawLine(x,y,x+xl,y+yl);
}
/*
*获取随机的字符
*/
publicStringgetRandomString(intnum){
returnString.valueOf(randString.charAt(num));
}
}

⑷ 用java怎么制作验证码

验证方法很多
蠢一点的后台写代码,或者前台页面加js
当然你用框架自带的也行,例如struts的

阅读全文

与java随机4位字符验证码相关的资料

热点内容
网络和编程哪个更吃香 浏览:583
神武用过的账号都在哪个文件夹里 浏览:70
对于初学者用什么编程语言比较好 浏览:470
吉林环保文件柜值多少钱 浏览:111
1607版本win10 浏览:968
如何使文件不能复制 浏览:96
matlabr2017a安装教程 浏览:287
access数据库查询生成表 浏览:661
nodejsforever配置 浏览:181
萝卜网络什么意思 浏览:925
steam地平线4存档在哪个文件夹 浏览:870
JAVA下载压缩文件包括文件夹 浏览:277
上古5法术代码 浏览:235
将文件打印成pdf格式 浏览:540
ug导出cad为什么没有文件 浏览:469
怪物猎人3g弓升级 浏览:373
java随机4位字符验证码 浏览:589
前端postjson数据 浏览:462
plc初次编程如何清理原有程序 浏览:204
苹果6plus手机美版序列号 浏览:6

友情链接