电信SMGP协议,基于开源的jar文件smgpapi20100113.jar进行实现

来源:互联网 发布:摄影灯品牌 知乎 编辑:程序博客网 时间:2024/06/17 02:11

一个电信项目,电信要求使用它们的短信接口SMGP进行短信通知.找了很多方法, 刚开始乱码,各种处理,后来通过smgpapi20100113.jar

电信分为长短信和短短信,具体实现如下:

import Java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.util.Properties;

import org.springframework.core.io.ClassPathResource;

 

import Sample.Config;

import cn.com.zjtelecom.smgp.Client;

import cn.com.zjtelecom.smgp.bean.Result;

import cn.com.zjtelecom.smgp.bean.Submit;

public classSmgpUtil {

   private static final String CONFURL = "config/smgp.properties";

   private static final String smgpHost = getConfigValue("smgpHost");//短信接口服务器ip

   private static final String smgpPort = getConfigValue("smgpPort");//服务器端口

   private static final String smgpUser = getConfigValue("smgpUser");//用户名

   private static final String smgpPwd = getConfigValue("smgpPwd");//密码

   private static final String smgpNo = getConfigValue("smgpNo");//电信提供的发送号码

   /**

    * 发送长短信

    * @param phoneNo  接收端号码

    * @param msg      消息内容

    * @throws IOException

    */

   public static void doSendLongMsg(String phoneNo,String msgthrows IOException{

      //soid一般指你的消息来源,如测试,或者来自XX项目的什么功能,参数传2和传是制定电信本次socket连接的具体用法,如发短信,收短信,还是其他

        Client client = new Client(smgpHost, Integer.valueOf(smgpPort.trim()), 2, smgpUsersmgpPwd"spid", 0);

        Submit submit = new Submit();

        submit.setSrcTermid(smgpNo);

        submit.setDestTermid(phoneNo);

        //短消息内容体的编码格式。

        //0ASCII 编码;

        //3=短消息写卡操作;

        //4=二进制短消息;

        //8UCS2 编码;

        //15GB18030 编码;

        //246F6)=UIM 相关消息 UIM 相关消息,用于与 UIM 卡相关的 OTA 等业务,终端

        //收到该类型消息直接转发给 UIM 卡,由 UIM 卡来处理该类型消息;

        //其它保留。

        //对于文字短消息,要求 MsgFormat15。对于回执消息,要求 MsgFormat0 

        submit.setMsgFormat(15);

        submit.setMsgContent(msg.getBytes("GB18030")); //将字符集编码和你设置的msgFormat对应

        submit.setProductID("产品ID");//可以为空

        submit.setMsgType(0);

        submit.setNeedReport(0);

        submit.setPriority(0);

        //serviceId表示业务代码,是该条短消息所属的业务类别,由数字、字母

        //和符号组合而成。对于从 WEB 上发送的点对点短消息,要求业务代码为 “PC2P” ,其它业务代码由 SP 自定义。

        submit.setServiceID("PC2P");

        // 发送短信

        Result[] result = client.SendLong(submit);

        for (int i = 0; i < result.lengthi++) {

                System.out.println("--------------------------------");

                System.out.println("Message "+i+"");

                System.out.println("Status:"result[i].ErrorCode);

                System.out.println("MsgID:"result[i].ErrorDescription);

                System.out.println("--------------------------------");

        }

        // 退出

        client.Close();

   }

   /**

    * 发送短短信

    * @param phoneNo  接收端号码

    * @param msg      消息内容

    * @throws IOException

    */

   public static void doSendShortMsg(String phoneNo,String msgthrows IOException{

      //初始化client

      ClientclientnewClient(smgpHost,Integer.valueOf(smgpPort.trim()), 2, smgpUsersmgpPwd,"spid",0);

      //设置submit

      Submitsubmit=newSubmit();

      submit.setSrcTermid(smgpNo);

      submit.setDestTermid(phoneNo);

      submit.setProductID("产品ID");//可以为空

        submit.setMsgType(0);

        submit.setNeedReport(0);

        submit.setPriority(0);

        submit.setServiceID("PC2P");

        submit.setMsgFormat(15);

      submit.setMsgContent(msg.getBytes("GB18030"));

      Result  result =client.Send(submit);

      System.out.println("Status:"+result.ErrorCode);

      System.out.println("MsgID:"+result.ErrorDescription);

      //退出

      client.Close();

   }

   privatestaticString getConfigValue(String key) {

 

   PropertiesconfignewProperties();

   InputStreamin = null;

   try {

       config.load(newClassPathResource(CONFURL).getInputStream());

   }catch(FileNotFoundException e) {

       e.printStackTrace();

   }catch(IOException e) {

       e.printStackTrace();

   }finally{

       if (in != null){

      try {

          in.close();

      }catch(IOException e) {

          e.printStackTrace();

      }

       }

   }

   return config.getProperty(key);

   }

}