---
title: "Axis2 附件传输 样例 解读"
author: "dingchd"
date: "2013-04-20T12:02:59Z"
tags: []
categories: ["开源软件"]
slug: "axis2"
draft: false
lastmod: "2013-04-20T12:02:59Z"
id: 1850635
url: "https://dingchd.iteye.com/blog/1850635"
---

axis2 的客户端发送带附件的过程如下

```java

		//设置options
		Options options = new Options();
		options.setTo(targetEPR);
		options.setProperty(Constants.Configuration.ENABLE_SWA,
				Constants.VALUE_TRUE);
		options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);		
		options.setTimeOutInMilliSeconds(1000000);
		options.setTo(targetEPR);
		options.setAction("urn:uploadFile");

		//获取context
		String base = System.getenv("AXIS2_HOME");
		ConfigurationContext configContext = ConfigurationContextFactory
				.createConfigurationContextFromFileSystem(base+File.separator+"repository",
						null);

		//创建发送客户端
		ServiceClient sender = new ServiceClient(configContext, null);
		sender.setOptions(options);
		OperationClient mepClient = sender
				.createClient(ServiceClient.ANON_OUT_IN_OP);

		//创建attach的消息上下文，指定attachid
		MessageContext mc = new MessageContext();
		FileDataSource fileDataSource = new FileDataSource(file);		
		DataHandler dataHandler = new DataHandler(fileDataSource);
		String attachmentID = mc.addAttachment(dataHandler);//重点是这句

		//构建信封（xml消息），并将信封设置到消息上下文中
		SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
		SOAPEnvelope env = fac.getDefaultEnvelope();
		OMNamespace omNs = fac.createOMNamespace(
				"http://service.soapwithattachments.sample", "swa");
		OMElement uploadFile = fac.createOMElement("uploadFile", omNs);
		OMElement nameEle = fac.createOMElement("name", omNs);
		nameEle.setText(destinationFile);
		OMElement idEle = fac.createOMElement("attchmentID", omNs);
		idEle.setText(attachmentID);
		uploadFile.addChild(nameEle);
		uploadFile.addChild(idEle);
		env.getBody().addChild(uploadFile);
		mc.setEnvelope(env);

		
		//将消息上下文设置到客户端，发送！
		mepClient.addMessageContext(mc);
		mepClient.execute(true);



```

1）构建信封的过程即使构建xml数据结构
2）MessageContext 分为In 和Out 

服务端接受消息过程如下：

```java

       // 获取当前消息上下文
       MessageContext msgCtx = MessageContext.getCurrentMessageContext();
       // 提取附件id，并获取附件二进制流
       Attachments attachment = msgCtx.getAttachmentMap();
       DataHandler dataHandler = attachment.getDataHandler(attchmentID);
       File file = new File(name);
       FileOutputStream fileOutputStream = new FileOutputStream(file);
       // 业务代码，写文件
       dataHandler.writeTo(fileOutputStream);
       fileOutputStream.flush();
       fileOutputStream.close();

```


服务端发送response给客户端的消息也可以粘贴附件，就是所谓的axis2 下载文件功能
下面代码接上面代码

```java

 		OperationContext opCtx = msgCtx.getOperationContext();
		MessageContext outMsgCtx = opCtx.getMessageContext(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
		
		DataHandler out = new DataHandler(new FileDataSource(new File("c:\\1.txt")));
		String downloadid = outMsgCtx.addAttachment(out);
		return downloadid;

```


客户端拿到响应的message时，就可以根据downloadid找到附件的文件流

其他设置可以参考:http://axis.apache.org/axis2/java/core/docs/mtom-guide.html