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

📄 mmsnotify.java

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

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;

public class MMSNotify {
	private static byte[] singleWapPushHeader = { (byte) 0x06, (byte) 0x05,
			(byte) 0x04, (byte) 0x0B, (byte) 0x84, (byte) 0x23, (byte) 0xF0 };

	private static byte[] mmsHeader = { (byte) 0xE9, (byte) 0x06, (byte) 0x22,
			(byte) 0x61, (byte) 0x70, (byte) 0x70, (byte) 0x6C, (byte) 0x69,
			(byte) 0x63, (byte) 0x61, (byte) 0x74, (byte) 0x69, (byte) 0x6F,
			(byte) 0x6E, (byte) 0x2F, (byte) 0x76, (byte) 0x6E, (byte) 0x64,
			(byte) 0x2E, (byte) 0x77, (byte) 0x61, (byte) 0x70, (byte) 0x2E,
			(byte) 0x6D, (byte) 0x6D, (byte) 0x73, (byte) 0x2D, (byte) 0x6D,
			(byte) 0x65, (byte) 0x73, (byte) 0x73, (byte) 0x61, (byte) 0x67,
			(byte) 0x65, (byte) 0x00, (byte) 0xAF, (byte) 0x84 };

	private static byte[] mms_Message_Type = { (byte) 0x8C, (byte) 0x82 };

	/*
	 * private static byte[] mms_Transaction_Id = { (byte) 0x98, (byte) 0x30,
	 * (byte) 0x30, (byte) 0x30, (byte) 0x30, (byte) 0x30, (byte) 0x30, (byte)
	 * 0x30, (byte) 0x30, (byte) 0x00 };// 0x98+8个Byte的TransactionID+0x00结尾
	 */
	private static byte[] mms_Version = { (byte) 0x8D, (byte) 0x90 };// version1.0

	private static byte[] mms_Message_Class = { (byte) 0x8A, (byte) 0x80 };// 80=personal,81=ad,82=info,83=auto

	private static byte[] mms_Expiry = { (byte) 0x88, (byte) 0x05, (byte) 0x81,
			(byte) 0x03, (byte) 0x09, (byte) 0x3A, (byte) 0x80 };

	public MMSNotify() {
		super();
	}

	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;
	}

	private byte[] getTransactIDPDU(String transId)
			throws UnsupportedEncodingException {
		// 0x98+8个Byte的TransactionID+0x00结尾
		byte[] result = new byte[10];
		result[0] = (byte) 0x98;
		result[9] = (byte) 0x00;
		// 如果没有设置Trans则填充8个字节的字符0;
		if (transId == null || transId.length() == 0) {
			for (int i = 1; i < 9; ++i) {
				result[i] = (byte) 0x30;
			}
			return result;
		}

		byte[] transByte = transId.getBytes("UTF-8");
		int len = Math.min(transByte.length, 8);

		for (int i = 1; i <= len; ++i) {
			result[i] = transByte[i - 1];
		}
		// 如果不足8个字节,后面补字符0,但是要以0x00结尾;
		if (len < 8) {
			for (int j = len + 1; j <= 8; ++j) {
				result[j] = (byte) 0x30;
			}
		}
		return result;
	}

	public byte[] getSubjectPDU(String subject)
			throws UnsupportedEncodingException {

		int len = subject.getBytes("UTF-8").length;
		byte[] result = new byte[4 + len];
		result[0] = (byte) 0x96;
		result[1] = (byte) (len + 2);
		result[2] = (byte) 0xEA;

		System.arraycopy(subject.getBytes("UTF-8"), 0, result, 3, len);

		result[4 + len - 1] = (byte) 0x00;

		return result;
	}

	private byte[] getMMSMessgeSizePDU(int size) {

		byte[] result = { (byte) 0x8E, (byte) 0x02, (byte) 0x0B, (byte) 0x05 };// new
		// byte[4];

		/*
		 * byte[] result = new byte[4];
		 * 
		 * result[0] = (byte) (((size >> 24) & 0x000000FF)); result[1] = (byte)
		 * (((size >> 16) & 0x000000FF)); result[2] = (byte) (((size >> 8) &
		 * 0x000000FF)); result[3] = (byte) (((size) & 0x000000FF));
		 */

		return result;
	}

	private byte[] getMMSExpirePDU() {
		return mms_Expiry;
	}

	private byte[] getUrlPDU(String url) throws UnsupportedEncodingException {
		int len = url.getBytes("UTF-8").length;
		byte[] result = new byte[2 + len];

		result[0] = (byte) 0x83;
		System.arraycopy(url.getBytes("UTF-8"), 0, result, 1, len);
		result[len + 1] = (byte) 0x00;

		return result;
	}

	public byte[] getSmsPDU() throws UnsupportedEncodingException {
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		stream.write(singleWapPushHeader, 0, singleWapPushHeader.length);
		stream.write(mmsHeader, 0, mmsHeader.length);
		byte[] body = generateBody();
		stream.write(body, 0, body.length);

		return stream.toByteArray();
	}

	private byte[] generateBody() throws UnsupportedEncodingException {
		ByteArrayOutputStream result = new ByteArrayOutputStream();
		result.write(mms_Message_Type, 0, mms_Message_Type.length);
		String transid = "11111111";
		result.write(getTransactIDPDU(transid), 0,
				getTransactIDPDU(transid).length);
		result.write(mms_Version, 0, mms_Version.length);
		String subject = "你好";
		result.write(this.getSubjectPDU(subject), 0, this
				.getSubjectPDU(subject).length);
		result.write(mms_Message_Class, 0, mms_Message_Class.length);
		int size = 1024;
		result.write(this.getMMSMessgeSizePDU(size), 0, this
				.getMMSMessgeSizePDU(size).length);
		result.write(mms_Expiry, 0, mms_Expiry.length);
		// String url = "http://211.137.168.174/ETong/a.MMS";
		String url = "http://211.137.168.174/ETong/c.MMS";
		// String url = "http://211.137.168.174/ETong/Sample.MMS";
		// String url = "http://wap.2163.com";
		result.write(this.getUrlPDU(url), 0, this.getUrlPDU(url).length);

		return result.toByteArray();
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		MMSNotify mms = new MMSNotify();

		String mb = new String(HexEncode.byteArrayToHexChars(mms.getSmsPDU()));
		// String mb = new
		// String(HexEncode.byteArrayToHexChars(mms.getTransactIDPDU("11111111")));
		System.out.println(mb);

		String s = "中国人";
		String s1 = "My first";
		String s2 = "几个";
		/*
		 * System.out.println(new
		 * String(HexEncode.byteArrayToHexChars(s.getBytes("UTF-8"))));
		 * System.out.println(new
		 * String(HexEncode.byteArrayToHexChars(s.getBytes("GB2312"))));
		 * System.out.println(new
		 * String(HexEncode.byteArrayToHexChars(s2.getBytes("UTF-8"))));
		 * System.out.println(new
		 * String(HexEncode.byteArrayToHexChars(s2.getBytes("GB2312"))));
		 * System.out.println(new
		 * String(HexEncode.byteArrayToHexChars(s1.getBytes("UTF-8"))));
		 * System.out.println(new
		 * String(HexEncode.byteArrayToHexChars(s1.getBytes("GB2312"))));
		 */
		System.out
				.println("  "
						+ new String(HexEncode.byteArrayToHexChars(s
								.getBytes("UTF-8"))));

		System.out.println(new String(HexEncode.byteArrayToHexChars(mms
				.getSubjectPDU(s))));

	}
}

⌨️ 快捷键说明

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