導航:首頁 > 編程語言 > java開源驗證碼

java開源驗證碼

發布時間:2023-06-10 00:34:57

❶ 如何用java代碼段生成四位數字加字母的驗證碼

不知道你問的是不是生成這種圖片驗證碼?如果只要一個隨機四位數 那這行代碼就夠了(new Random().nextInt(9000) + 1000;),如果是生成頁面圖片驗證碼就是下面的了: //設定 響應模式 resp.setContentType("image/jpeg"); // 生成令牌環數據; Integer token = new Random().nextInt(9000) + 1000; // 保存令牌環數據到session中 req.getSession().setAttribute(IMAGE_TOKEN_NAME, token); // 生成令牌環圖片 ServletOutputStream out = resp.getOutputStream(); BufferedImage img = new BufferedImage(60, 20, BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); g.setColor(Color.YELLOW); g.fillRect(0, 0, img.getWidth(), img.getHeight()); g.setColor(Color.BLUE); g.setFont(new Font("", Font.BOLD, 18)); g.drawString(String.valueOf(token), 10, 16); ImageIO.write(img, "jpg", out); out.close();
下面簡單的介紹他們的功能和用途,執行效率等。每個都有各自的優缺點看你是做甚什麼方面的研究開發用。.net,是網站編程,現在很多都用這個,但是這個語言編程都有統一思路,很好掌握。窒息那個效率不是很高;php 支持跨平台,很容易學會,執行的效率很高;asp是ASP.net的前身,它比較穩定,比.net要弱一點。但是比.net好學。jsp 是網頁編程,這個學習大約一周就能搞定,不過這個得多實踐,不然的話,時間長了,就容易忘記。
我自己做的系統裡面用作驗證碼的JSP的<%@page contentType="image/jpeg;charset=utf-8"%><%@page import="java.util.*,java.awt.*,java.awt.image.*,javax.imageio.*" %><%@ page import="java.io.OutputStream" %><html> <body> <%! Color getRandColor(int fc,int bc) { Random rd=new Random(); if(fc>255) fc=255; if(bc>255) bc=255; int red=fc+rd.nextInt(bc-fc); int green=fc+rd.nextInt(bc-fc); int blue=fc+rd.nextInt(bc-fc); return new Color(red,green,blue); } %> <% Random r=new Random(); response.addHeader("Pragma","No-cache"); response.addHeader("Cache-Control","no-cache"); response.addDateHeader("expires",0); int width=90; int height=23; BufferedImage pic=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); Graphics gc=pic.getGraphics(); gc.setColor(getRandColor(200,250)); gc.fillRect(0,0,width,height); String[] rNum ={"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"}; int[] style = {Font.PLAIN,Font.BOLD,Font.ITALIC,Font.PLAIN+Font.BOLD, Font.BOLD+Font.ITALIC,Font.PLAIN+Font.ITALIC,Font.PLAIN+Font.BOLD+Font.ITALIC}; gc.setColor(Color.WHITE); gc.drawLine(0,30,90,10); gc.setColor(getRandColor(160,200)); for (int i=0;i<50;i++) { int x = r.nextInt(width); int y = r.nextInt(height); int xl = r.nextInt(10); int yl = r.nextInt(10); gc.drawLine(x,y,x+xl,y+yl); } gc.setColor(getRandColor(60,150)); String rt = ""; for(int i=0;i<4;i++){ String temp = rNum[r.nextInt(62)]; rt = rt+temp; gc.setFont(new Font("Times New Roman",style[r.nextInt(7)],15)); gc.drawString(temp,5+i*15+r.nextInt(10),10+r.nextInt(10)); } gc.dispose(); session.setAttribute("randNum",rt); OutputStream os=response.getOutputStream(); ImageIO.write(pic,"JPEG",os); System.out.println("當前驗證碼為:"+session.getAttribute("randNum")); os.flush(); os.close(); os=null; response.flushBuffer(); out.clear(); out = pageContext.pushBody(); %> </body></html>

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

❹ java怎麼開通簡訊驗證碼登錄功能

實現jiava簡訊驗證碼可以按下面的步奏進行:
1、首先,找到一個支持Java語言的介面簡訊平台。
2、接著下載介面文檔,和自己的開發平台進行對接。
3、注意在對接之前測試一下簡訊的速度,一旦對接好想換就比較麻煩,之前就吃過這個虧,最後有個朋友介紹我去簡訊網。
4、如果要購買的話,一定要多測試幾家。
如果在碰到有疑問的地方一定要和技術或者客服多多溝通。

❺ JAVAWEB項目怎麼實現驗證碼

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

publicclassCode{

//圖片的寬度。
privateintwidth=160;
//圖片的高度。
privateintheight=38;
//驗證碼字元個數
privateintcodeCount=4;
//驗證碼干擾線數
privateintlineCount=20;
//驗證碼
privateStringcode=null;
//驗證碼圖片Buffer
privateBufferedImagebuffImg=null;
Randomrandom=newRandom();

privatebooleantype=false;

publicCode(){

}

publicCode(intwidth,intheight){
this.width=width;
this.height=height;
}

publicCode(intwidth,intheight,intcodeCount){
this.width=width;
this.height=height;
this.codeCount=codeCount;
}

publicCode(intwidth,intheight,intcodeCount,intlineCount){
this.width=width;
this.height=height;
this.codeCount=codeCount;
this.lineCount=lineCount;
}

publicvoidinit(booleantype){
this.type=type;
}

//生成圖片
privatevoidcreatImage(booleantype){
intfontWidth=width/codeCount;//字體的寬度
intfontHeight=height-5;//字體的高度
intcodeY=height-8;

//圖像buffer
buffImg=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphicsg=buffImg.getGraphics();
//Graphics2Dg=buffImg.createGraphics();
//設置背景色
g.setColor(getRandColor(200,250));
g.fillRect(0,0,width,height);//設置字體
Fontfont=null;
if(!type)font=newFont("Fixedsys",Font.BOLD,fontHeight);
elsefont=getFont(fontHeight);
g.setFont(font);

//設置干擾線
for(inti=0;i<lineCount/2;i++){
intxs=random.nextInt(width);
intys=random.nextInt(height);
intxe=xs+random.nextInt(width);
intye=ys+random.nextInt(height);
g.setColor(getRandColor(1,255));
if(!type)g.drawLine(xs,ys,xe,ye);
elseshear(g,width,height,getRandColor(1,255));
}

//添加噪點
floatyawpRate=0.01f;//雜訊率
intarea=(int)(yawpRate*width*height);
for(inti=0;i<area;i++){
intx=random.nextInt(width);
inty=random.nextInt(height);

buffImg.setRGB(x,y,random.nextInt(255));
}


Stringstr1=randomStr(codeCount);//得到隨機字元
this.code=str1;
for(inti=0;i<codeCount;i++){
StringstrRand=str1.substring(i,i+1);
g.setColor(getRandColor(1,255));
//g.drawString(a,x,y);
//a為要畫出來的東西,x和y表示要畫的東西最左側字元的基線位於此圖形上下文坐標系的(x,y)位置處

g.drawString(strRand,i*fontWidth+3,codeY);
}


}

//得到隨機字元
privateStringrandomStr(intn){
Stringstr1="";//I和l不要
Stringstr2="";
intlen=str1.length()-1;
doubler;
for(inti=0;i<n;i++){
r=(Math.random())*len;
str2=str2+str1.charAt((int)r);
}
returnstr2;
}

//得到隨機顏色
privateColorgetRandColor(intfc,intbc){//給定范圍獲得隨機顏色
if(fc>255)
fc=255;
if(bc>255)
bc=255;
intr=fc+random.nextInt(bc-fc);
intg=fc+random.nextInt(bc-fc);
intb=fc+random.nextInt(bc-fc);
returnnewColor(r,g,b);
}

/**
*產生隨機字體
*/
privateFontgetFont(intsize){
Randomrandom=newRandom();
Fontfont[]=newFont[5];
font[0]=newFont("Ravie",Font.PLAIN,size);
font[1]=newFont("AntiqueOliveCompact",Font.PLAIN,size);
font[2]=newFont("Fixedsys",Font.PLAIN,size);
font[3]=newFont("WideLatin",Font.PLAIN,size);
font[4]=newFont("GillSansUltraBold",Font.PLAIN,size);
returnfont[random.nextInt(5)];
}

//扭曲方法
privatevoidshear(Graphicsg,intw1,inth1,Colorcolor){
shearX(g,w1,h1,color);
shearY(g,w1,h1,color);
}

privatevoidshearX(Graphicsg,intw1,inth1,Colorcolor){

intperiod=random.nextInt(2);

booleanborderGap=true;
intframes=1;
intphase=random.nextInt(2);

for(inti=0;i<h1;i++){
doubled=(double)(period>>1)
*Math.sin((double)i/(double)period
+(6.2831853071795862D*(double)phase)
/(double)frames);
g.Area(0,i,w1,1,(int)d,0);
if(borderGap){
g.setColor(color);
g.drawLine((int)d,i,0,i);
g.drawLine((int)d+w1,i,w1,i);
}
}

}

privatevoidshearY(Graphicsg,intw1,inth1,Colorcolor){

intperiod=random.nextInt(40)+10;//50;

booleanborderGap=true;
intframes=20;
intphase=7;
for(inti=0;i<w1;i++){
doubled=(double)(period>>1)
*Math.sin((double)i/(double)period
+(6.2831853071795862D*(double)phase)
/(double)frames);
g.Area(i,0,1,h1,0,(int)d);
if(borderGap){
g.setColor(color);
g.drawLine(i,(int)d,i,0);
g.drawLine(i,(int)d+h1,i,h1);
}

}

}publicvoidwrite(OutputStreamsos)throwsIOException{
if(buffImg==null)creatImage(type);
ImageIO.write(buffImg,"png",sos);
//JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(sos);
//encoder.encode(buffImg);
sos.close();
}

publicBufferedImagegetBuffImg(){
if(buffImg==null)creatImage(type);
returnbuffImg;
}

publicStringgetCode(){
returncode.toLowerCase();
}

//使用方法
/*publicvoidgetCode3(HttpServletRequestreq,HttpServletResponseresponse,HttpSessionsession)throwsIOException{
//設置響應的類型格式為圖片格式
response.setContentType("image/jpeg");
//禁止圖像緩存。
response.setHeader("Pragma","no-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0);


CreateImageCodevCode=newCreateImageCode(100,30,5,10);
session.setAttribute("code",vCode.getCode());
vCode.write(response.getOutputStream());
response.flushBuffer();
}*/

}

❻ java怎麼實現驗證碼識別

圖片驗證碼是什麼
圖片驗證碼,這個大家應該都見過。最普遍的圖片驗證碼就是一張圖片上面有4-6個歪歪扭扭的數字字母,圖片還有點看不清楚,但是基本可以肉眼識別出上面的數字字母。那為什麼要有這個東東呢?

其實驗證碼的出現為了區分人與機器。對於歪歪妞妞還有點看不清的數字字母圖片,由於人腦的特殊構造,是可以完全無障礙識別的,但是想讓奇跡識別出這些字母數字,就會出現識別錯誤。那為什麼要區別人與機器呢?假如一個一個系統沒有驗證碼,我知道了你的用戶名,並且知道你的登錄密碼是8位的數字,那我完全可以寫個腳本程序窮舉出所有的8位數組合,挨個去嘗試登錄,這個過程對於人來說可能耗時耗力,但是對於程序來說,so easy。所以驗證碼的出現就會阻止程序進行這樣的窮舉登錄。

隨著技術的發展,現在很多的驗證碼系統都可以通過圖像處理、機器學習深度學習等方式進行攻破,圖片驗證碼已經不再安全,即使是非常有名的12306驗證碼,也已經被利用深度學習達到了很高的識別精度。所以也出現了手機驗證碼、拖動滑塊圖片到指定位置的驗證碼等各種驗證碼。

❼ java簡訊驗證碼如何驗證

先創建一個驗證碼,通過簡訊代理發送到用戶指定的手機,待用戶提交後做對比,看驗證碼是否一致。

整體流程:

  1. 用戶輸入手機號碼,點擊獲取驗證碼

  2. 伺服器創建驗證碼,並通過簡訊代理商發送到用戶手機

  3. 用戶查看驗證碼,輸入提交

  4. 伺服器進行確認,反饋成功或者失敗。

❽ 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代碼得到頁面上的驗證碼

具體的倒沒做過,不過原理應該差不多,不過不會簡單,一句兩句是將不清楚的,呵呵
基本原理是這樣的,這個圖片在IE的緩存文件夾Local Settings\Temporary Internet Files中一定會有一個對應的固定名稱的圖片,每次這個圖片文件名稱是一樣的,只是裡面的內容不一樣,你可以找一下看看,呵呵,下面就簡單了吧,不過,前提是這個頁面你要在瀏覽器載入過,這樣才能形成緩存文件。

你點這些分,我就說這么多了~
===================================================
呵呵,你這個不好解決,是不是想搞自動注冊呢?
人家驗證碼明顯是不會在客戶端產生的,這個是保存在服務端的,那你如何得到呢?客戶端得到的只是一個圖片而已,方法也有,就是你把這個圖片得到,動態解析,從這個圖片的解析中獲取它所表示的內容,這個難度有點大的,圖片解析難度比較高的,如果再加一些干擾,呵呵,基本能解出來的不是高手也差不多了。
所以,你的這個問題本身比較難實現,驗證碼使用的目的就是為了防止自動注冊,而且這個注冊碼本身是不會傳遞到客戶端的,所以,你要獲取的可能性很小。

❿ 用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開源驗證碼相關的資料

熱點內容
viper4android安卓60 瀏覽:485
java軟體源碼 瀏覽:159
空氣壓縮機的類型 瀏覽:352
centos圖形命令行界面切換 瀏覽:237
新京報新聞APP什麼時候有的 瀏覽:818
華為手機文件夾重命名空白 瀏覽:742
通俗理解螞蟻演算法 瀏覽:555
俠盜獵車手怎麼注冊伺服器 瀏覽:341
去商場吃飯預約什麼app 瀏覽:776
nginx不能解析php 瀏覽:135
安卓系統如何轉換中文 瀏覽:316
小米手機用什麼下載非官方app 瀏覽:760
linux修改readonly 瀏覽:32
演算法時代我們能做什麼 瀏覽:928
牛津英語搭配詞典pdf 瀏覽:284
慧連a6怎麼連接安卓 瀏覽:235
python使用什麼編譯器最好 瀏覽:52
小程序編譯藍屏 瀏覽:947
程序員賽車的gif 瀏覽:413
購買新車能用到什麼app 瀏覽:775