电信的彩信发送axis2-附件

在处理一个这样的问题,如:一些基本的信息,姓名,年龄,电话, 加一些附件文件,可以是 doc, jpg 等,内容不限制,原来的 client 用 axis 生产,在发送附件的时候用了下列指令,能将附件和基本信息发送给 server (web-service),代码如下:
String fileName1 = new String("/local/data/g.txt");
File file1 = new File(fileName1);
javax.activation.DataHandler attachmentFile1 = new DataHandler(new
FileDataSource(file1));
_call.addAttachmentPart(attachmentFile1);
//_call 是 axis 的 org.apache.axis.client.Call 类的对象。


现在用 axis2 v1.5 来生产 client/stub , 参考例子:soapwithattachments , 发送, 代码如下:

File f=new File(p.getFileName());
DataHandler h=new DataHandler(new FileDataSource(f));
_messageContext.addAttachment(h);

这样的方法,结果,server 没有收到附件,一定是这样处理有问题?水能告诉如何处理才能发送附件呢?

soapwithattachments 例子,好像只能传输文件,不能包含文件以为的信息。

问题补充:
再一次比较了程序,终于知道原因了。

在 stub 中加下面代码:
_operationClient.getOptions().setProperty(org.apache.axis2.Constants.Configuration.ENABLE_SWA, org.apache.axis2.Constants.VALUE_TRUE);

详细代码如下:

/**
* 建立一个 SendMessage 对象
* @param task
* @return
*/
private SendMessage createMessage(Task task,MmsPartSet mms) throws Exception {

SendMessage m=new SendMessage();
URI[] add=new org.apache.axis2.databinding.types.URI[1];
String to=task.getToPhn();
String pre=config.get("add_86");
if(pre==null) pre="";
to=pre+to;
add[0]=new org.apache.axis2.databinding.types.URI(to);
String srcPhn=task.getSrcPhn();
ChargingInformation fee = new ChargingInformation();
fee.setAmount(new java.math.BigDecimal(task.getFeeCode()));
fee.setDescription("feeType="+task.getFeeType());
fee.setCode("0");
fee.setCurrency("rmb");

SimpleReference rr = new SimpleReference();
String ae=config.get("add_endpoint");
if(ae==null) ae="";
rr.setEndpoint(new org.apache.axis2.databinding.types.URI(ae+to));
rr.setInterfaceName("33");
String msgid=task.getTaskId();;
while(msgid.length()<11){
msgid="0"+msgid;
}
msgid=task.getTaskType()+msgid;

rr.setCorrelator(Tools.getTimeStamp()+msgid.substring(msgid.length()-12));

m.setAddresses(add);
m.setSenderAddress(srcPhn);
m.setPriority(MessagePriority.Normal);
m.setCharging(fee);
m.setSubject(Tools.toUtf(mms.getTitle())); //要以UTF-8格式的字符串
m.setReceiptRequest(rr);

return m;
}

/**
* 建立一个 RequestSOAPHeader 对象
* @param task
* @return
*/
private RequestSOAPHeader createHeader(Task task) throws Exception {
RequestSOAPHeader h=new RequestSOAPHeader();
h.setTransactionId(task.getTaskType()+ task.getTaskId());
String tmp=Tools.getTimeStamp();

String spid=config.get("spid_"+task.getGwid());;
String pwd=config.get("spwd_"+task.getGwid());
String temp = spid + pwd + tmp;
String md5=Tools.md5(temp);

h.setTimeStamp(tmp);
h.setSpId(spid);

h.setSpPassword(md5);
String dst=config.get("fa_"+task.getGwid());
if(dst==null) dst="";
dst=dst+task.getToPhn();

URI fa=new org.apache.axis2.databinding.types.URI(dst);
h.setFA(fa);
URI oa=new org.apache.axis2.databinding.types.URI(dst);
h.setOA(oa);
h.setLinkId(task.getLinkid());
h.setProductId(task.getSrvCode());
h.setMulticastMessaging(false);
h.setSAN(config.get("san_"+task.getGwid()));
h.setTransEnd(new EndReason("0",true));

return h;
}

/**
* _messageContext.addAttachment(null);//dataHandler);//加附件
* @param ctx
* @param mms
*/
private void addMms(MessageContext ctx,org.apache.axiom.soap.SOAPEnvelope env,MmsPartSet mms) throws Exception {
DataHandler h;
MmsPart p;
SOAPFactory fac = OMAbstractFactory.getSOAP12Factory();
OMNamespace omNs = fac.createOMNamespace("http://service.soapwithattachments.sample", "swa");
String fileName="";

for(int i=0;i<mms.getCount();i++){
p=mms.getPart(i);
fileName="a"+i+"."+p.getNameExt();
h=null;
if(p.getType()==MmsPart.TEXT){
String msg=Tools.readFromFile(new File(p.getFileName()));
msg=Tools.toUtf(msg);
byte []b=msg.getBytes("UTF-8");
ByteArrayDataSource bs=new ByteArrayDataSource();
bs.setInputStream(new ByteArrayInputStream(b));
bs.setName(p.getFileName());
bs.setContentType("text/plain");
h=new DataHandler(bs);
}else{
File f=new File(p.getFileName());
if(f.exists()){
h=new DataHandler(new FileDataSource(f));
}
}
if(h!=null){
ctx.addAttachment(h);
}
}
}

/**
* Auto generated method signature - 同步方式发送彩信
* @see dx.client.mt.SendMessageService#sendMessage
* @param sendMessage0
* @throws dx.client.mt.PolicyException
* @throws dx.client.mt.ServiceException , ADBException
*/

public MmsSendResult sendMessage(Task task,MmsPartSet mms) throws Exception {

MessageContext _messageContext = null;

try {
OperationClient _operationClient = _serviceClient.createClient(_operations[0].getName());

//附件以真实内容方法传递,否则不会传到远程去的
_operationClient.getOptions().setProperty(org.apache.axis2.Constants.Configuration.ENABLE_SWA, org.apache.axis2.Constants.VALUE_TRUE);

_operationClient.getOptions().setAction(
"http://www.chinatelecom.com.cn/wsdl/ctcc/multimedia_messaging/send/v2_2/interface/SendMessage/sendMessageRequest");
_operationClient.getOptions().setExceptionToBeThrownOnSOAPFault(true);

addPropertyToOperationClient(
_operationClient,
org.apache.axis2.description.WSDL2Constants.ATTR_WHTTP_QUERY_PARAMETER_SEPARATOR,
"&");

// create a message context
_messageContext = new org.apache.axis2.context.MessageContext();

SendMessageE me=new SendMessageE();
SendMessage m=createMessage(task,mms);
me.setSendMessage(m);

org.apache.axiom.soap.SOAPEnvelope env = null; // create SOAP envelope with that payload
env = toEnvelope(
getFactory(_operationClient.getOptions().getSoapVersionURI()),
me,
optimizeContent(new javax.xml.namespace.QName(
"http://www.chinatelecom.com.cn/wsdl/ctcc/multimedia_messaging/send/v2_2/interface",
"sendMessage")));
// 设置SOAP head

RequestSOAPHeader h = createHeader(task);//new RequestSOAPHeader();
RequestSOAPHeaderE he = new RequestSOAPHeaderE();
he.setRequestSOAPHeader(h);
OMFactory fac = OMAbstractFactory.getOMFactory();
QName qn=new QName("RequestSOAPHeader","http://www.chinatelecom.com.cn/schema/ctcc/common/v2_1");

OMElement oe=he.getOMElement(qn, fac);//将头添加给SOAP

addMms(_messageContext,env,mms); //加附件

_messageContext.setEnvelope(env);
_serviceClient.addHeader(oe); //将头添加给SOAP
_serviceClient.addHeadersToEnvelope(env);//给Envelope设置 Soap-Head

_operationClient.addMessageContext(_messageContext); // add the message contxt to client
_operationClient.execute(true); // 执行-向 server发送消息

org.apache.axis2.context.MessageContext _returnMessageContext = _operationClient
.getMessageContext(org.apache.axis2.wsdl.WSDLConstants.MESSAGE_LABEL_IN_VALUE);
org.apache.axiom.soap.SOAPEnvelope _returnEnv = _returnMessageContext.getEnvelope();

java.lang.Object object = fromOM(
_returnEnv.getBody().getFirstElement(),
dx.client.mt.SendMessageServiceStub.SendMessageResponseE.class,
getEnvelopeNamespaces(_returnEnv));

SendMessageResponseE re=(SendMessageResponseE)object;
SendMessageResponse or=re.getSendMessageResponse();

MmsSendResult res=new MmsSendResult();
res.setStatusCode(or.getResult());
res.setStatusMsg(or.getResult());
res.setToPhn(task.getToPhn());
String msgid=m.getReceiptRequest().getCorrelator();
res.setMsgid(msgid);
res.setTransid(h.getTransactionId());

return res;
} catch (org.apache.axis2.AxisFault f) {

org.apache.axiom.om.OMElement faultElt = f.getDetail();
if (faultElt != null) {
if (faultExceptionNameMap.containsKey(faultElt.getQName())) {
// make the fault by reflection
try {
java.lang.String exceptionClassName = (java.lang.String) faultExceptionClassNameMap
.get(faultElt.getQName());
java.lang.Class exceptionClass = java.lang.Class
.forName(exceptionClassName);
java.lang.Exception ex = (java.lang.Exception) exceptionClass
.newInstance();
// message class
java.lang.String messageClassName = (java.lang.String) faultMessageMap
.get(faultElt.getQName());
java.lang.Class messageClass = java.lang.Class
.forName(messageClassName);
java.lang.Object messageObject = fromOM(faultElt,
messageClass, null);
java.lang.reflect.Method m = exceptionClass.getMethod(
"setFaultMessage",
new java.lang.Class[] { messageClass });
m.invoke(ex, new java.lang.Object[] { messageObject });

if (ex instanceof dx.client.mt.PolicyException) {
throw (dx.client.mt.PolicyException) ex;
}

if (ex instanceof dx.client.mt.ServiceException) {
throw (dx.client.mt.ServiceException) ex;
}

throw new java.rmi.RemoteException(ex.getMessage(), ex);
} catch (java.lang.ClassCastException e) {
// we cannot intantiate the class - throw the original
// Axis fault
throw f;
} catch (java.lang.ClassNotFoundException e) {
// we cannot intantiate the class - throw the original
// Axis fault
throw f;
} catch (java.lang.NoSuchMethodException e) {
// we cannot intantiate the class - throw the original
// Axis fault
throw f;
} catch (java.lang.reflect.InvocationTargetException e) {
// we cannot intantiate the class - throw the original
// Axis fault
throw f;
} catch (java.lang.IllegalAccessException e) {
// we cannot intantiate the class - throw the original
// Axis fault
throw f;
} catch (java.lang.InstantiationException e) {
// we cannot intantiate the class - throw the original
// Axis fault
throw f;
}
} else {
throw f;
}
} else {
throw f;
}
} finally {
_messageContext.getTransportOut().getSender().cleanup(
_messageContext);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值