當前位置:首頁 » 編程語言 » java驗證碼生成

java驗證碼生成

發布時間: 2022-06-07 01:01:50

Ⅰ 用java生成6位驗證碼,要求字母大小寫數字組成,不能重復字元

表達式轉換類
${expression}計算expression並輸出
#{ expression }數字計算#{ expression ;format}安格式輸出數字format為M和m
M表示小數點後最多的位數,m表示小數點後最少的位數如#{121.2322;m2M2}輸出121.23 !

Ⅱ 怎麼用Java代碼實現一個驗證碼,求具體實現方法

packageutil;

importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.OutputStream;
importjava.util.Random;
importjavax.imageio.ImageIO;

publicfinalclassImageUtil{

//驗證碼字元集
privatestaticfinalchar[]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'};
//字元數量
privatestaticfinalintSIZE=4;
//干擾線數量
privatestaticfinalintLINES=5;
//寬度
privatestaticfinalintWIDTH=80;
//高度
privatestaticfinalintHEIGHT=40;
//字體大小
privatestaticfinalintFONT_SIZE=30;

/**
*生成隨機驗證碼及圖片
*返回的數組中,第1個值是驗證碼,第2個值是圖片
*/
publicstaticObject[]createImage(){
StringBuffersb=newStringBuffer();
//1.創建空白圖片
BufferedImageimage=newBufferedImage(
WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
//2.獲取圖片畫筆
Graphicsgraphic=image.getGraphics();
//3.設置畫筆顏色
graphic.setColor(Color.LIGHT_GRAY);
//4.繪制矩形背景
graphic.fillRect(0,0,WIDTH,HEIGHT);
//5.畫隨機字元
Randomran=newRandom();
for(inti=0;i<SIZE;i++){
//取隨機字元索引
intn=ran.nextInt(chars.length);
//設置隨機顏色
graphic.setColor(getRandomColor());
//設置字體大小
graphic.setFont(newFont(
null,Font.BOLD+Font.ITALIC,FONT_SIZE));
//畫字元
graphic.drawString(
chars[n]+"",i*WIDTH/SIZE,HEIGHT/2);
//記錄字元
sb.append(chars[n]);
}
//6.畫干擾線
for(inti=0;i<LINES;i++){
//設置隨機顏色
graphic.setColor(getRandomColor());
//隨機畫線
graphic.drawLine(ran.nextInt(WIDTH),ran.nextInt(HEIGHT),
ran.nextInt(WIDTH),ran.nextInt(HEIGHT));
}
//7.返回驗證碼和圖片
returnnewObject[]{sb.toString(),image};
}

/**
*隨機取色
*/
(){
Randomran=newRandom();
Colorcolor=newColor(ran.nextInt(256),
ran.nextInt(256),ran.nextInt(256));
returncolor;
}

publicstaticvoidmain(String[]args)throwsIOException{
Object[]objs=createImage();
BufferedImageimage=(BufferedImage)objs[1];
OutputStreamos=newFileOutputStream("d:/1.png");
ImageIO.write(image,"jpeg",os);
os.close();
}

}

Ⅲ java如何實現發送簡訊驗證碼功能

1、創建一個Http的模擬請求工具類,然後寫一個POST方法或者GET方法

/** * 文件說明 * @Description:擴展說明 * @Copyright: XXXX dreamtech.com.cn Inc. All right reserved * @Version: V6.0 */package com.demo.util; import java.io.IOException;import java.util.Map; import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpException;import org.apache.commons.httpclient.SimpleHttpConnectionManager;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.httpclient.methods.PostMethod; /** * @Author: feizi * @Date: XXXX年XX月XX日 XX:XX:XX * @ModifyUser: feizi * @ModifyDate: XXXX年XX月XX日 XX:XX:XX * @Version:V6.0 */public class HttpRequestUtil { /** * HttpClient 模擬POST請求 * 方法說明 * @Discription:擴展說明 * @param url * @param params * @return String * @Author: feizi * @Date: XXXX年XX月XX日 XX:XX:XX * @ModifyUser:feizi * @ModifyDate: XXXX年XX月XX日 XX:XX:XX */ public static String postRequest(String url, Map<String, String> params) { //構造HttpClient的實例 HttpClient httpClient = new HttpClient(); //創建POST方法的實例 PostMethod postMethod = new PostMethod(url); //設置請求頭信息 postMethod.setRequestHeader("Connection", "close"); //添加參數 for (Map.Entry<String, String> entry : params.entrySet()) { postMethod.addParameter(entry.getKey(), entry.getValue()); } //使用系統提供的默認的恢復策略,設置請求重試處理,用的是默認的重試處理:請求三次 httpClient.getParams().setBooleanParameter("http.protocol.expect-continue", false); //接收處理結果 String result = null; try { //執行Http Post請求 httpClient.executeMethod(postMethod); //返回處理結果 result = postMethod.getResponseBodyAsString(); } catch (HttpException e) { // 發生致命的異常,可能是協議不對或者返回的內容有問題 System.out.println("請檢查輸入的URL!"); e.printStackTrace(); } catch (IOException e) { // 發生網路異常 System.out.println("發生網路異常!"); e.printStackTrace(); } finally { //釋放鏈接 postMethod.releaseConnection(); //關閉HttpClient實例 if (httpClient != null) { ((SimpleHttpConnectionManager) httpClient.getHttpConnectionManager()).shutdown(); httpClient = null; } } return result; } /** * HttpClient 模擬GET請求 * 方法說明 * @Discription:擴展說明 * @param url * @param params * @return String * @Author: feizi * @Date: XXXX年XX月XX日 XX:XX:XX * @ModifyUser:feizi * @ModifyDate: XXXX年XX月XX日 XX:XX:XX */ public static String getRequest(String url, Map<String, String> params) { //構造HttpClient實例 HttpClient client = new HttpClient(); //拼接參數 String paramStr = ""; for (String key : params.keySet()) { paramStr = paramStr + "&" + key + "=" + params.get(key); } paramStr = paramStr.substring(1); //創建GET方法的實例 GetMethod method = new GetMethod(url + "?" + paramStr); //接收返回結果 String result = null; try { //執行HTTP GET方法請求 client.executeMethod(method); //返回處理結果 result = method.getResponseBodyAsString(); } catch (HttpException e) { // 發生致命的異常,可能是協議不對或者返回的內容有問題 System.out.println("請檢查輸入的URL!"); e.printStackTrace(); } catch (IOException e) { // 發生網路異常 System.out.println("發生網路異常!"); e.printStackTrace(); } finally { //釋放鏈接 method.releaseConnection(); //關閉HttpClient實例 if (client != null) { ((SimpleHttpConnectionManager) client.getHttpConnectionManager()).shutdown(); client = null; } } return result; }}

2、在創建一個類,生成驗證碼,然後傳遞相應的參數(不同的簡訊平台介面會有不同的參數要求,這個一般簡訊平台提供的介面文檔中都會有的,直接看文檔然後按要求來即可)

/** * 文件說明 * @Description:擴展說明 * @Copyright: XXXX dreamtech.com.cn Inc. All right reserved * @Version: V6.0 */package com.demo.util; import java.net.URLEncoder;import java.util.HashMap;import java.util.Map; /** * @Author: feizi * @Date: XXXX年XX月XX日 XX:XX:XX * @ModifyUser: feizi * @ModifyDate: XXXX年XX月XX日 XX:XX:XX * @Version:V6.0 */public class SendMsgUtil { /** * 發送簡訊消息 * 方法說明 * @Discription:擴展說明 * @param phones * @param content * @return * @return String * @Author: feizi * @Date: 2015年4月17日 下午7:18:08 * @ModifyUser:feizi * @ModifyDate: 2015年4月17日 下午7:18:08 */ @SuppressWarnings("deprecation") public static String sendMsg(String phones,String content){ //簡訊介面URL提交地址 String url = "簡訊介面URL提交地址"; Map<String, String> params = new HashMap<String, String>(); params.put("zh", "用戶賬號"); params.put("mm", "用戶密碼"); params.put("dxlbid", "簡訊類別編號"); params.put("extno", "擴展編號"); //手機號碼,多個號碼使用英文逗號進行分割 params.put("hm", phones); //將簡訊內容進行URLEncoder編碼 params.put("nr", URLEncoder.encode(content)); return HttpRequestUtil.getRequest(url, params); } /** * 隨機生成6位隨機驗證碼 * 方法說明 * @Discription:擴展說明 * @return * @return String * @Author: feizi * @Date: 2015年4月17日 下午7:19:02 * @ModifyUser:feizi * @ModifyDate: 2015年4月17日 下午7:19:02 */ public static String createRandomVcode(){ //驗證碼 String vcode = ""; for (int i = 0; i < 6; i++) { vcode = vcode + (int)(Math.random() * 9); } return vcode; } /** * 測試 * 方法說明 * @Discription:擴展說明 * @param args * @return void * @Author: feizi * @Date: XXXX年XX月XX日 XX:XX:XX * @ModifyUser:feizi * @ModifyDate: XXXX年XX月XX日 XX:XX:XX */ public static void main(String[] args) {// System.out.println(SendMsgUtil.createRandomVcode());// System.out.println("&ecb=12".substring(1)); System.out.println(sendMsg("18123456789,15123456789", "尊敬的用戶,您的驗證碼為" + SendMsgUtil.createRandomVcode() + ",有效期為60秒,如有疑慮請詳詢XXX-XXX-XXXX【XXX中心】")); }

然後執行一下,一般的情況下參數傳遞正確,按照介面文檔的規范來操作的話,都會發送成功的,手機都能收到驗證碼的,然後可能會出現的問題就是:發送的簡訊內容有可能會出現中文亂碼,然後就會發送不成功,按照簡訊平台的要求進行相應的編碼即可。一般都會是UTF-8編碼。

Ⅳ Java如何實現驗證碼驗證功能

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'}; // 字元數量 private static final int SIZE = 4; // 干擾線數量 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; /** * 生成隨機驗證碼及圖片 * 返回的數組中,第1個值是驗證碼,第2個值是圖片 */ public static Object[] createImage() { StringBuffer sb = new StringBuffer(); // 1.創建空白圖片 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 登陸時的驗證碼怎麼做

後台寫一個生成圖片隨機的代碼,生成圖片給前台。切換圖片的時候,使用ajax獲取圖片數據就行。
附上生成圖片的代碼
public class ValidateCode {

private int width=180;
private int height=60;
private int codeCount = 4;
private int x = 0;
private int codeY;
private String Code;
private BufferedImage buffImg;
static char[] codeSequence = { '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', 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
private int fontHeight;

public ValidateCode() {
x = width / (codeCount + 2);
fontHeight = height - 2;
codeY = height - 4;
CreateCode();
}

public void CreateCode(){

// 定義圖像buffer
BufferedImage buffImg = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
// 創建一個隨機數生成器類
Random random = new Random();

// 將圖像填充為白色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);

// 創建字體,字體的大小應該根據圖片的高度來定。
Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);
// 設置字體。
g.setFont(font);

// 畫邊框。
g.setColor(Color.BLACK);
g.drawRect(0, 0, width - 1, height - 1);

// randomCode用於保存隨機產生的驗證碼,以便用戶登錄後進行驗證。
StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0;

// 隨機產生codeCount數字的驗證碼。
for (int i = 0; i < codeCount; i++) {
// 得到隨機產生的驗證碼數字。
String strRand = String.valueOf(codeSequence[random.nextInt(62)]);
// 產生隨機的顏色分量來構造顏色值,這樣輸出的每位數字的顏色值都將不同。
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);

// 用隨機產生的顏色將驗證碼繪制到圖像中。
g.setColor(new Color(red, green, blue));
g.drawString(strRand, (i ) * x+20, codeY);

// 將產生的四個隨機數組合在一起。
randomCode.append(strRand);
}
this.Code=randomCode.toString().toUpperCase();
this.buffImg=buffImg;

}

public String getCode() {
return Code;
}

public void setCode(String code) {
Code = code;
}

public BufferedImage getBuffImg() {
return buffImg;
}

public void setBuffImg(BufferedImage buffImg) {
this.buffImg = buffImg;
}
}

熱點內容
新逍客20發動機壓縮比 發布:2025-02-08 17:58:10 瀏覽:114
qq號和密碼我都知道為什麼登不上 發布:2025-02-08 17:52:21 瀏覽:872
寶塔伺服器ip進不去 發布:2025-02-08 17:52:18 瀏覽:382
擔保中介源碼 發布:2025-02-08 17:14:37 瀏覽:412
手機存儲卡速度測試 發布:2025-02-08 17:02:57 瀏覽:25
洪恩編程 發布:2025-02-08 17:02:19 瀏覽:814
linux遠程式控制制 發布:2025-02-08 17:02:16 瀏覽:153
珠心算演算法 發布:2025-02-08 17:00:37 瀏覽:919
動態ip可以做伺服器么 發布:2025-02-08 17:00:33 瀏覽:220
oracle定義存儲過程 發布:2025-02-08 16:54:35 瀏覽:151