`
fangzhu19880123
  • 浏览: 30032 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

ISAG webservice接口发送彩信附件问题

阅读更多
工作需要,使用电信提供的webservice接口发送彩信,调了几天,终于可以把彩信发送出去了,代码如下:
package com.onewaveinc.message;

import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.util.ByteArrayDataSource;

import org.apache.axis.attachments.AttachmentPart;
import org.apache.axis.message.SOAPHeaderElement;
import org.apache.axis.types.URI;

import com.onewaveinc.pnp.mms.ChargingInformation;
import com.onewaveinc.pnp.mms.DeliveryInformation;
import com.onewaveinc.pnp.mms.MessagePriority;
import com.onewaveinc.pnp.mms.SendMessageBindingStub;
import com.onewaveinc.pnp.mms.SendMessageServiceLocator;
import com.onewaveinc.pnp.mms.SimpleReference;
import com.onewaveinc.properties.PropertiesManage;
import com.onewaveinc.utils.MD5;

public class Message {
	
	public static void main(String[] args) {
		SendMessageServiceLocator loc = new SendMessageServiceLocator();
		String url = PropertiesManage.getValue("url");
		System.out.println("接口地址: " + url);
		loc.setSendMessageEndpointAddress(url);
		try {
			SendMessageBindingStub binding = (SendMessageBindingStub) loc.getSendMessage();
			binding.setTimeout(60000);
			
			//添加头信息
			String spId = PropertiesManage.getValue("message.header.spId");
			String spPassword = PropertiesManage.getValue("message.header.spPassword");
			SimpleDateFormat formatter = new SimpleDateFormat("MMddhhmmss");
			String timeStamp = formatter.format(Calendar.getInstance().getTime());
			//密码加密
			spPassword = MD5.compile(spId + spPassword + timeStamp);
			System.out.println("密码加密后:" + spPassword);
			String productId = PropertiesManage.getValue("message.header.productId");
			String SAN = PropertiesManage.getValue("message.header.SAN");
			String transactionId = PropertiesManage.getValue("message.header.transactionId");
			String transEnd = PropertiesManage.getValue("message.header.transEnd");
			String linkId = PropertiesManage.getValue("message.header.linkId");
			String OA = PropertiesManage.getValue("message.header.OA");
			String FA = PropertiesManage.getValue("message.header.FA");
			String multicastMessaging = PropertiesManage.getValue("message.header.multicastMessaging");
			
			SOAPHeaderElement header = new SOAPHeaderElement("http://www.chinatelecom.com.cn/schema/ctcc/common/v2_1", "RequestSOAPHeader");
			header.addChildElement("spId").addTextNode(spId);
			header.addChildElement("spPassword").addTextNode(spPassword);
			header.addChildElement("timeStamp").addTextNode(timeStamp);
			header.addChildElement("productId").addTextNode(productId);
			header.addChildElement("SAN").addTextNode(SAN);
			header.addChildElement("transactionId").addTextNode(transactionId);
			header.addChildElement("transEnd").addTextNode(transEnd);
			header.addChildElement("linkId").addTextNode(linkId); // linkID
			header.addChildElement("OA").addTextNode(OA);
			header.addChildElement("FA").addTextNode(FA);
			header.addChildElement("multicastMessaging").addTextNode(multicastMessaging);
			
			binding.setHeader(header);
			
			//短信接收者
			URI[] uris = new URI[args.length];
			for (int i = 0; i < args.length; i++) {
				uris[i] = new URI(args[i]);
				System.out.println("消息接收号码: " + args[i]);
			}
			
			
			//短信发送者
			String sender = PropertiesManage.getValue("message.sender");
			
			//短信主题
			String sub = PropertiesManage.getValue("message.subject");
			System.out.println("消息内容: " + sub);
			
			//计费信息
			ChargingInformation info = new ChargingInformation();
			info.setDescription(PropertiesManage.getValue("charging.description"));
			info.setAmount(new BigDecimal(Integer.valueOf(PropertiesManage.getValue("charging.amount"))));
			info.setCode(PropertiesManage.getValue("charging.code"));
			info.setCurrency(PropertiesManage.getValue("charging.currency"));
			
			//状态报告接口
			SimpleReference request = null;
			if ("on".equals(PropertiesManage.getValue("reference.need"))) {
				request = new SimpleReference();
				request.setCorrelator(PropertiesManage.getValue("reference.correlator"));
				request.setInterfaceName(PropertiesManage.getValue("reference.interfaceName"));
				request.setEndpoint(new URI(PropertiesManage.getValue("reference.endpoint")));
			}
			
			//彩信内容
			if ("on".equals(PropertiesManage.getValue("message.need"))) {
				String fileName = PropertiesManage.getValue("message.content");
				String[] fileNames = fileName.split(";");
				for (String name : fileNames) {
					System.out.println("彩信内容: " + name);
					File file = new File(name);
					
					DataHandler dh = new DataHandler(new FileDataSource(file));
					
					AttachmentPart ap= new AttachmentPart(dh);
					ap.setContentId(file.getName());
					ap.setContentLocation(file.getName());
					if (file.getName().indexOf("smil") > 0) {
						ap.setContentType("application/smil");
					} else if (file.getName().endsWith("txt")) {
						ap.setContentType("text/plain;charset=utf-8");
					} else if (file.getName().endsWith("amr")) {
						ap.setContentType("audio/amr");
					} else if (file.getName().endsWith("mp3")) {
						ap.setContentType("audio/mpeg");
					} else {
						ap.setContentType(ap.getContentType());
					}
					binding.addAttachment(ap);
				}
			}
			
			System.out.println("消息发送中...");
			String value = binding.sendMessage(uris, sender, sub, MessagePriority.Default, info, request);
			System.out.println("返回的彩信状态查询标识符为:" + value);

		} catch (Exception e) {
        	e.printStackTrace();
		}
	}
	
}


但是悲哀的发现这段代码运行起来可以发送图片和文字等信息并且能显示,但是音频文件如amr什么的却只能显示为附件不能直接播放,以为是contenttype的问题,所以添加了设置contenttype的代码ap.setContentType(...);但还是不能解决问题...

测试发现下发的彩信文件名称会少掉第一个字符和最后一个字符,导致amr文件成了am文件,以至于手机播放器不能识别,故将contentid前后各加一字符,问题解决,但至今未知原因
分享到:
评论
3 楼 Lance0512 2014-05-16  
我的邮箱是 :521881857@qq.com 
2 楼 Lance0512 2014-05-16  
你好 ,我现在也在做电信彩信发送的功能,您这里能提供一下 wsdl文件吗,或者给我一个生成的客户端,你这个代码不能用
1 楼 Callo_xu 2014-01-09  
你好~本人也因工作问题正在搞彩信这块~彩信是能发,但彩信展示效果没有按smil文件定义的展示~请问什么原因呢?本人QQ:502268897,请加我帮我解决一下~谢谢~

相关推荐

Global site tag (gtag.js) - Google Analytics