Java实现发送手机短信语音验证功能代码实例
这篇文章主要介绍了Java实现发送手机短信语音验证功能代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
利用第三方平台可以实现发送手机短信验证码和语音验证码的功能,本文使用框架是struts2+spring+hibernate,现就action层给出核心代码功能。
publicclassVerifyActionextendsBaseAction{ privatestaticfinallongserialVersionUID=1L; privateCommonConfigServicecommonConfigService; privateUserPhoneVerifyServiceuserPhoneVerifyService; privateUserVerifyServiceuserVerifyService; privatePhoneDetectServicephoneDetectService; privateAccountServiceaccountService; //发送短信验证码 publicStringsensSms(){ try{ result.setResult(JsonResult.SUCCESS); if(model.getType()==PhoneVerifyType.REGISTER){ if(userVerifyService.checkRegisterPhone(model.getPhone())){ result.setErrorMessage("该手机号已注册"); result.setResult(JsonResult.ERROR); returnSUCCESS; } } if(!phoneDetectService.check(model.getPhone())){ result.setErrorMessage("该手机号异常,不能进行验证操作"); result.setResult(JsonResult.ERROR); returnSUCCESS; } Dh3TongSmsOperatordh3TongSmsOperator=newDh3TongSmsOperator( commonConfigService.getString("dh3tong","account"), DigestUtil.md5(commonConfigService.getString("dh3tong","password"))); Stringcode=RandomUtil.randomNum(6); if(dh3TongSmsOperator.send(UUIDGenerator.generate(),model.getPhone(), String.format("尊敬的用户,您的验证码是:%s",code),"【XX系统名】")){ userPhoneVerifyService.record(model.getPhone(),model.getType(),code,model.getUserId()); }else{ result.setErrorMessage("很抱歉,发送短信失败,请稍后再试"); result.setResult(JsonResult.ERROR); returnSUCCESS; } result.setErrorMessage("短信验证码发送成功,请在5分钟内及时使用,谢谢!"); }catch(Exceptione){ e.printStackTrace(); } returnSUCCESS; } //发送语音验证码 publicStringsensVoice(){ try{ result.setResult(JsonResult.SUCCESS); if(model.getType()==PhoneVerifyType.REGISTER){ if(userVerifyService.checkRegisterPhone(model.getPhone())){ result.setErrorMessage("该手机号已注册"); result.setResult(JsonResult.ERROR); returnSUCCESS; } } if(!phoneDetectService.check(model.getPhone())){ result.setErrorMessage("该手机号异常,不能进行验证操作"); result.setResult(JsonResult.ERROR); returnSUCCESS; } Stringcode=RandomUtil.randomNum(6); Dh3TongVoiceOperatordh3TongVoiceOperator=newDh3TongVoiceOperator( commonConfigService.getString("dh3tong","account"), DigestUtil.md5(commonConfigService.getString("dh3tong","password"))); if(dh3TongVoiceOperator.send(UUIDGenerator.generate(),request.getPhone(),code)){ userPhoneVerifyService.record(request.getPhone(),request.getType(),code,request.getUserId()); }else{ result.setErrorMessage("很抱歉,发送语音失败,请稍后再试"); result.setResult(JsonResult.ERROR); returnSUCCESS; } result.setErrorMessage("语音验证码发送成功,请在5分钟内及时使用,谢谢!"); }catch(Exceptione){ e.printStackTrace(); } returnSUCCESS; } publicvoidsetCommonConfigService(CommonConfigServicecommonConfigService){ this.commonConfigService=commonConfigService; } publicvoidsetUserPhoneVerifyService(UserPhoneVerifyServiceuserPhoneVerifyService){ this.userPhoneVerifyService=userPhoneVerifyService; } publicvoidsetUserVerifyService(UserVerifyServiceuserVerifyService){ this.userVerifyService=userVerifyService; } publicvoidsetPhoneDetectService(PhoneDetectServicephoneDetectService){ this.phoneDetectService=phoneDetectService; } publicvoidsetAccountService(AccountServiceaccountService){ this.accountService=accountService; } }
以上是干货分享,已测试验证过!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。