springboot +秒嘀 实现短信发送

短信发送

步骤一:注册秒嘀(或者用其他短信平台)
springboot +秒嘀 实现短信发送_第1张图片
第二步:添加短信模板,验证通过即可
springboot +秒嘀 实现短信发送_第2张图片
第三步:在springboot项目中添加依赖

        
            net.sf.json-lib
            json-lib
            2.4
            jdk15
        

第四步:添加工具类,修改标记的位置即可

package com.qf.fayuan.user.controller;

import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("myTest")
public class TestContr {

    public static final String ACCOUNT_SID = "eccbc60599474470aae2f840728xxxxx";//这里填写你在平台里的ACOUNT_SID
    public static final String AUTH_TOKEN = "b816dec86e5043508acb13d99d6xxxxx";//这里填写你在平台里的AUTH_TOKEN
    public static final String BASE_URL = "https://api.miaodiyun.com/20150822/industrySMS/sendSMS";//请求地址是固定的不用改
    public static int randNum = (int)((Math.random()*9+1)*100000);//随机数
    //短信模板
    public static String smsContent = "【一方家居】您的验证码为" + randNum + ",请于" + 2 + "分钟内正确输入,如非本人操作,请忽略此短信。";

    /**
     * 测试短信发送(平台:秒嘀科技),测试通过,可用=================================
     * @param to  接收信息手机号
     * @return
     */
    @RequestMapping(value = "/smsg" ,method = RequestMethod.GET)
    public String sendSMSG( String to){
        //设置时间戳
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        String timeStamp = format.format(date);

        //各个数据经过MD5加密之后返回sig
        String sig=MD5(ACCOUNT_SID,AUTH_TOKEN,timeStamp);
        String str = "accountSid="+ACCOUNT_SID+"&smsContent="+
                smsContent+"&to="+to+"×tamp="+timeStamp+"&sig="+sig;

        OutputStreamWriter out = null;
        InputStream in = null;
        BufferedReader br = null;
        StringBuffer sb = new StringBuffer();
        try {
            URL url = new URL(BASE_URL);
            URLConnection connection = url.openConnection();//打开连接
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setConnectTimeout(5000);  //设置链接超时
            connection.setReadTimeout(10000);    //设置读取超时
            out = new OutputStreamWriter(connection.getOutputStream(),"utf-8");
            out.write(str);
            out.flush();
            //读取返回数据
            br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line = "";
            while((line = br.readLine())!=null){
                sb.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        JSONObject jsonObject = JSONObject.fromObject(sb.toString());
        System.out.println(jsonObject);
        Object object = jsonObject.get("respCode");
        System.out.println("状态码:"+object+"验证码:"+randNum);
        System.out.println(!object.equals("00000"));
        return jsonObject.toString();
    }

    //MD5算法
    public static String MD5(String... args){ //动态参数
        StringBuffer result = new StringBuffer();
        if (args == null || args.length == 0) {
            return "";
        } else {
            StringBuffer str = new StringBuffer();
            for (String string : args) {
                str.append(string);
            }
            System.out.println("加密前:\t"+str.toString());

            try {
                MessageDigest digest = MessageDigest.getInstance("MD5");
                byte[] bytes = digest.digest(str.toString().getBytes());
                for (byte b : bytes) {
                    String hex = Integer.toHexString(b&0xff);  //转化十六进制
                    if (hex.length() == 1) {
                        result.append("0"+hex);
                    }else{
                        result.append(hex);
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("加密后:\t"+result.toString());
        return result.toString();
    }
}

你可能感兴趣的:(软件小技术)