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

📄 longsmstest.java

📁 短信发送的程序。JAVA编写
💻 JAVA
字号:
package com.bci.cmpp.example;

import java.io.IOException;
import java.net.SocketTimeoutException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.PropertyConfigurator;

import com.bci.cmpp.AlreadyBoundException;
import com.bci.cmpp.BadCommandIDException;
import com.bci.cmpp.CMPPConstants;
import com.bci.cmpp.Connection;
import com.bci.cmpp.UnsupportedOperationException;
import com.bci.cmpp.message.CMPPActiveTest;
import com.bci.cmpp.message.CMPPActiveTestResp;
import com.bci.cmpp.message.CMPPProtocolException;
import com.bci.cmpp.message.CMPPSubmit;
import com.bci.cmpp.message.CMPPSubmitResp;
import com.bci.cmpp.util.*;

public class LongSMSTest extends CMPPAPIExample {

	private Log logger = LogFactory.getLog(LongSMSTest.class);

	private static int refence = 1;

	public LongSMSTest() {
	}

	public int execute(String mobile, String content) {
		int result = -1;
		try {
			int refences = getRefence();
			// LongSMSTest st = new LongSMSTest();
			// try {
			// this.configure("config/javacmppc2008.ini");
			// st.execute();
			// }

			// catch (ConfigurationException e) {
			// e.printStackTrace();
			// }

			// 发送短信 使用同步模式
			while (!isConnected())
				reConnect(Connection.TRANSMITTER, false);
			// Submit a simple message
			CMPPSubmit sm = (CMPPSubmit) myConnection
					.newInstance(CMPPConstants.CMPP_SUBMIT);
			// sm.setSequenceNum(0);
			sm.setSpCode(this.spCode);
			sm.setServiceId(this.serviceId);
			sm.setSourceAddr(this.sourceAddr);
			sm.setFeeTerminalId(getProperty("feeTerm"));
			sm.setDestTerminalId(mobile);
			sm.setFeeUserType(3);
			sm.setRegistered(1);
			sm.setFeeType(getProperty("feeType"));
			sm.setFeeCode(getProperty("feeCode"));
			sm.setTpUdhi(1);
			// sm.setMsgCoding(15);
			// String content = content;
			// int bytelen = content.getBytes().length;
			int chlen = content.length();
			int count = chlen / 67 + 1;
			logger.info("count==" + count);
			for (int i = 1; i <= count; i++) {
				int startpos = (i - 1) * 67;
				int endpos = startpos + 67;
				endpos = endpos > chlen ? chlen : endpos;
				String subcontent = content.substring(startpos, endpos);
				// logger.info("subcontent=" + subcontent);
				byte[] subMsg = UCS2Encoding.getInstance().encodeString(
						subcontent);
				byte[] msg = new byte[6 + subMsg.length];
				msg[0] = 0x05; // udhl
				msg[1] = 0x00; // IE
				msg[2] = 0x03; // IEL
				msg[3] = (byte) refences;// 0x01; // reference number
				msg[4] = (byte) count; // total number of
				// sms
				msg[5] = (byte) i; // 1/2 sms
				System.arraycopy(subMsg, 0, msg, 6, subMsg.length);
				sm.setMessage(msg);
				sm.setMsgCoding(0x18);
				CMPPSubmitResp smr = (CMPPSubmitResp) myConnection
						.sendRequest(sm);

				if (smr.getCommandStatus() == 0)
					logger.info("下发长短信.成功:(" + i + "/" + count + ") MOBILE="
							+ mobile + " 内容=" + subcontent + "  发送返回状态:"
							+ smr.getMsgIdStr());
				else
					logger.info("下发长短信.失败: (" + i + "/" + count + ")MOBILE="
							+ mobile + " 内容=" + subcontent + "  发送返回状态:"
							+ smr.getCommandStatus());
				result = smr.getCommandStatus();
			}

		} catch (Exception x) {
			logger.info("An exception occurred.");
			x.printStackTrace(System.err);
		} finally {
			try {
				if (myConnection != null) {
					myConnection.unbind();
					myConnection.closeLink();
				}
			} catch (Exception x) {
			}
		}
		// while (true) {
		// while (!isConnected())
		// reConnect(Connection.TRANSMITTER, false);
		// // 每30秒发一次ActiveTest
		// try {
		// Thread.sleep(1000 * 30);
		// CMPPActiveTest at = (CMPPActiveTest) myConnection
		// .newInstance(CMPPConstants.CMPP_ACTIVE_TEST);
		// CMPPActiveTestResp atr = (CMPPActiveTestResp) myConnection
		// .sendRequest(at);
		// if (atr != null && atr.getCommandStatus() == 0)
		// logger.info("active test seq:" + atr.getSequenceNum());
		// }
		// catch (BadCommandIDException e) {
		// e.printStackTrace();
		// disConnect();
		// }
		// catch (SocketTimeoutException e) {
		// e.printStackTrace();
		// disConnect();
		// }
		// catch (AlreadyBoundException e) {
		// e.printStackTrace();
		// disConnect();
		// }
		// catch (CMPPProtocolException e) {
		// e.printStackTrace();
		// disConnect();
		// }
		// catch (UnsupportedOperationException e) {
		// e.printStackTrace();
		// disConnect();
		// }
		// catch (IOException e) {
		// e.printStackTrace();
		// disConnect();
		// }
		// catch (InterruptedException e) {
		// e.printStackTrace();
		// disConnect();
		// }
		// }
		return result;
	}

	private static synchronized int getRefence() {
		if (refence > 126) {
			refence = 1;
		} else {
			refence++;
		}
		return refence;
	}

	public static void main(String[] arg) {
		PropertyConfigurator.configure("config/log4j.properties");
		LongSMSTest st = new LongSMSTest();
		try {
			st.configure("config/mtTest.properties");
			st.execute(arg[0], arg[1]);
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
	}

}

⌨️ 快捷键说明

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