如何利用Java進行高效的彩信群發
阿新 • • 發佈:2018-11-29
Java實現彩信群發的優勢就是簡單、效能高以及多執行緒處理能力。如果企業平臺要接入彩信群發介面,首先應該去該平臺申請賬號,不然只能進行一些簡單的測試,無法批量進行傳送。賬號申請完成之後得到appid和appkey,就能夠使用java對接服務商的彩信介面,然後建立彩信,傳送彩信了。下面介紹一種比較簡單的接入方式:
提供一個介面的demo供大家參考
package com.example.socketdemo; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Base64; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import sun.misc.BASE64Encoder; /** * 彩信介面demo * 平臺網址 :plat.veesing.com * @author MWH * */ @SuppressWarnings("restriction") public class test { public static void main(String[] args) { //建立介面地址 String url = "http://plat.veesing.com/mmsApi/commit"; //彩信標題 String title = "發彩信啦!"; //圖片轉BASE64格式 //引數傳檔案路徑 String img = GetImageStr("D:\\logo.jpg"); //文字轉GB2312格式 String txt = getTxt("文字內容示例:發彩信發彩信!"); // content拼接示例 對接之前必須仔細閱讀,content拼接失敗會導致傳送錯誤 /* 格式要求:包括型別,文字和圖片,圖片格式為base64,文字資訊格式為gb2312 每個資源內部包含多個型別資訊, 分別是:播放時間,型別 | 內容(按位元組碼base64編碼), 型別 | 內容(按位元組碼base64編碼), 每一幀資源型別和內容之間以 | 隔開,每一個幀可以有多個資源型別和內容,同一幀之間用半形逗號(,)分隔,幀與幀之間用分號(;)分隔。*/ String content = "3,jpg|"+img+",txt|"+txt+""; //System.out.println(content); // 新增引數 Map<String, String> map = new HashMap<String, String>(); map.put("appId", "");//請從平臺獲取 平臺地址 map.put("appkey", "");//請從平臺獲取 map.put("content", content);//彩信內容,必填 map.put("title", title);//彩信標題,必填 //呼叫建立彩信請求 //呼叫成功會返回 taskId //taskid就是建立好的彩信模板ID String result = MmsPost(url, map); System.out.println(result); //-------下面是傳送彩信-------- //建立介面地址 String sendurl = "http://plat.veesing.com/mmsApi/send"; Map<String, String> sendMap = new HashMap<>(); sendMap.put("appId", ""); sendMap.put("appKey", ""); sendMap.put("sentid","建立的taskid"); sendMap.put("sendtime", "傳送時間,不填代表立即傳送"); sendMap.put("sendto", "手機號碼,以,隔開"); //傳送彩信 String result2 = MmsPost(sendurl, sendMap); System.out.println(result2); } /** * 發起請求 * * @param url 介面地址 * @param map 引數 * @return * @throws IOException */ public static String MmsPost(String url, Map<String, String> map) { CloseableHttpClient httpClient = null; HttpPost httpPost = null; String charset = "utf-8"; String result = null; try { // 建立請求 httpClient = HttpClients.createDefault(); httpPost = new HttpPost(url); httpPost.setHeader("Content-Type", "application/json;charset=UTF-8"); List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); Iterator<Entry<String, String>> iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, String> param = (Map.Entry<String, String>) iterator.next(); list.add(new BasicNameValuePair(param.getKey(), param.getValue())); } // 判斷是否引數為空 if (list.size() > 0) { // 用來把輸入資料編碼成合適的內容 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset); httpPost.setEntity(entity); } HttpResponse response = httpClient.execute(httpPost); if (response != null) { HttpEntity httpEntity = response.getEntity(); if (httpClient != null) { result = EntityUtils.toString(httpEntity,charset); } } } catch (Exception e) { e.printStackTrace(); } finally { //關閉連線 try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } return result; } /** * 將圖片檔案轉化為位元組陣列字串,並對其進行Base64編碼處理 * @param imgFilePath 圖片路徑 * @return String */ public static String GetImageStr(String imgFilePath) { byte[] data = null; // 讀取圖片位元組陣列 try { InputStream in = new FileInputStream(imgFilePath); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } // 對位元組陣列Base64編碼 BASE64Encoder encoder = new BASE64Encoder(); // 返回Base64編碼過的位元組陣列字串,去除回車換行空格 return encoder.encode(data).replaceAll("[\\t\\n\\r]", ""); } /** * 字串轉GB2312格式 * @return str */ public static String getTxt(String txt){ String str = null; try { str = Base64.getEncoder().encodeToString(new String(txt).getBytes("gb2312")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return str; } }
介面返回引數
3000 |
傳送成功,彩信稽核中等待發送 |
4012 |
陣列格式錯誤 |
2001 |
單次提交號碼不能超過2000個 |
4011 |
餘額不足,請充值 |
2002 |
身份認證資訊有誤,請檢查APPID,APPKEY |
2000 |
建立成功 |
4005 |
引數不合法 |
4013 |
使用者資質未通過稽核或者正在稽核中 |
4010 |
賬戶被鎖定 |
4200 |
未進行企業認證,只能傳送系統預設模板 |
4080 |
定時時間必須大於當前時間30分鐘以上 |
4081 |
定時時間不正確 例:2017-4-5 15:34:00 |
4014 |
Sentid(模板ID)有誤 |
返回示例
{ "returnStatus": "2000", "message": "建立成功", "remainpoint": null, "taskId": "55", "successCounts": "1" }
好了,以上就是Java實現彩信群的簡單方法,有疑問可直接留言給我哦!