Get the App
SLTechnology News&Howtos  ›  Development  › 

How to randomly generate 6-digit SMS CAPTCHA by java

Shulou Source: shulou.com Published: 2022-06-03 09:47:50 10月30日 Update

This article mainly shows you "how java randomly generates 6-digit SMS verification code". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how java randomly generates 6-digit SMS verification code".

Generating 6-digit random numbers is actually very simple, with only one line of code, as follows:

String verifyCode = String.valueOf (new Random (). NextInt (899999) + 100000)

Specifically implement the function of sending SMS verification code. Take registration as an example, the current code is as follows:

You need to use the third-party SMS CAPTCHA API. Here you use Lexin SMS CAPTCHA API, Lexin API API document description (http://www.lx598.com/apitext.html), and Lexin java call SMS CAPTCHA API.

/ / sign up for a new user @ Action (value = "reAimcodeGetVeCode") public void reAimcodeGetVeCode () {PrintWriter out; String result = "CAPTCHA application failed! please try again!" ; try {smsUnit = new SmsUnit (ConfUtil.getProperty ("sys_sms_server")); if (null! = account.getACCMOB () & &! account.getACCMOB (). Equals (")) {account.setACCSTATUS (new BigDecimal (1)); / / set usage status unused String verifyCode = String .valueof (new Random (). NextInt (899999) + 100000) / / generate SMS verification code account.setFSECURITYCODE (verifyCode); account.setACCCREATEDATE (new Date ()); / / set the expiration time of the verification code to 1 minute Calendar c = Calendar.getInstance (); c.add (Calendar. Minute in hour, 1); / execute SMS account.setFREGISTERSOURCE (fromSource); request.getSession () .removeAttribute (ConstValues.WEB_SESSION_PROMOTE); AccountCriteria ac = new AccountCriteria (); ac.createCriteria () .andACCMOBEqualTo (account.getACCMOB ()); List acList = new ArrayList (); acList = accountService.selectByExample (ac) If (acList! = null & & acList.size () > 0) {String content = "your CAPTCHA is:" + verifyCode+ ", which is valid for 24 hours and can only be used once! [SMS signature] "; SendSmsReply sendSmsReply = smsUnit.sendSms (accName,accPwd, account.getACCMOB (), content,"); / / call the third-party API to send SMS result = sendSmsReply.getReplyMsg () +" & "+ acList.get (0). GetFID () +" & "+ acList.get (0). GetSDKURL () }} catch (Exception e) {logger.error ("failed to get CAPTCHA", e);} finally {try {response.setContentType ("text/html;charset=UTF-8"); response.setCharacterEncoding ("UTF-8"); out = response.getWriter (); out.write (result);} catch (IOException e) {logger.error ("", e) } / / third-party SMS sending interface code: / * send SMS * @ param accName account user name * @ param accPwd Lexin account password * @ param seed current time format: YYYYMMDD HHMISS for example: 20130806102030 * @ param aimcodes mobile phone number between multiple mobile phone numbers English half-corner comma Add signature after opening * @ param content content * @ param schTime timing time format such as: 2010-01-01 08:00:00 * @ return the result returned by the server ok: business id or error code * / public static String sendSms (String accName String accPwd,String mobies,String content,String schTime) {StringBuffer sb = new StringBuffer ("http://sdk.lx198.com/sdk/send2?"); Try {String seed=new SimpleDateFormat (dateFormatStr) .format (new Date ()); sb.append (& accName= + accName); sb.append (& seed= + seed); sb.append (& accPwd= + MD5.getMd5String (MD5.getMd5String (accPwd) + seed)); sb.append (& aimcodes= + mobies) Sb.append ("& schTime=" + URLEncoder.encode (schTime, "UTF-8")); / / encode conversion sb.append for space punctuation ("& content=" + URLEncoder.encode (content, "UTF-8")); / / Chinese encode conversion URL url = new URL (sb.toString ()); HttpURLConnection connection = (HttpURLConnection) url.openConnection (); connection.setRequestMethod ("POST") BufferedReader in = new BufferedReader (new InputStreamReader (url.openStream (); return in.readLine ();} catch (Exception e) {e.printStackTrace ();} return null;}

PS:Java randomly generates four-digit CAPTCHA codes

Package com.day14string;import java.util.Random;public class Test2 {public String getCheckCode () {String ZiMu = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGJKLZXCVBNM1234567890"; String result = ""; Random random = new Random (); for (int I = 0; I < 4; iTunes +) {int index = random.nextInt (ZiMu.length ()); char c = ZiMu.charAt (index); result + = c;} return result;} public static void main (String [] args) {/ / TODO Auto-generated method stub Test2 test2 = new Test2 () System.out.println (test2.getCheckCode ());}} these are all the contents of the article "how to randomly generate 6-digit SMS verification codes by java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: SMS verification generation interface code content time third party article UTF-8 mobile phone number number format user SMS sending account learning help valid Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Redmi Shulou Information Linux Xiaomi