java獲取驗證碼源代碼 java驗證碼

java模擬登陸js動態(tài)生成的驗證碼怎么獲取

登錄頁面login.jsp示例代碼:

成都創(chuàng)新互聯(lián)公司專注于洱源網(wǎng)站建設服務及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供洱源營銷型網(wǎng)站建設,洱源網(wǎng)站制作、洱源網(wǎng)頁設計、洱源網(wǎng)站官網(wǎng)定制、微信小程序服務,打造洱源網(wǎng)絡公司原創(chuàng)品牌,更為您提供洱源網(wǎng)站排名全網(wǎng)營銷落地服務。

%@ page language="java" import="java.util.*" pageEncoding="utf-8"%

%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%

!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

html

head

base href="%=basePath%"

titleMy JSP 'login.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

%

String incode = (String)request.getParameter("code");

String rightcode = (String)session.getAttribute("rCode");

if(incode != null rightcode != null){

if(incode.equals(rightcode)){

out.println("驗證碼輸入正確!");

}else{

out.println("驗證碼輸入不正確,請重新輸入!");

}

}

%

body

form action="login.jsp" method="post"

用戶名:

input type="text" name="username"/br/

密碼:

input type="password" name="password"/br/

驗證碼:

img src="number.jsp"/

input type="text" name="code"/

input type="submit" value="登錄"/

/form

/body

/html

驗證碼頁面number.jsp代碼:

%@ page contentType="image/jpeg" language="java" import="java.util.*,java.awt.*,java.awt.image.*,javax.imageio.*" pageEncoding="utf-8"%

%!

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-catch");

response.setDateHeader("Expires",0);

//在內(nèi)存中創(chuàng)建圖象

int width = 60;

int height = 20;

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

//創(chuàng)建圖象

Graphics g = image.getGraphics();

//生成隨機對象

Random random = new Random();

//設置背景色

g.setColor(getRandColor(200,250));

g.fillRect(0,0,width,height);

//設置字體

g.setFont(new Font("Tines Nev Roman",Font.PLAIN,18));

//隨機產(chǎn)生干擾線

g.setColor(getRandColor(160,200));

for(int i = 0; i 255; i++){

int x = random.nextInt(width);

int y = random.nextInt(height);

int xl = random.nextInt(12);

int yl = random.nextInt(12);

}

//隨機產(chǎn)生認證碼,4位數(shù)字

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("rCode",sRand);

//圖像生效

g.dispose();

//輸出圖像到頁面

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

out.clear();

out = pageContext.pushBody();

%

怎么用Java代碼實現(xiàn)一個驗證碼,求具體實現(xiàn)方法

package?util;

import?java.awt.Color;

import?java.awt.Font;

import?java.awt.Graphics;

import?java.awt.image.BufferedImage;

import?java.io.FileOutputStream;

import?java.io.IOException;

import?java.io.OutputStream;

import?java.util.Random;

import?javax.imageio.ImageIO;

public?final?class?ImageUtil?{

//?驗證碼字符集

private?static?final?char[]?chars?=?{?

'0',?'1',?'2',?'3',?'4',?'5',?'6',?'7',?'8',?'9',?

'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',?

'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'};

//?字符數(shù)量

private?static?final?int?SIZE?=?4;

//?干擾線數(shù)量

private?static?final?int?LINES?=?5;

//?寬度

private?static?final?int?WIDTH?=?80;

//?高度

private?static?final?int?HEIGHT?=?40;

//?字體大小

private?static?final?int?FONT_SIZE?=?30;

/**

*?生成隨機驗證碼及圖片

*?返回的數(shù)組中,第1個值是驗證碼,第2個值是圖片

*/

public?static?Object[]?createImage()?{

StringBuffer?sb?=?new?StringBuffer();

//?1.創(chuàng)建空白圖片

BufferedImage?image?=?new?BufferedImage(

WIDTH,?HEIGHT,?BufferedImage.TYPE_INT_RGB);

//?2.獲取圖片畫筆

Graphics?graphic?=?image.getGraphics();

//?3.設置畫筆顏色

graphic.setColor(Color.LIGHT_GRAY);

//?4.繪制矩形背景

graphic.fillRect(0,?0,?WIDTH,?HEIGHT);

//?5.畫隨機字符

Random?ran?=?new?Random();

for?(int?i?=?0;?i?SIZE;?i++)?{

//?取隨機字符索引

int?n?=?ran.nextInt(chars.length);

//?設置隨機顏色

graphic.setColor(getRandomColor());

//?設置字體大小

graphic.setFont(new?Font(

null,?Font.BOLD?+?Font.ITALIC,?FONT_SIZE));

//?畫字符

graphic.drawString(

chars[n]?+?"",?i?*?WIDTH?/?SIZE,?HEIGHT?/?2);

//?記錄字符

sb.append(chars[n]);

}

//?6.畫干擾線

for?(int?i?=?0;?i??LINES;?i++)?{

//?設置隨機顏色

graphic.setColor(getRandomColor());

//?隨機畫線

graphic.drawLine(ran.nextInt(WIDTH),?ran.nextInt(HEIGHT),

ran.nextInt(WIDTH),?ran.nextInt(HEIGHT));

}

//?7.返回驗證碼和圖片

return?new?Object[]{sb.toString(),?image};

}

/**

*?隨機取色

*/

public?static?Color?getRandomColor()?{

Random?ran?=?new?Random();

Color?color?=?new?Color(ran.nextInt(256),?

ran.nextInt(256),?ran.nextInt(256));

return?color;

}

public?static?void?main(String[]?args)?throws?IOException?{

Object[]?objs?=?createImage();

BufferedImage?image?=?(BufferedImage)?objs[1];

OutputStream?os?=?new?FileOutputStream("d:/1.png");

ImageIO.write(image,?"jpeg",?os);

os.close();

}

}

怎么樣通過java代碼得到頁面上的驗證碼

具體的倒沒做過,不過原理應該差不多,不過不會簡單,一句兩句是將不清楚的,呵呵

基本原理是這樣的,這個圖片在IE的緩存文件夾Local Settings\Temporary Internet Files中一定會有一個對應的固定名稱的圖片,每次這個圖片文件名稱是一樣的,只是里面的內(nèi)容不一樣,你可以找一下看看,呵呵,下面就簡單了吧,不過,前提是這個頁面你要在瀏覽器加載過,這樣才能形成緩存文件。

你點這些分,我就說這么多了~

===================================================

呵呵,你這個不好解決,是不是想搞自動注冊呢?

人家驗證碼明顯是不會在客戶端產(chǎn)生的,這個是保存在服務端的,那你如何得到呢?客戶端得到的只是一個圖片而已,方法也有,就是你把這個圖片得到,動態(tài)解析,從這個圖片的解析中獲取它所表示的內(nèi)容,這個難度有點大的,圖片解析難度比較高的,如果再加一些干擾,呵呵,基本能解出來的不是高手也差不多了。

所以,你的這個問題本身比較難實現(xiàn),驗證碼使用的目的就是為了防止自動注冊,而且這個注冊碼本身是不會傳遞到客戶端的,所以,你要獲取的可能性很小。

用java實現(xiàn):隨機獲取4位的驗證碼

驗證碼是指網(wǎng)頁的驗證碼還是手機的驗證碼

下面是隨機生成四位數(shù)的相關代碼

import?java.util.Random;

public?class?RandomTest?{

public?static?void?main(String[]?args)?{

System.out.println("Math.random得到小數(shù)");

System.out.println(Math.round(Math.random()?*?10000));

System.out.println("Random");

System.out.println(new?Random().nextInt(9999));

System.out.println("字符串前面補0的話就這樣String.format");

System.out.println(String.format("%04d",new?Random().nextInt(9999)));

}

}

網(wǎng)頁名稱:java獲取驗證碼源代碼 java驗證碼
URL標題:http://muchs.cn/article10/docoego.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設、品牌網(wǎng)站制作、ChatGPT、網(wǎng)站設計公司、搜索引擎優(yōu)化、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設