⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pushaction.java

📁 中国联通短信通信协议
💻 JAVA
字号:
package com.wireless.sms.gwif.smsagent.func;

import java.util.*;
//import org.apache.log4j.Logger;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class PushAction
    extends AbstractAction {
  private static String HexCode[] = {
      "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
      "a", "b", "c", "d", "e", "f"
  };

  public PushAction() {
  }

//  public static void main(String[] args) {
//    String termid = "13754568503";
//    String content = "享受清爽音乐风暴;F4罩杯“挺”进歌坛;免费炫铃随意下!";
//    String url = "wap.phoho.com/myltx/";
//    PushAction pa = new PushAction();
//
//    String[] ss = pa.formatM(termid, content, url);
//    for (int i = 0; i < ss.length; i++) {
//      System.out.println(ss[i].length() / 2 - 1);
//      System.out.println(ss[i]);
//
//    }
//  }
//added by wtg 用于得到二进制图片
  public byte[] getPic(String content) {
    System.out.println("length="+content.length());
    int length = content.length();
    byte[] b = new byte[length/2];
    for (int t = 0, i = 0; t < length; t += 2) {
      String tempS = content.substring(t, t + 2);
      System.out.println("tempS="+tempS);
      b[i++] = (byte) getPicInt(tempS);
    }
    return b;
  }

  public int getPicInt(String content) {
    int t = 0;
    if (content.length() == 2) {
      String h = content.substring(0, 1);
      System.out.println("-------h="+h);
      int hh = 0;
      for (int i = 0; i < HexCode.length; i++) {
        System.out.println("HexCode["+i+"]="+HexCode[i]);
        if (HexCode[i].equalsIgnoreCase(h)) {
          hh = i;
          break;
        }

      }
      String l = content.substring(1, 2);
      System.out.println("-------l="+l);
      int ll = 0;
      for (int i = 0; i < HexCode.length; i++) {
        if (HexCode[i].equalsIgnoreCase(l)) {
          ll = i;
          break;
        }

      }
      t = 16 * hh + ll;
    }
    return t;
  }

  private String formatContent(String content) {
    byte[] bb = content.getBytes();
    StringBuffer sb = new StringBuffer();
    try {
      content = new String(bb, "GBK");
      bb = content.getBytes("UTF-8");
      for (int i = 0; i < bb.length; i++) {
        String s = Integer.toHexString(bb[i]);
        if (s.length() == 1) {
          sb.append("0");
        }
        if (bb[i] < 0) {
          s = s.substring(s.length() - 2);
        }
        sb.append(s);
      }
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    //System.out.println(sb.toString());
    return sb.toString();
  }

  private String formatURL(String url) {
    byte[] bb = url.getBytes();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < bb.length; i++) {
      sb.append(Integer.toHexString(bb[i]));
    }
    //System.out.println(sb.toString());
    return sb.toString();
  }

  public byte[] format(String content, String url) {

    byte[] bytContent = null;
    byte[] bytURL = url.getBytes();
    try {
      bytContent = content.getBytes();
      content = new String(bytContent, "GBK");
      bytContent = content.getBytes("UTF-8");
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }

    int iContentLen = bytContent.length;
    int iURLLen = bytURL.length;

    byte[] bytMsg = new byte[36 + iContentLen + iURLLen];

    int i = 0;
    bytMsg[i++] = (byte) 0x06;
    bytMsg[i++] = (byte) 0x05;
    bytMsg[i++] = (byte) 0x04;
    bytMsg[i++] = (byte) 0x0b;
    bytMsg[i++] = (byte) 0x84;
    bytMsg[i++] = (byte) 0x23;
    bytMsg[i++] = (byte) 0xf0;

    bytMsg[i++] = (byte) 0x25;
    bytMsg[i++] = (byte) 0x06;
    bytMsg[i++] = (byte) 0x0a;
    bytMsg[i++] = (byte) 0x03;
    bytMsg[i++] = (byte) 0xae;
    bytMsg[i++] = (byte) 0x81;
    bytMsg[i++] = (byte) 0xea;
    bytMsg[i++] = (byte) 0xaf;
    bytMsg[i++] = (byte) 0x82;
    bytMsg[i++] = (byte) 0x8d;
    bytMsg[i++] = (byte) 0xae; //18

    int ipos = i;
    bytMsg[i++] = (byte) 159;
    bytMsg[i++] = (byte) 0x87;
    bytMsg[i++] = (byte) 0x01;
    bytMsg[i++] = (byte) 0x05;
    bytMsg[i++] = (byte) 0x6a;
    bytMsg[i++] = (byte) (iContentLen + 1);

    //bytMsg[i] = $msgcontent;
    System.arraycopy(bytContent, 0, bytMsg, i, iContentLen);
    i += iContentLen;
    bytMsg[i++] = (byte) 0x00;
    bytMsg[i++] = (byte) 0x45;
    bytMsg[i++] = (byte) 0xc6;
    bytMsg[i++] = (byte) 0x0c;
    bytMsg[i++] = (byte) 0x03;

    //bytMsg[i] = $url;
    System.arraycopy(bytURL, 0, bytMsg, i, iURLLen);
    i += iURLLen;
    bytMsg[ipos] += (byte) iURLLen;
    bytMsg[i++] = (byte) 0x00;
    bytMsg[i++] = (byte) 0x08;
    bytMsg[i++] = (byte) 0x01;
    bytMsg[i++] = (byte) 0x83;
    bytMsg[i++] = (byte) 0x00;
    bytMsg[i++] = (byte) 0x01;
    bytMsg[i++] = (byte) 0x01;

    return bytMsg;
  }

  public String formatS(String termid, String content, String url) {
    StringBuffer push = new StringBuffer();
    content = formatContent(content);
    url = formatURL(url);
//    int total = 70 + content.length() + 6 + url.length() + 6;
    int msg = 38 + content.length() + 6 + url.length() + 6;
    if (msg / 2 > 140) { //multiple message
      return null;
    }
    String t = getFormatedTermId(termid);
    String tLen = Integer.toHexString(termid.length());
    if (tLen.length() < 2) {
      tLen = "0" + tLen;
    }
    push.append("00"); //default smsc number
    push.append("51"); //submit type
    push.append("00"); //SMS_TP_Message_Reference type
    push.append(tLen); //length of dest number
    push.append("81"); //number type
    push.append(t); //dest number
    push.append("00"); //SMS_TP_PID
    push.append("f5"); //SMS DCS
    push.append("aa"); //SMS available date
    push.append(Integer.toHexString(msg / 2)); //length of sms body!!!
    push.append("0605040B8423F0"); //wap push header 1
    push.append("dc0601ae02056a0045c60c03"); //wap push header 2
    push.append(url); //
    push.append("000103"); //
    push.append(content); //
    push.append("000101"); //
    return push.toString();
  }

  /**
   *
   * smsheader = 32
   * 0051000d91683157548605f300f5aaXX
   * wapheader = 54
   * 0B05040B8423F00003EBXXXXXX060403AE81EA02056A0045C60C03
   * a message = smsheader + wapheader + part of content
   * wapheader + part of content = 280
   * 54 + 226 = 2*140
   */
  public String[] formatM(String termid, String content, String url) {
    content = formatContent(content);
    url = formatURL(url);
    String msg = url + "000103" + content + "000101";
//    int smsheader = 32;
    int wapheader = 54;
    int msglength = msg.length();
    int num = 0;
    while (msglength >= 0) {
      if (num == 0) {
        msglength -= 2 * 140 - wapheader;
      }
      else {
        msglength -= 2 * 140 - (wapheader - 30);
      }
      num++;
    }
    String[] msgs = new String[num];
    Random r = new Random();
    String seq = getHexNumber(r.nextInt(255));
    int point = 0;
    for (int i = 0; i < num; i++) {
      StringBuffer push = new StringBuffer();
      String m = "";
      if (i == (num - 1)) {
        m = msg.substring(point);
      }
      else if (i == 0) {
        m = msg.substring(point, 226);
        point += 226;
      }
      else {
        m = msg.substring(point, point + 256);
        point += 256;
      }
      String t = getFormatedTermId(termid);
      String tLen = Integer.toHexString(termid.length());
      if (tLen.length() < 2) {
        tLen = "0" + tLen;
      }
      push.append("00"); //default smsc number
      push.append("51"); //submit type
      push.append("00"); //SMS_TP_Message_Reference type
      push.append(tLen); //length of dest number
      push.append("81"); //number type
      push.append(t); //dest number
      push.append("00"); //SMS_TP_PID
      push.append("f5"); //SMS DCS
      push.append("aa"); //SMS available date
      if (i == 0) {
        push.append(Integer.toHexString( (m.length() + 54) / 2));
      }
      else {
        push.append(Integer.toHexString( (m.length() + 24) / 2));
      }
      push.append("0B05040B8423F00003");
      push.append(seq);
      push.append(getHexNumber(num)); //total number of messages
      push.append(getHexNumber(i + 1)); //current number of this message
      if (i == 0) {
        push.append(seq);
        push.append("060403AE81EA02056A0045C60C03");
      }
      push.append(m);
      msgs[i] = push.toString();
    }
    return msgs;
  }
public  static void main(String args[])
  {
    String t="0605041583158300480E011F07C00FC1F003E0F820FC20107E08041F044E03902700E409C07234016D9A6C59B6802C09B092446C22490D900890904400220909103C01E89E7E7917803C43E015208104A807C284100A40A502500821855008408102100AA1841008407E0210082143E0102000040807C230006018381806000C0D01800600600180B0";

    byte [] b=new PushAction().getPic(t);
    String n=com.wireless.sms.pub.util.CMPPTools.byteArrayToHexString(b);
    System.out.println("---------new String:"+n);
     System.out.println("---------old String:"+t);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -