佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > ssh生成隨機(jī)數(shù)字驗(yàn)證碼操作步驟

ssh生成隨機(jī)數(shù)字驗(yàn)證碼操作步驟

熱門標(biāo)簽:Linux服務(wù)器 電子圍欄 團(tuán)購(gòu)網(wǎng)站 阿里云 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 銀行業(yè)務(wù) 服務(wù)器配置 Mysql連接數(shù)設(shè)置
1、login.jsp頁(yè)面程序
復(fù)制代碼 代碼如下:

script type="text/javascript">
function changeValidateCode(obj) {
//獲取當(dāng)前的時(shí)間作為參數(shù),無(wú)具體意義
var timenow = new Date().getTime();
//每次請(qǐng)求需要一個(gè)不同的參數(shù),否則可能會(huì)返回同樣的驗(yàn)證碼
//這和瀏覽器的緩存機(jī)制有關(guān)系,也可以把頁(yè)面設(shè)置為不緩存,這樣就不用這個(gè)參數(shù)了。
obj.src="rand.action?d="+timenow;
}
/script>

在表單中添加下面這句話:
復(fù)制代碼 代碼如下:

s:text name="random">/s:text>:s:textfield name="rand" size="5">/s:textfield>img src="rand.action" onclick="changeValidateCode(this)" title="點(diǎn)擊圖片刷新驗(yàn)證碼"/>

2、RandomNumUtil.java 生成驗(yàn)證碼的類文件
復(fù)制代碼 代碼如下:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
public class RandomNumUtil {
private ByteArrayInputStream image;//圖像
private String str;//驗(yàn)證碼
private RandomNumUtil(){
init();//初始化屬性
}
/*
* 取得RandomNumUtil實(shí)例
*/
public static RandomNumUtil Instance(){
return new RandomNumUtil();
}
/*
* 取得驗(yàn)證碼圖片
*/
public ByteArrayInputStream getImage(){
return this.image;
}
/*
* 取得圖片的驗(yàn)證碼
*/
public String getString(){
return this.str;
}
private void init() {
// 在內(nèi)存中創(chuàng)建圖象
int width=85, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 獲取圖形上下文
Graphics g = image.getGraphics();
// 生成隨機(jī)類
Random random = new Random();
// 設(shè)定背景色
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
// 設(shè)定字體
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
// 隨機(jī)產(chǎn)生155條干擾線,使圖象中的認(rèn)證碼不易被其它程序探測(cè)到
g.setColor(getRandColor(160,200));
for (int i=0;i155;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);
}
// 取隨機(jī)產(chǎn)生的認(rèn)證碼(6位數(shù)字)
String sRand="";
for (int i=0;i6;i++){
String rand=String.valueOf(random.nextInt(10));
sRand+=rand;
// 將認(rèn)證碼顯示到圖象中
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
// 調(diào)用函數(shù)出來(lái)的顏色相同,可能是因?yàn)榉N子太接近,所以只能直接生成
g.drawString(rand,13*i+6,16);
}
//賦值驗(yàn)證碼
this.str=sRand;
//圖象生效
g.dispose();
ByteArrayInputStream input=null;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try{
ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
ImageIO.write(image, "JPEG", imageOut);
imageOut.close();
input = new ByteArrayInputStream(output.toByteArray());
}catch(Exception e){
System.out.println("驗(yàn)證碼圖片產(chǎn)生出現(xiàn)錯(cuò)誤:"+e.toString());
}
this.image=input;/* 賦值圖像 */
}
/*
* 給定范圍獲得隨機(jī)顏色
*/
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);
}
}

3、RandomAction.java 生成驗(yàn)證碼的action程序
復(fù)制代碼 代碼如下:

import java.io.ByteArrayInputStream;
import com.mxl.util.RandomNumUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class RandomAction extends ActionSupport{
private ByteArrayInputStream inputStream;
public String execute() throws Exception{
RandomNumUtil rdnu=RandomNumUtil.Instance();
this.setInputStream(rdnu.getImage());//取得帶有隨機(jī)字符串的圖片
ActionContext.getContext().getSession().put("random", rdnu.getString());//取得隨機(jī)字符串放入HttpSession
return SUCCESS;
}
public void setInputStream(ByteArrayInputStream inputStream) {
this.inputStream = inputStream;
}
public ByteArrayInputStream getInputStream() {
return inputStream;
}
}

4、LoginAction.java 驗(yàn)證驗(yàn)證碼的action
復(fù)制代碼 代碼如下:

private String rand; //表單中的rand
public String getRand() {
return rand;
}
public void setRand(String rand) {
this.rand = rand;
}
//從session中取出RandomAction.java 中生成的驗(yàn)證碼random
String arandom=(String)(ActionContext.getContext().getSession().get("random"));
//下面就是將session中保存驗(yàn)證碼字符串與客戶輸入的驗(yàn)證碼字符串對(duì)比了
if(arandom.equals(this.getRand())) {
ActionContext.getContext().getSession().put("user", this.getUsername());
return SUCCESS;
}
else {
return ERROR;
}

5、配置struts.xml文件
復(fù)制代碼 代碼如下:

!-- Random驗(yàn)證碼 -->
action name="rand" class="com.mxl.rand.RandomAction">
result type="stream">
param name="contentType">image/jpeg/param>
param name="inputName">inputStream/param>
/result>
/action>

6、生成的驗(yàn)證碼圖片演示(實(shí)現(xiàn)的6位數(shù)字的驗(yàn)證碼)
說(shuō)明:
如果想修改驗(yàn)證碼生成的個(gè)數(shù),需要修改以下幾個(gè)地方:
第一點(diǎn):
int width=85, height=20; int width=85, height=20;
第二點(diǎn): for (int i=0;i6;i++) for (int i=0;i6;i++)
數(shù)字6,修改成你想生成的位數(shù)就可以了~
您可能感興趣的文章:
  • asp.net(C#) 生成隨機(jī)驗(yàn)證碼的代碼
  • php 生成隨機(jī)驗(yàn)證碼圖片代碼
  • Python 隨機(jī)生成中文驗(yàn)證碼的實(shí)例代碼
  • C# 生成驗(yàn)證碼取隨機(jī)數(shù)字加字母(改進(jìn)版)
  • Java 隨機(jī)生成驗(yàn)證碼(支持大小寫字母、數(shù)字、隨機(jī)字體)的實(shí)例
  • python生成隨機(jī)驗(yàn)證碼(中文驗(yàn)證碼)示例
  • Python隨機(jī)生成一個(gè)6位的驗(yàn)證碼代碼分享
  • Python生成隨機(jī)驗(yàn)證碼的兩種方法

標(biāo)簽:蚌埠 大理 衢州 江蘇 衡水 棗莊 廣元 萍鄉(xiāng)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ssh生成隨機(jī)數(shù)字驗(yàn)證碼操作步驟》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    清水河县| 南昌县| 南溪县| 新蔡县| 元朗区| 沿河| 武胜县| 彰武县| 内乡县| 象州县| 唐山市| 晋中市| 鄂尔多斯市| 平山县| 深泽县| 青浦区| 波密县| 方正县| 凌云县| 大关县| 康乐县| 澄城县| 文山县| 秦皇岛市| 苏州市| 灯塔市| 班玛县| 宜君县| 黑山县| 贵州省| 舞钢市| 汪清县| 富蕴县| 保亭| 客服| 北安市| 晴隆县| 河南省| 开封市| 龙游县| 句容市|