1,首先要去看一下官方网站的短信发送接口!我直接补上代码,自己写的!
package com.hanren.util;
import java.util.HashMap;
import java.util.Random;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
public class SendMessage2 {
// 产品名称:云通信短信API产品,开发者无需替换
private static final String product = "Dysmsapi";
// 产品域名,开发者无需替换
private static final String domain = "dysmsapi.aliyuncs.com";
// 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
private static String accessKeyId = "";//填上你自己的
private static String accessKeySecret = "";//填上你自己的
private static String signName = "注册验证";
private static String identifyingTempleteCode = "";//填上你自己的
private static String phone;
public static void init(String accessKeyId, String accessKeySecret, String signName, String identifyingTempleteCode) {
SendMessage2.accessKeyId = accessKeyId;
SendMessage2.accessKeySecret = accessKeySecret;
SendMessage2.signName = signName;
SendMessage2.identifyingTempleteCode = identifyingTempleteCode;
}
//生成验证码
public static int randomInt(int from, int to) {
Random r = new Random();
return from + r.nextInt(to - from);
}
public static HashMap<String,String> m = new HashMap<String,String>();
@SuppressWarnings("unused")
public static void main(String[] args) throws Throwable {
SendMessage2.init(accessKeyId, accessKeySecret, signName, identifyingTempleteCode);
//生成六位验证码
String charValue = "";
for (int i = 0; i < 6; i++) {
char c = (char) (randomInt(0, 9) + '0');
charValue += String.valueOf(c);
}
// 发短信
SendSmsResponse response = SendMessage2.sendIdentifyingCode(phone, charValue);
m.put("charValue",charValue);//自己可以定义话语
m.put("code", response.getCode());//这里是发送验证码放进map集合在后台中可以取出用于存入数据库,方便登陆的验证手机发送的验证码
System.out.println("短信接口返回的数据----------------");
}
public static SendSmsResponse sendSms(String mobile, String templateParam, String templateCode)
throws ClientException {
// 可自助调整超时时间
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
// 初始化acsClient,暂不支持region化
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
// 组装请求对象-具体描述见控制台-文档部分内容
SendSmsRequest request = new SendSmsRequest();
// 必填:待发送手机号
request.setPhoneNumbers(mobile);
// 必填:短信签名-可在短信控制台中找到
request.setSignName(signName);
// 必填:短信模板-可在短信控制台中找到
request.setTemplateCode(templateCode);
// 可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
request.setTemplateParam(templateParam);
// 选填-上行短信扩展码(无特殊需求用户请忽略此字段)
// request.setSmsUpExtendCode("90997");
// 可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
request.setOutId("yourOutId");
// hint 此处可能会抛出异常,注意catch
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
return sendSmsResponse;
}
public static SendSmsResponse sendIdentifyingCode(String mobile, String code) throws Throwable {
return sendSms(mobile, "{\"code\":\"" + code + "\"}", identifyingTempleteCode);
}
}
2,下面是插入数据库的操作同时返回前端的数据json数据!
//插入对象
@ResponseBody
@RequestMapping(value="/admin/insert",method=RequestMethod.POST)
public Result<String> Get2(Model md, Admin ad,MultipartFile picFile,HttpServletResponse response) throws Exception{
response.setHeader( "Access-Control-Allow-Origin", "*");//跨域设置
System.out.println(ad+"--"+"insert---------前台获取参数-----------insert封装对象");
if(as.checkByPersonId(ad.getPonserid1())) {
return new Result<String>("msg", "606");//用户已经存在!返回code=606,msg设置信息提示,一会附上我Result代码,你就明白了
}else if(ad.getPonserid1()==null||ad.getPonserid1()==""||ad.getPhone()==null||ad.getPhone()==""){
return new Result<String>("msg", "602");//ponserid是身份证ID,这里验证ID与phone是否为空,如果是就返回code=602提示msg为输入信息为空
}else{
TestSms ts = new TestSms(ad.getPhone());//这里用到了上面1附上的代码的短信发送工具类,ad.getPhone是你从前端传来的手机号,然后就请求到这里代表发送手机验证码到你传的这个手机号,(Admin ad)实体类封装前端各个数据与Admin的实体类字段对应
@SuppressWarnings("static-access")
HashMap<String, String> m=ts.sample(ad.getPhone());
System.out.println(m.get("code")+"=========code的值");
ad.setPhonecode(m.get("code"));//m是Hashmap用于存键值对,你发送的code验证码就在短信工具类SendMessage2中,你可以去看看我写的注释!ad.setPhonecode(m.get("code"))这是把code验证码存入ad实体类
ad.setPwd("123456");
ad.setStus("是");
ad.setFlag("未审核");
//下面是文件图片等上传的方法,有指定的路径,仔细辨别都是什么意思!!
//使用UUID给图片重命名,并去掉四个“-”
String name = UUID.randomUUID().toString().replaceAll("-", "");
//获取文件的扩展名
String ext = FilenameUtils.getExtension(picFile.getOriginalFilename());
String path = "D:\\pic";
System.out.println(path+"=========path的路径");
ad.setHeadImg(name+"_"+ad.getName1()+ "." + ext);
File dir1 = new File(path+"/"+name+"_"+ad.getName1()+"." + ext);
if(!dir1.exists()){
dir1.mkdirs();
}
picFile.transferTo(dir1);
as.registUser(ad);//把ad实体类存入数据库
System.out.println(ad+" insert admin===============insert成功插入后显示对象");
System.out.println(as.findUserByponserid(ad.getPonserid1()).getId()+"=========返回值id");
return new Result<String>(ad.getPonserid1(),"msg", "200");//返回前端的ponserid身份ID,同时msg=操作成功,code=200
}
}
3,这里是Result返回前端的code与msg工具类!
package com.hanren.util;
/**
* 结果包装类
*
* @author 于留俊 2018.11.29 15:25
* @author QQ:1099887856
* @author 微信:d17839166759
* @author qq邮箱:1099887856qq.com
*
*/
public class Result<T> {
private boolean success = true;
private String msg = "";
private T data = null;
private String code = ResultCode.SUCCESS.getCode();
public Result() {
super();
}
public Result(boolean success) {
super();
this.success = success;
}
public Result(boolean success, T data) {
super();
this.success = success;
this.data = data;
}
public Result( T data, String msg,String code) {
super();
this.code =code;
this.data = data;
if(code=="200") {
this.msg = msg="成功";
}else if(code=="400"){
this.msg = msg="失败";
}else if(code=="404"){
this.msg = msg="请求不存在";
}else if(code=="500"){
this.msg = msg="参数携带有误";
}else if(code=="600"){
this.msg = msg="所传id已经存在,重复";
}else if(code=="601"){
this.msg = msg="身份证id不能为空!";
}else if(code=="602"){
this.msg = msg="";
}
}
public Result(String msg,String code) {
super();
this.code =code;
if(code=="200") {
this.msg = msg="操作成功";
}else if(code=="400"){
this.msg = msg="操作失败";
}else if(code=="404"){
this.msg = msg="请求不存在";
}else if(code=="500"){
this.msg = msg="参数携带有误";
}else if(code=="600"){
this.msg = msg="所传id已经存在,重复";
}else if(code=="601"){
this.msg = msg="身份证id不能为空!";
}else if(code=="602"){
this.msg = msg="传参错误!";
}else if(code=="603"){
this.msg = msg="身份证id不存在!";
}else if(code=="604"){
this.msg = msg="ponserid不存在";
}else if(code=="605"){
this.msg = msg="用户不存在!";
}else if(code=="606"){
this.msg = msg="用户已经存在!";
}else if(code=="607"){
this.msg = msg="登陆成功!";
}else if(code=="608"){
this.msg = msg="注册成功!";
}else if(code=="609"){
this.msg = msg="用户已存在!";
}
}
public Result(boolean success, String msg, T data) {
super();
this.success = success;
this.msg = msg;
this.data = data;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
4,可以随时发送短信,不要浪费短息啊,小伙伴们!!