SendMsgUtil.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package com.kmall.api.util;
  2. import com.kmall.manager.manager.common.CommonPropertiesBuilder;
  3. import org.apache.http.HttpEntity;
  4. import org.apache.http.NameValuePair;
  5. import org.apache.http.client.entity.UrlEncodedFormEntity;
  6. import org.apache.http.client.methods.CloseableHttpResponse;
  7. import org.apache.http.client.methods.HttpPost;
  8. import org.apache.http.impl.client.CloseableHttpClient;
  9. import org.apache.http.impl.client.HttpClients;
  10. import org.apache.http.message.BasicNameValuePair;
  11. import org.apache.http.util.EntityUtils;
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16. /**
  17. * @author huangyaqin
  18. * @version 1.0
  19. * 2018-10-16 11:27
  20. */
  21. public class SendMsgUtil {
  22. //编码格式。发送编码格式统一用UTF-8
  23. private static String ENCODING = "UTF-8";
  24. // 请求地址
  25. private static String SMS_URL = "https://sms.yunpian.com/v2/sms/single_send.json";
  26. // 中网cw apikey
  27. private static String API_KEY = "1dd75986321871d706334e60b89c4021";
  28. /**
  29. * 发送短信消息
  30. * 方法说明
  31. * @Discription:扩展说明
  32. * @param phones
  33. * @param text
  34. * @return
  35. * @return String
  36. */
  37. @SuppressWarnings("deprecation")
  38. public static String sendMsg(String phones,String text) {
  39. System.out.println(text);
  40. String apikey = API_KEY;
  41. String url = SMS_URL;
  42. if(apikey != null && url != null){
  43. Map<String, String> params = new HashMap<String, String>();
  44. params.put("apikey", apikey);
  45. //手机号码,多个号码使用英文逗号进行分割
  46. params.put("mobile", phones);
  47. //将短信内容进行URLEncoder编码
  48. params.put("text", text);
  49. return post(url, params);
  50. }else{
  51. return "";
  52. }
  53. }
  54. /**
  55. * 随机生成6位随机验证码
  56. * 方法说明
  57. * @Discription:扩展说明
  58. * @return
  59. * @return String
  60. */
  61. public static String createRandomVcode(){
  62. //验证码
  63. String vcode = "";
  64. for (int i = 0; i < 6; i++) {
  65. vcode = vcode + (int)(Math.random() * 9);
  66. }
  67. return vcode;
  68. }
  69. /**
  70. * 基于HttpClient 4.3的通用POST方法
  71. *
  72. * @param url 提交的URL
  73. * @param paramsMap 提交<参数,值>Map
  74. * @return 提交响应
  75. */
  76. public static String post(String url, Map < String, String > paramsMap) {
  77. CloseableHttpClient client = HttpClients.createDefault();
  78. String responseText = "";
  79. CloseableHttpResponse response = null;
  80. try {
  81. HttpPost method = new HttpPost(url);
  82. if (paramsMap != null) {
  83. List<NameValuePair> paramList = new ArrayList<NameValuePair>();
  84. for (Map.Entry < String, String > param: paramsMap.entrySet()) {
  85. NameValuePair pair = new BasicNameValuePair(param.getKey(),
  86. param.getValue());
  87. paramList.add(pair);
  88. }
  89. method.setEntity(new UrlEncodedFormEntity(paramList,
  90. ENCODING));
  91. }
  92. response = client.execute(method);
  93. HttpEntity entity = response.getEntity();
  94. if (entity != null) {
  95. responseText = EntityUtils.toString(entity, ENCODING);
  96. }
  97. } catch (Exception e) {
  98. e.printStackTrace();
  99. } finally {
  100. try {
  101. response.close();
  102. } catch (Exception e) {
  103. e.printStackTrace();
  104. }
  105. }
  106. return responseText;
  107. }
  108. /**
  109. * 测试
  110. * 方法说明
  111. * @Discription:扩展说明
  112. * @param args
  113. * @return void
  114. */
  115. public static void main(String[] args) {
  116. // System.out.println(SendMsgUtil.createRandomVcode());
  117. // System.out.println("&ecb=12".substring(1));
  118. System.out.println(sendMsg("13229855975", "【CW惠州门店】惠州港惠店 尊敬的CW会员,您购买的订单56151,已清关成功,感谢您的耐心等待。"));
  119. }
  120. }