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前后各加一字符,问题解决,但至今未知原因

你可能感兴趣的:(webservice,附件,彩信)