当前位置:   article > 正文

使用java发送短信业务代码_java发送短信代码

java发送短信代码
import cn.hutool.core.util.StrUtil;
import com.dlfxzp.common.constant.Constants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;

public class HttpSend {
    private static Log log = LogFactory.getLog(HttpSend.class);

    /**
     * 环境参数
     */
    //private static String environment = ((Environment) SpringUtils.getBean("environment")).getProperty("spring.profiles.active");

    /**
     * 短信发送接口
     * 建议不超过300字 + 100个手机号码,或者不超过800字 + 30个手机号码
     *
     * @param phone    接收人号码:多个之间用“,”分割,例如:130000000,12000000000
     * @param content  短信类容
     * @param sendTime 定时发送时间。可以为空,为空就是立即发送。定时发送格式为年月日时分,如:200803201615。,
     * @return int 大于0,这是成功的条数,小于等于0发送失败
     */
    public static int send(String phone, String content, String sendTime) {
        // todo 暂时注释掉,生产需要放开
//        if (!("prod".equals(environment) || "qa".equals(environment))) {
//            to = "";
//        }
        if (StrUtil.isBlank(phone)) {
            return 0;
        }
        try {
            String _content = java.net.URLEncoder.encode(content, "UTF-8");
            String _param = "Account=" + Constants.sms_user(你自己的用户) + "&Password=" + Constants.sms_pwd(你的密码)
                    + "&Phone=" + phone + "&Content=" + _content
                    + "&SubCode=&SendTime=" + sendTime;
            log.info("短信发送参数为:" + _param);
            URL url = null;
            HttpURLConnection urlConn = null;
            url = new URL(Constants.sms_url(你自己使用平台的url));
            urlConn = (HttpURLConnection) url.openConnection();
            urlConn.setRequestMethod("POST");
            urlConn.setDoOutput(true);
            OutputStream out = urlConn.getOutputStream();
            out.write(_param.getBytes("GBK"));
            out.flush();
            out.close();
            BufferedReader rd = new BufferedReader(new InputStreamReader(
                    urlConn.getInputStream(), "UTF-8"));
            StringBuffer sb = new StringBuffer();
            int ch;
            while ((ch = rd.read()) > -1) {
                sb.append((char) ch);
            }
            log.info("短信发送返回结果为:" + sb.toString());
            Document document = DocumentHelper.parseText(sb.toString());
            Element root = document.getRootElement();
            int res = Integer.parseInt(root.elementText("response"));
            rd.close();
            return res;
        } catch (Exception ex) {
            log.error("短信发送异常:", ex);
            return 0;
        }
    }

    public static String MD5Encode(String sourceString) {
        String resultString = null;
        try {
            resultString = new String(sourceString);
            MessageDigest md = MessageDigest.getInstance("MD5");
            resultString = byte2hexString(md.digest(resultString.getBytes()));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return resultString;
    }

    public static final String byte2hexString(byte[] bytes) {
        StringBuffer bf = new StringBuffer(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            if ((bytes[i] & 0xff) < 0x10) {
                bf.append("0");
            }
            bf.append(Long.toString(bytes[i] & 0xff, 16));
        }
        return bf.toString();
    }

    public static void main(String[] args) {
        HttpSend.send("137********", "定量分析指数专屏短信发送", "");
    }
}

  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/908876
推荐阅读
相关标签
  

闽ICP备14008679号