mitao
2025-03-13 dba9034024de73ebf8d1184b53ec9a559c1c12c6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package com.ruoyi.auth.utils;
 
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.nacos.common.utils.StringUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
 
/**
 * 聚通达短信发送工具类
 *
 * @author mitao
 * @date 2024/6/17
 */
@Slf4j
public class JuTongDaSMSUtil {
 
    private static final String UID = "201000";
    private static final String PWD = "926893";
    private static final String template = "【酒鼎老酒】您的验证码为:${code},该验证码5分钟内有效,请勿泄露于他人!";
 
    public static void main(String[] args) {
        String moblies = "18283820718";
        String content = "123456";
        String ext = "";
        String attime = "";
        String result = JuTongDaSMSUtil.smsSend(moblies, content, ext, attime);
        System.out.println("result=" + result);
    }
 
    // 发送短信
    public static String smsSend(String mobile, String content,
            String ext, String attime) {
        String errMess = "";
        StringBuffer sendData = new StringBuffer("");
        try {
            sendData.append("uid=").append(UID);    // 用户名
            String pwd = getMD5(PWD);// 原始密码做MD5加密,32位大写格式
            sendData.append("&password=").append(pwd);    // 密码
 
            sendData.append("&encode=").append("GBK");    // encode=GBK或者encode=utf8
            content = template.replace("${code}", content);
            String contentBase64 = Base64.getEncoder()
                    .encodeToString(content.getBytes("gbk"));// 先用encode中定义的格式编码,再用base64加密内容
            sendData.append("&encodeType=base64");    // 固定
            sendData.append("&content=").append(contentBase64);    // base64加密后的内容
 
            sendData.append("&mobile=").append(mobile);    // 手机号
//            sendData.append("&cid=").append("45955855252252555");    // 唯一标识,选填,如果不填系统自动生成作为当前批次的唯一标识
            if (!StringUtils.isNotBlank(ext)) {
                sendData.append("&extNumber=").append(ext);    // 扩展
            }
 
            if (!StringUtils.isNotBlank(attime)) {
                sendData.append("&schtime=").append(attime);    // 定时时间,选填,格式2008-06-09 12:00:00
            }
            errMess = sendPost("https://sms3api.jvtd.cn/jtdsms/smsSend",
                    sendData.toString());// 普通短信
//            errMess =sendPost("http://ip:8090/jtdsms/sendData.do", sendData.toString());//个性短信
        } catch (Exception e) {
            errMess = "-601";
        }
        return errMess;
    }
 
    public static void smsSendBatch(List<String> mobileList, String content,
            String ext, String attime) {
        log.info("发送短信:号码{},内容:{}", JSONObject.toJSONString(mobileList), content);
        for (String mobile : mobileList) {
            String errMess = "";
            StringBuffer sendData = new StringBuffer("");
            try {
                sendData.append("uid=").append(UID);    // 用户名
                String pwd = getMD5(PWD);// 原始密码做MD5加密,32位大写格式
                sendData.append("&password=").append(pwd);    // 密码
 
                sendData.append("&encode=").append("GBK");    // encode=GBK或者encode=utf8
                String contentBase64 = Base64.getEncoder()
                        .encodeToString(content.getBytes("gbk"));// 先用encode中定义的格式编码,再用base64加密内容
                sendData.append("&encodeType=base64");    // 固定
                sendData.append("&content=").append(contentBase64);    // base64加密后的内容
 
                sendData.append("&mobile=").append(mobile);    // 手机号
//            sendData.append("&cid=").append("45955855252252555");    // 唯一标识,选填,如果不填系统自动生成作为当前批次的唯一标识
                if (!StringUtils.isNotBlank(ext)) {
                    sendData.append("&extNumber=").append(ext);    // 扩展
                }
 
                if (!StringUtils.isNotBlank(attime)) {
                    sendData.append("&schtime=").append(attime);    // 定时时间,选填,格式2008-06-09 12:00:00
                }
                errMess = sendPost("https://sms3api.jvtd.cn/jtdsms/smsSend",
                        sendData.toString());// 普通短信
//            errMess =sendPost("http://ip:8090/jtdsms/sendData.do", sendData.toString());//个性短信
            } catch (Exception e) {
                log.error("发送短信异常:", e);
                errMess = "-601";
            }
        }
    }
    public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常!" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }
 
 
    public static String getMD5(String sourceStr) {
        String resultStr = "";
        try {
            byte[] temp = sourceStr.getBytes();
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(temp);
            byte[] b = md5.digest();
            for (int i = 0; i < b.length; i++) {
                char[] digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
                        '9', 'A', 'B', 'C', 'D', 'E', 'F'};
                char[] ob = new char[2];
                ob[0] = digit[(b[i] >>> 4) & 0X0F];
                ob[1] = digit[b[i] & 0X0F];
                resultStr += new String(ob);
            }
            return resultStr;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }
}