内容导航:可以使用Random类来随机生成字符串,验证码是在用户登录页面上加入一张随机生成的图片,int g = fc + (bc - fc),static color getrandcolor(int fc
{image}
一 、java获取网页验证码
验证码获取JAVA采用http链接请求就可以 ,陪答不过验证码无法识别,多数验证码都是图片,又有宽配很多干扰线 ,目前没有高效识别方法。芦巧慧用个IE抓包工具 ,看每次提交都有什么数据 。
二、用java实现:随机获取4位的验证码
验证码是指网页的验证码还是手机的验证码
下面是随机生成四位数的相关代码
import ;
public class RandomTest {
public static void main(String[] args) {
("得到小数");
((() * ));
("Random");
(new Random().nextInt(9999));
("字符串前面补0的话就这样");
(("%04d",new Random().nextInt(9999)));
}
}public static void main(String[] args){
Random d = new Random();
String str = "";
for(int i=0;i<4;i++){
int num = (10);
str += num+"";
}
(str);
}
希望能帮到你for (int i = 0; i < 4; i++) {
((int) (() * 10));
}public static void processrequest(httpservletrequest request,
httpservletresponse response) throws servletexception, ioexception {
ttype("image/jpeg");
("pragma", "no-cache");
("cache-control", "no-cache");
ader("expires", 0);
httpsession session = n();
int width = 60, height = 20;
bufferedimage image = new bufferedimage(width, height,
_int_rgb);
// 获取图形上下文
graphics g = cs();
// 生成随机类
random random = new random();
// 设定背景色
(getrandcolor(200, 250));
(0, 0, width, height);
// 设定字体
(new font("times new roman", , 18));
// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
(getrandcolor(160, 200));
for (int i = 0; i < 155; i++) {
int x = (width);
int y = (height);
int xl = (12);
int yl = (12);
(x, y, x + xl, y + yl);
}
// 取随机产生的认证码(4位数字)
string srand = "";
for (int i = 0; i < 4; i++) {
string rand = ((10));
srand += rand;
// 将认证码显示到图象中
(new color(20 + (110), 20 + random
.nextint(110), 20 + (110)));// 调用函数出来的颜色相同,可能是因为种子太接近 ,所以只能直接生成
g(rand, 13 * i + 6, 16);
}
// 将认证码存入session
ute("rand", srand);
// 图象生效
();
servletoutputstream responseoutputstream = stream();
// 输出图象到页面
(image, "jpeg", responseoutputstream);
// 以下关闭输入流!
();
();
}
static color getrandcolor(int fc, int bc) {
// 给定范围获得随机颜色
random random = new random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + (bc - fc);
int g = fc + (bc - fc);
int b = fc + (bc - fc);
return new color(r, g, b);
}
/
handles the http get method.
@param request
servlet request
@param response
servlet response
*/
protected void doget(httpservletrequest request,
httpservletresponse response) throws servletexception, ioexception {
processrequest(request, response);
}这个是我web里用的验证码,你可以改改
三、java验证码识别
是你自己要识别别人的,还是自己识别自己的啊?自己识别自己的 ,很简单的,每一张图片,都有一个对应的 另考占需另程厚 字符串数组保存在 来自 本地 ,例如Picture pic[N};String strs[N];系统随机是用地i张 己经压资决论 图片的时候,字符串就是strs[i]。不需要图片处理的!
四 、java 登陆时的验证码怎么做
验证码是在用户登录页面上加入一张随机生成的图片,让用户输入图片中所显示的字符或数字 ,以此来验证用户是否为人类行为,有效防止机器人或者恶意软件攻击。Java实饥芹现验证码的具体步骤如下:1. 生成随机字符串:可以使用Random类来随机生成字符串,也可以使用UUID等工具 。2. 把生成渗正的随机字符串放在图片上:可以使用Java的Graphics2D来绘制文本 ,并设置字体 、颜色等属性 。3. 把生成的图片响应给客户端:可以使用ImageIO类来生成图片文件。4. 检验用户输入的验证码是否和图片中的相同:可以使用Session来存储验证码 ,提交表单烂喊毕时再验证,或者在Ajax请求时把验证码验证结果返回给客户端。 完成以上四个步骤就能够完成Java登录页面的验证码功能 。