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

📄 smspushmessage.java

📁 MM7彩信对接网关示例
💻 JAVA
字号:
package com.rainbow.mms.golden.base;

import java.io.ByteArrayOutputStream;

public class SMSPushMessage {

	public static byte[] WDP_DESTINATIONPORT = new byte[] { 0x0B, (byte) 0x84 };

	public static byte[] WDP_SOURCEPORT = new byte[] { (byte) 0x23, (byte) 0xf0 };

	public static byte[] Datagram_Ref_No = new byte[] { (byte) 04 };

	public static byte[] mut_wdp = new byte[] { (byte) 0x04, (byte) 0xc3,
			(byte) 0x4f, (byte) 0xc0, (byte) 0x02, (byte) 0x00, (byte) 0x03,
			(byte) 0x04 };

	private ServiceIndication si;

	public SMSPushMessage(String href, String text) {
		this.si = new ServiceIndication(href, text,
				ServiceIndication.SERVICEINDICATION_signal_high);
	}

	/**
	 * Generates the body of the SMS message
	 * 
	 * @return byte array
	 */
	public byte[] GetSMSBytes() {
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		byte[] wdpHeader = GetWDPHeaderBytes();
		stream.write(wdpHeader, 0, wdpHeader.length);

		byte[] pdu = GetPDUBytes();
		stream.write(pdu, 0, pdu.length);

		return stream.toByteArray();
	}

	public byte[] GetWDPHeaderBytes() {
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		ByteArrayOutputStream headerStream = new ByteArrayOutputStream();

		try {
			stream.write(WDP.INFORMATIONELEMENT_IDENTIFIER_APPLICATIONPORT);
			stream
					.write((byte) (WDP_DESTINATIONPORT.length + WDP_SOURCEPORT.length));
			stream.write(WDP_DESTINATIONPORT, 0, WDP_DESTINATIONPORT.length);
			stream.write(WDP_SOURCEPORT, 0, WDP_SOURCEPORT.length);

			// write length of header
			headerStream.write((byte) stream.size());

			stream.writeTo(headerStream);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return headerStream.toByteArray();
	}

	private static byte[] getPushBytes(String pushString) {
		pushString = "0" + pushString;
		int length = pushString.length() / 2;

		byte[] bMsg = new byte[length];
		for (int i = 0; i < length; i++) {
			byte x = Byte.parseByte(pushString.substring(2 * i, 2 * i + 1), 16);
			byte y = Byte.parseByte(pushString.substring(2 * i + 1, 2 * i + 2),
					16);
			int j = x * 16 + y;
			bMsg[i] = (byte) j;
		}
		return bMsg;
	}

	public byte[] GetMutiWDPHeaderBytes(int total, int count) {
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		ByteArrayOutputStream headerStream = new ByteArrayOutputStream();
		// byte[] totalByte = Integer.toHexString(total).getBytes();
		// byte[] countByte = Integer.toHexString(count).getBytes();
		String hexTotal = Integer.toHexString(total);
		String hexCount = Integer.toHexString(count);
		byte[] totalByte = getPushBytes(hexTotal);
		byte[] countByte = getPushBytes(hexCount);
		// HexDecoder decoder = new HexDecoder();

		try {
			stream
					.write(WDP.INFORMATIONELEMENT_IDENTIFIER_APPLICATIONPORT_MUTI);

			stream.write(mut_wdp, 0, mut_wdp.length);
			stream.write(totalByte, 0, totalByte.length);
			stream.write(countByte, 0, countByte.length);
			// write length of header
			headerStream.write((byte) stream.size());

			stream.writeTo(headerStream);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return headerStream.toByteArray();
	}

	public byte[] GetWSPHeaderBytes(byte contentLength) {
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		ByteArrayOutputStream headersStream = new ByteArrayOutputStream();
		try {
			stream.write(WSP.TRANSACTIONID_CONNECTIONLESSWSP);
			stream.write(WSP.PDUTYPE_PUSH);

			headersStream
					.write(
							WSP.HEADER_CONTENTTYPE_application_vnd_wap_sic_utf_8,
							0,
							WSP.HEADER_CONTENTTYPE_application_vnd_wap_sic_utf_8.length);// content
			// type:
			// application/vnd.wap.sic;;
			// charset=utf-8

			// 应该写入ContentLength
			headersStream.write(WSP.HEADER_CONTENTLENGTH); // ContentLength标志
			headersStream.write((byte) (contentLength + 128));

			// Push Flag属性
			headersStream.write(WSP.HEADER_PUSHFLAG, 0,
					WSP.HEADER_PUSHFLAG.length);

			stream.write((byte) headersStream.size());

			headersStream.writeTo(stream);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return stream.toByteArray();
	}

	public byte[] GetPDUBytes() {
		byte[] body = si.GetWBXMLBytes();
		byte[] headerBuffer = GetWSPHeaderBytes((byte) (body.length));

		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		stream.write(headerBuffer, 0, headerBuffer.length);
		stream.write(body, 0, body.length);

		return stream.toByteArray();
	}

	public static void main(String[] argv) {
		//String recipient = "13920160328";
		String href = "http://www.2163.com";
		String text = "A WAP Push to the 2163";
		SMSPushMessage singletMsg = new SMSPushMessage(href, text);
		MultiPushMsg multiMsg = new MultiPushMsg(href, text);
		//HexDecode decoder = new HexDecode();

		String singleBody = new String(HexEncode.byteArrayToHexChars(singletMsg.GetSMSBytes()));
		String multiBody = new String(HexEncode.byteArrayToHexChars(multiMsg.getSMSBytes()));
		
		System.out.println(singleBody);
		System.out.println(multiBody);
	}
}

⌨️ 快捷键说明

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