联通SGIP2.0收发短信遇到的问题!求救
考虑到用户手机不支持长短信问题。我自己将长短信按照每条70个汉字截取。比如短信内容为“若用户传递参数不合法则接口服务器端不记录相关信息,当且仅当服务器端处理数据发生异常时候记录错误日志传递的号码中只要有一个不合法就全部取消短信发送123456”我截取成两条后存入数据库。
但是将数据库中的这两条通过SMG发送至指定手机的时候,第二条出现的短信内容变成了“@@@@@@@”这个问题该怎么解决呢?是我截取的问题吗?
代码贴上:
private ResultEnum insertDb(String serviceType, String mobiles,
String content, int priority) {
int morelatetoMTFlag = MorelatetoMTFlagEnum.NormalFirst.getValue();
int messageCoding = MessageCodingEnum.UCS2.getValue();
// 判断短信长度
byte[] messageBytes = new byte[0];
int messageBytesLength = 0;
try {
messageBytes = content.getBytes("utf-16");
messageBytesLength = messageBytes.length;
log.info("短信字节长度:" + messageBytesLength);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
log.error(e);
return ResultEnum.ContentError;
}
int count = 0;
int tp_udhi = 0;
if (messageBytesLength > 140) // 分割长短信
{
tp_udhi = 1;
/*
* count=messageBytesLength/134; if((messageBytesLength%134)>0){
* count++; }
*/
if (messageBytesLength % 134 == 0) {
count = messageBytesLength / 134;
} else {
count = messageBytesLength / 134 + 1;
}
log.debug("messageBytesLength=" + messageBytesLength + ",count="
+ count);
Random random = new Random();
for (int i = 0; i < count; i++) {
byte[] bt = new byte[140];
/*int startindex=i*134,endindex=(i+1)*134-1;
if(count-1==i&&messageBytesLength%134!=0)
{
endindex=messageBytesLength - 1;
}*/
/*int startindex = i * 134, endindex = startindex + 134 - 1;
if (count - 1 == i && messageBytesLength % 134 != 0) {// 最后一个长度不够134
endindex = messageBytesLength - 1;
}*/
//byte[] bt=getByteContent(messageBytes,i,134,endindex);
bt[0] = 0x05;// 包头长度固定0x05
bt[1] = 0x00;// 类型标识固定0x00
bt[2] = 0x03;// 子包长度固定0x03
bt[3] = ByteUtil.IntToByte(random.nextInt(255));// 短信唯一标识 0-255
bt[4] = ByteUtil.IntToByte(count);// 长短信被分割的条数
bt[5] = ByteUtil.IntToByte(i + 1);// 本条短信在 长短信中的位置 序号 从1开始
int startindex = i * 134, endindex = startindex + 134 - 1;
if (count - 1 == i && messageBytesLength % 134 != 0) {// 最后一个长度不够134
endindex = messageBytesLength - 1;
}
log.debug("i=" + i + ",startindex=" + startindex + ",endindex="
+ endindex);
ByteUtil.BytesCopy(messageBytes, bt, startindex, endindex, 6);
String smsContent = "";
try {
smsContent = new String(bt, "utf-16");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.error("短信内容转换utf-16 Error Detail:" + e.getMessage());
return ResultEnum.ContentError;
}
// 插入数据库
submit instance = new submit();
instance.setServiceType(serviceType);
instance.setSpNumber(GetWayConfig.spNumber);
instance.setUserNumber(mobiles);
instance.setTP_udhi(tp_udhi);
instance.setMessageCoding(MessageCodingEnum.UCS2.getValue());
instance.setMessageContent(smsContent);
instance.setMorelatetoMTFlag(MorelatetoMTFlagEnum.NormalFirst
.getValue());
instance.setPriority(priority);
log.info("业务代码:" + serviceType);
log.info("电话号码:" + mobiles);
log.info("短信内容:" + smsContent);
log.info("增加立即短信至数据库 [ 开始 ]");
try {
DBSubmit.Insert(instance);
} catch (Exception ex) {
log.error("增加立即短信 Error Detail:" + ex.getMessage());
return ResultEnum.DbError;
}
log.info("增加立即短信至数据库 [ 结束 ]");
log.debug("smsContent=" + smsContent);
StringBuffer sb = new StringBuffer();
for (byte b : bt) {
sb.append(b);
}
log.debug(sb.toString());
}
} --------------------编程问答-------------------- 自己先顶下
补充:Java , Java相关