1、导入的类大致如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import org.apache.axiom.attachments.Attachments;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.OperationClient;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.axis2.context.MessageContext;
2、主要代码大致如下:
private static EndpointReference targetEPR = new EndpointReference("soap 的地址");
Options options = new Options();
options.setTo(targetEPR);
options.setProperty(Constants.Configuration.ENABLE_SWA, Constants.VALUE_TRUE);
options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
// Increase the time out when sending large attachments
options.setTimeOutInMilliSeconds(100000);
options.setTo(targetEPR);
//urn后面跟的是soap消息的名字
options.setAction("urn:importTreeFile");
ConfigurationContext configContext = ConfigurationContextFactory
.createConfigurationContextFromFileSystem(null, null);
ServiceClient sender = new ServiceClient(configContext, null);
sender.setOptions(options);
OperationClient mepClient = sender.createClient(ServiceClient.ANON_OUT_IN_OP);
//构建soap请求,并设置contentID,soap是通过contentID来找对应的文件的
ImportTreeFileRequestMsgDocument msg = ImportTreeFileRequestMsgDocument .Factory.newInstance()
ImportTreeFileRequestMsgDocument.setContentID("file");
DataHandler dh = null;
MessageContext mc = new MessageContext();
FileDataSource fds = new FileDataSource("文件名”);
try {
dh = new DataHandler(fds);
} catch (RuntimeException e) {
e.printStackTrace();
}
Attachments att = new Attachments();
att.addDataHandler("file", dh);
mc.setAttachmentMap(att);
SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
SOAPEnvelope env = fac.getDefaultEnvelope();
env.getBody().addChild(toOM(msg, false));
mc.setEnvelope(env);
mepClient.addMessageContext(mc);
mepClient.execute(true);
readAttachment(mc);
private org.apache.axiom.om.OMElement toOM(
com.huawei.www.ngin.routetree.ImportTreeFileRequestMsgDocument param, boolean optimizeContent) {
org.apache.axiom.om.impl.builder.StAXOMBuilder builder = new org.apache.axiom.om.impl.builder.StAXOMBuilder(
org.apache.axiom.om.OMAbstractFactory.getOMFactory(), new org.apache.axis2.util.StreamWrapper(param
.newXMLStreamReader()));
org.apache.axiom.om.OMElement documentElement = builder.getDocumentElement();
((org.apache.axiom.om.impl.OMNodeEx) documentElement).setParent(null);
return documentElement;
}
private void readAttachment(org.apache.axis2.context.MessageContext messageContext) {
String contentID = messageContext.getAttachmentMap().getAllContentIDs()[0];
DataHandler dh = messageContext.getAttachment(contentID);
dh.getDataSource().getName();
InputStream is = null;
try {
is = dh.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println("client read2:" + line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import org.apache.axiom.attachments.Attachments;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.OperationClient;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.axis2.context.MessageContext;
2、主要代码大致如下:
private static EndpointReference targetEPR = new EndpointReference("soap 的地址");
Options options = new Options();
options.setTo(targetEPR);
options.setProperty(Constants.Configuration.ENABLE_SWA, Constants.VALUE_TRUE);
options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
// Increase the time out when sending large attachments
options.setTimeOutInMilliSeconds(100000);
options.setTo(targetEPR);
//urn后面跟的是soap消息的名字
options.setAction("urn:importTreeFile");
ConfigurationContext configContext = ConfigurationContextFactory
.createConfigurationContextFromFileSystem(null, null);
ServiceClient sender = new ServiceClient(configContext, null);
sender.setOptions(options);
OperationClient mepClient = sender.createClient(ServiceClient.ANON_OUT_IN_OP);
//构建soap请求,并设置contentID,soap是通过contentID来找对应的文件的
ImportTreeFileRequestMsgDocument msg = ImportTreeFileRequestMsgDocument .Factory.newInstance()
ImportTreeFileRequestMsgDocument.setContentID("file");
DataHandler dh = null;
MessageContext mc = new MessageContext();
FileDataSource fds = new FileDataSource("文件名”);
try {
dh = new DataHandler(fds);
} catch (RuntimeException e) {
e.printStackTrace();
}
Attachments att = new Attachments();
att.addDataHandler("file", dh);
mc.setAttachmentMap(att);
SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
SOAPEnvelope env = fac.getDefaultEnvelope();
env.getBody().addChild(toOM(msg, false));
mc.setEnvelope(env);
mepClient.addMessageContext(mc);
mepClient.execute(true);
readAttachment(mc);
private org.apache.axiom.om.OMElement toOM(
com.huawei.www.ngin.routetree.ImportTreeFileRequestMsgDocument param, boolean optimizeContent) {
org.apache.axiom.om.impl.builder.StAXOMBuilder builder = new org.apache.axiom.om.impl.builder.StAXOMBuilder(
org.apache.axiom.om.OMAbstractFactory.getOMFactory(), new org.apache.axis2.util.StreamWrapper(param
.newXMLStreamReader()));
org.apache.axiom.om.OMElement documentElement = builder.getDocumentElement();
((org.apache.axiom.om.impl.OMNodeEx) documentElement).setParent(null);
return documentElement;
}
private void readAttachment(org.apache.axis2.context.MessageContext messageContext) {
String contentID = messageContext.getAttachmentMap().getAllContentIDs()[0];
DataHandler dh = messageContext.getAttachment(contentID);
dh.getDataSource().getName();
InputStream is = null;
try {
is = dh.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println("client read2:" + line);
}
} catch (IOException e) {
e.printStackTrace();
}
}