導航:首頁 > 源碼編譯 > java簡訊驗證碼源碼

java簡訊驗證碼源碼

發布時間:2023-02-07 00:18:31

❶ 驗證碼源代碼,jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@page import="java.awt.Graphics2D"%>
<%@page import="java.awt.Color"%>
<%@page import="java.awt.Font"%>
<%@page import="javax.imageio.ImageIO"%>

<%
int width = 60;
int height = 20;
// 創建具有可訪問圖像數據緩沖區的Image
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("Times New Roman", Font.PLAIN, 18);
// 設置字體
g.setFont(font);

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

// 隨機產生160條干擾線
g.setColor(Color.LIGHT_GRAY);
for (int i = 0; i < 160; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(12);
int y1 = random.nextInt(12);
g.drawLine(x, y, x + x1, y + y1);
}

// randomCode 用於保存隨機產生的驗證碼
StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0;

// 隨機產生4位數字的驗證碼
for (int i = 0; i < 4; i++) {
// 得到隨機產生的驗證碼數字
String strRand = String.valueOf(random.nextInt(10));

// 產生隨機的顏色分量來構造顏色值
red = random.nextInt(110);
green = random.nextInt(50);
blue = random.nextInt(50);

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

randomCode.append(strRand);
}

// 將四位數字的驗證碼保存到session中
//HttpSession session = request.getSession();
session.setAttribute("randomCode", randomCode.toString());

// 禁止圖像緩存
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);

response.setContentType("image/jpeg");
// 將圖像輸出到servlet輸出流中
ServletOutputStream sos = response.getOutputStream();
ImageIO.write(buffImg, "jpeg", sos);
sos.close();
//sos = null;
out.clear();
out = pageContext.pushBody();
%>

❷ java中發送簡訊驗證碼怎麼實現的

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

❸ Java簡訊驗證碼功能怎麼實現

要實現簡訊驗證碼的功能其實不難,首先找到一個靠譜點的簡訊平台,要求支持Java語言的介面,然後下載介面文檔,和自己的開發平台進行對接就行了,要注意在對接之前要測試一下簡訊的速度,一旦對接好想換就有點麻煩,我之前就吃過這個虧,最後有個朋友介紹我去簡訊網,還算可以。再提醒一下,如果要購買簡訊,一定要多測試幾家,選擇一個優質的平台。

❹ 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介面怎麼寫

這個首先你要確定一下簡訊平台,他們會給你提供簡訊實現的介面文檔。
比如:
public static String doPost(String reqUrl, Map parameters, String recvEncoding)
{
HttpURLConnection url_con = null;
String responseContent = null;
try
{
StringBuffer params = new StringBuffer();
Iterator iter = parameters.entrySet().iterator();
while (iter
.hasNext())
{
Map.Entry element = (Map.Entry)iter.next();
params.append(element.getKey().toString());
params.append("=");
params.append(URLEncoder.encode(element.getValue().toString(),
requestEncoding));
params.append("&");
}

if (params.length() > 0)
{
params = params.deleteCharAt(params.length() - 1);
}

URL url = new URL(reqUrl);
url_con = (HttpURLConnection)url.openConnection();
url_con.setRequestMethod("POST");
System.setProperty("sun.net.client.defaultConnectTimeout",
String.valueOf(connectTimeOut));
System.setProperty("sun.net.client.defaultReadTimeout",
String.valueOf(readTimeOut));

url_con.setDoOutput(true);
byte[] b = params.toString().getBytes();
url_con.getOutputStream().write(b, 0, b.length);
url_con.getOutputStream().flush();
url_con.getOutputStream().close();

InputStream in = url_con.getInputStream();
BufferedReader rd = new BufferedReader(
new InputStreamReader(in,
recvEncoding));
String tempLine = rd.readLine();
StringBuffer tempStr = new StringBuffer();
String crlf = System.getProperty("line.separator");
while (tempLine != null)
{
tempStr.append(tempLine);
tempStr.append(crlf);
tempLine = rd.readLine();
}
responseContent = tempStr.toString();
rd.close();
in.close();
}
catch (IOException localIOException)
{
}
finally
{
if (url_con != null)
{
url_con.disconnect();
}
}
return responseContent;
}

public static String sendTelCode(String mobile,String telcode){

Map<String ,String> map = new HashMap<String ,String>();
map.put("account", "Babo");
map.put("mobile", mobile);
map.put("pswd", "D3dddD");
try {
map.put("msg", java.net.URLEncoder.encode("您的驗證碼是"+telcode+",若非本人操作請忽略","utf-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

String getValue = doPost("http://www.ibabo.cn:7788/msg/HttpSendSM", map, "UTF-8");
System.out.println(getValue);
return getValue;
}

❻ java怎麼實現群發簡訊的功能

JAVA實現簡訊群發的步驟:

1、使用第三方簡訊平台服務商,接入簡訊服務;

2、調用簡訊提交頁面發送請求;

3、伺服器向第三方簡訊平台提交發送請求;

4、簡訊平台通過運營商將簡訊下發至用戶的手機上。

以下是秒賽簡訊平台JAVA簡訊驗證碼介面代碼示例

package test;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.net.URISyntaxException;

import java.net.URLEncoder;

import org.apache.commons.httpclient.HttpClient;

import org.apache.commons.httpclient.NameValuePair;

import org.apache.commons.httpclient.methods.PostMethod;

import org.apache.commons.lang3.StringUtils;

public class Apis {

// 簡訊發送介面的http地址,請咨詢客服

private static String url = 「xxxxxxxxxxxxxxxxxxxxxxxxxxxx」;

// 編碼格式。發送編碼格式統一用UTF-8

private static String ENCODING = 「UTF-8」;

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

// 賬號

String account = 「************************」;

// 密碼

String pswd = 「************************」;

// 修改為您要發送的手機號,多個用,分割

String mobile = 「13*********」;

// 設置您要發送的內容

String msg = 「【秒賽科技】您的驗證碼是:1234」;

// 發簡訊調用示例

System.out.println(Apis.send(account,pswd, mobile, msg));

}

/**

* 發送簡訊

*

* @param account

* account

* @param pswd

* pswd

* @param mobile

* 手機號碼

* @param content

* 簡訊發送內容

*/

public static String send(String account,String pswd, String mobile, String msg) {

NameValuePair[] data = { new NameValuePair(「account」, account),

new NameValuePair(「pswd」, pswd),

new NameValuePair(「mobile」, mobile),

new NameValuePair(「msg」, msg),

new NameValuePair(「needstatus」, 「true」),

new NameValuePair(「proct」, 「」) };

return doPost(url, data);

}

/**

* 基於HttpClient的post函數

* PH

* @param url

* 提交的URL

*

* @param data

* 提交NameValuePair參數

* @return 提交響應

*/

private static String doPost(String url, NameValuePair[] data) {

HttpClient client = new HttpClient();

PostMethod method = new PostMethod(url);

// method.setRequestHeader(「ContentType」,

// 「application/x-www-form-urlencoded;charset=UTF-8」);

method.setRequestBody(data);

// client.getParams()。setContentCharset(「UTF-8」);

client.getParams()。setConnectionManagerTimeout(10000);

try {

client.executeMethod(method);

return method.getResponseBodyAsString();

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

}

閱讀全文

與java簡訊驗證碼源碼相關的資料

熱點內容
1980法國電影少年的秘密 瀏覽:953
滿清十部電影大全 瀏覽:292
周末夫妻演員 瀏覽:345
末世之一女多男推薦 瀏覽:567
電腦VIP影視 瀏覽:395
看電影不要會員的網站 瀏覽:894
已知演算法文法G如下S 瀏覽:710
程序員周先生彩票 瀏覽:837
免費韓國中文倫理電影 瀏覽:975
大上海片尾曲 瀏覽:35
在哪裡可以看vr電視劇免費 瀏覽:15
steam的啟動項在文件夾的哪裡 瀏覽:375
午夜宅男看片網站 瀏覽:491
煤氣解壓伐不是加減調節是數字 瀏覽:123
對越自衛反擊戰電影大全集 影片 瀏覽:766
百度網盤小說資源 瀏覽:915
電影院第三排 瀏覽:516
sp兄弟訓誡文推薦 瀏覽:52
反垃圾雲網關如何防止郵件伺服器 瀏覽:716
韓國倫理電影翠花 瀏覽:83