如何利用Java进行高效的彩信群发

版权声明:此文章属于中昱维信短信平台撰写,如需转载,请联系www.veesing.com中的客服 https://blog.csdn.net/veesing/article/details/81450118

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实现彩信群的简单方法,有疑问可直接留言给我哦!

猜你喜欢

转载自blog.csdn.net/veesing/article/details/81450118