導航:首頁 > 編程語言 > java6位驗證碼

java6位驗證碼

發布時間:2025-07-16 10:55:30

Ⅰ 用java怎麼製作驗證碼

原理:

1.隨機生成4個數字 用到了Random類
2.對這4個數字設置字體格式 用 setFont方法
3.改變字體顏色用setColor 然後隨機生成顏色

代碼如下
package s1;

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

import javax.imageio.ImageIO;
import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class GetImage extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

this.doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 發送圖片不能夠添加這2行代碼
// response.setContentType("text/html;charset=UTF-8");
// request.setCharacterEncoding("UTF-8");

int width=100;
int height=50;
//獲得一張圖片
BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

Graphics g=image.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(1, 1, width-2, height-2);
g.setFont(new Font("宋體",Font.BOLD,30));
Random random=new Random();

/虛枯兄/ 填充的字元串
String str="";

//緩存生成的驗證碼敗橘
StringBuffer stringbuffer=new StringBuffer();

//隨機生成驗證碼的顏差襲色和字元
for(int i=0;i<4;i++)
{ //設置隨機顏色
g.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));

int index=random.nextInt(62);//這里的62就是從填充字元段中隨意選取一個位置
String str1=str.substring(index,index+1);
g.drawString(str1, 20*i, 30);//x,y數值設置太小會顯示不出來
stringbuffer.append(str1);
}

//將生成的驗證碼存到伺服器
request.getSession().setAttribute("checkcode", stringbuffer.toString());//key和value

//將圖片發送給瀏覽器
ImageIO.write(image, "jpg", response.getOutputStream());

}

}

用戶登錄界面代碼
package s1;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Login extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");// 設置伺服器發送給瀏覽器的編碼方式
request.setCharacterEncoding("UTF-8"); // 客戶端向伺服器提交的數據的解碼方式
// 獲得用戶提交的數據
String checkcode = request.getParameter("checkcode");
System.out.println(checkcode);

// 判斷輸入的驗證碼是不是符合
HttpSession session = request.getSession();// session是存放數據的地方
String str = (String) session.getAttribute("checkcode");

if (str != null) {
if (checkcode.compareToIgnoreCase(str) == 0) // 驗證碼忽略大小寫
response.getWriter().println("驗證碼輸入正確");

else
response.getWriter().println("驗證碼輸入錯誤");
}

else response.getWriter().println("驗證碼失效");

// 使用完的驗證碼信息要刪除,返回原頁面再輸一次,驗證碼就失效了
session.removeAttribute("checkcode");

}

}

Ⅱ java中如何編寫輸出一組由大寫和數字組成的6個隨機驗證碼,字母O和I不能輸出

importjava.util.Random;

publicclassTest{
publicstaticvoidmain(String[]args){
charc='i';
StringBuffersb=newStringBuffer();
for(inti=0;i<6;i++){
inta=Math.abs((newRandom()).nextInt(57));//產生0~57的隨機數
if(a<=9){//將0~9轉為char的0~9
sb.append((char)(a+48));
}elseif(a<33){//將10~33轉為char的A~Z
if((a+55)==79||(a+55)==73){
sb.append((char)(a+63));
}else{
sb.append((char)(a+55));
}
}else{//將33~57轉為char的a~z
sb.append((char)(a+63));
}
}
System.out.println("隨機生成的6位密碼為:"+sb.toString());
}
}

Ⅲ java如何隨機生成6位數的驗證碼

public static final char[] chars={'1','2','3','4','5','6','7','8','9','0','Q','W','E','R','T','Y','U','I','O',
'P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M'};
public static Random random=new Random();
public static String getRandomString(){
StringBuffer sb=new StringBuffer();
for(int i=0;i<=5;i++){
sb.append(chars[random.nextInt(chars.length)]);;
}
return sb.toString();
}

Ⅳ 用java生成6位驗證碼,由大小寫數字組成,不能重復字元

生成隨機可以用Random類,不能重復可以考慮放在HashSet中,因為HashSet的值是不能重復的,看代碼如下:

{
publicstaticvoidmain(String[]args){

Set<String>store=getletterandnum(6);

printSet(store);

}

publicstaticSet<String>getletterandnum(intlength){

Set<String>set=newHashSet<String>();

for(inti=0;i<length;i++){

Stringvalue=getrandom();

set.add(value);
}

if(set.size()<length){//如果沒有生成6位

Stringvalue=getrandom();//繼續調用生成隨機數的方法

set.add(value);

}

returnset;
}

privatestaticStringgetrandom(){//生成隨機字母和數字方法

Stringvalue="";

Randomrandom=newRandom();

intgen=random.nextInt(2);//0、1、2

Stringcharornum=gen%2==0?"char":"num";

if("char".equals(charornum)){

inttemp=random.nextInt(2)%2==0?65:97;

intascii=random.nextInt(26);

value+=(char)(ascii+temp);

}elseif("num".equalsIgnoreCase(charornum)){

value+=String.valueOf(random.nextInt(10));
}
returnvalue;
}

publicstaticvoidprintSet(Setset){//列印set的方法

Iteratoriterator=set.iterator();
while(iterator.hasNext()){
Stringele=(String)iterator.next();
System.out.print(ele+"");

}

}
閱讀全文

與java6位驗證碼相關的資料

熱點內容
源文件編譯成位元組碼文件 瀏覽:419
恢復的文檔怎麼加密 瀏覽:462
推塔榮耀怎麼連接不上伺服器 瀏覽:102
ios怎麼把app一次移到桌面 瀏覽:414
單片機蜂鳴器埠 瀏覽:216
安卓如何下載下架軟體 瀏覽:972
伺服器收費率怎麼取 瀏覽:897
python航空公司繪制熱力圖 瀏覽:202
安卓應用程序文件夾怎麼建立 瀏覽:447
曲線加密學基礎知識 瀏覽:213
vb中命令按鈕的英語是什麼 瀏覽:815
演算法不能申請專利 瀏覽:416
不會解壓手機文件怎麼辦 瀏覽:775
怎麼遠程式控制制app 瀏覽:270
java亂碼問號 瀏覽:149
自動互助鏈網站源碼php 瀏覽:115
蘋果怎麼下花季app 瀏覽:845
移動伺服器怎麼連接不上 瀏覽:833
配置資料庫伺服器如何設置IP 瀏覽:213
零基礎學st編程語言 瀏覽:704