springBoot+maven+秒嘀科技发送短信

一、先注册秒嘀科技(http://www.miaodiyun.com/?qmymark=dedcbe590015c3e82cc9bbfc7f3a1e74#B_vid=13731678955423562021)

①注册完成平台会分配相应的ACCOUNT SID和AUTH TOKEN。

②设置模板

 

 二、java代码实现

①相关maven包(不一定全部一样)

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.message</groupId>
    <artifactId>sendMessage</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>

    </dependencies>
    <build>
        <finalName>SMS</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

  

②代码

@Controller
@RequestMapping("myTest")
public class TestContr {
    
    public static final String ACCOUNT_SID = "4bb3abb626a44dea93171a4a158f18db";//这里填写你在平台里的ACOUNT_SID
    public static final String AUTH_TOKEN = "8375854a138f455098a1a69d8f50594a";//这里填写你在平台里的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/{to}" ,method = RequestMethod.POST)
    public String sendSMSG(@PathVariable 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+"&timestamp="+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();
    }
}

  

 

posted @ 2018-09-20 15:59  一块钱的约定  阅读(1099)  评论(0编辑  收藏  举报