本文介绍如何使用Axis2传递附件。
author: ZJ
07-5-7
1.工作环境
IDE: Eclipse
3.1.2
jdk: jdk
1.5.0
_04
Tomcat: apache-tomcat-
5.0.28
AXIS2:1.0(war版本和bin版本)
2.实现
AXIS2的webservice发布的时候是打包成xxx.aar发布的,xxx.aar展开后的目录结构为
--
--meta-inf
services.xml
--包含server端实现的class( 目录跟package是一样的结构)
3.服务器端FileTransferServer.java
package sample;
import org.apache.axiom.attachments.utils.IoUtils;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMText;
import org.apache.axis2.AxisFault;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Iterator;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
public class FileTransferServer {
public static final String TMP_PATH = "D:/temp";
public OMElement upload(OMElement element) throws Exception {
OMElement _fileName = null;//文件名
OMElement _fileType = null;//文件类型
System.out.println("The element for upload: " + element);
.hasNext();) {
OMElement _ele = (OMElement) _iterator.next();
if (_ele.getLocalName().equalsIgnoreCase("fileContent")) {
_fileContent = _ele;
}
if (_ele.getLocalName().equalsIgnoreCase("fileName")) {
_fileName = _ele;
}
if (_ele.getLocalName().equalsIgnoreCase("fileType")) {
_fileType = _ele;
}
}
if (_fileContent == null || _fileType == null) {
throw new AxisFault("Either Image or FileName is null");
}
OMText binaryNode = (OMText) _fileContent.getFirstOMChild();
String fileName = _fileName.getText();
String fileType = _fileType.getText();
String storeDir = TMP_PATH + "/" + "tempTest";
File dir = new File(storeDir);
if (!dir.exists()) {
dir.mkdir();
}
String filePath = storeDir + "/" + fileName + "." + fileType;
File uploadFile = new File(filePath);
if (uploadFile.exists()) {
filePath = storeDir + "/" + fileName + "(1)" + "." + fileType;
uploadFile = new File(filePath);
}
// Extracting the data and saving
DataHandler actualDH;
actualDH = (DataHandler) binaryNode.getDataHandler();
FileOutputStream imageOutStream = new FileOutputStream(uploadFile);
InputStream is = actualDH.getInputStream();
imageOutStream.write(IoUtils.getStreamAsByteArray(is));
// setting response
OMFactory fac = OMAbstractFactory.getoMFactory();
OMNamespace ns = fac.createOMNamespace("http://example.org/filedata",
"fd");
OMElement ele = fac.createOMElement("response",ns);
ele.setText("true");
return ele;
}
public OMElement download(OMElement element) throws Exception {
System.out.println("The element for download: " + element);
OMElement _userName = null;
OMElement _fileName = null;
OMElement _fileType = null;
.hasNext();) {
OMElement _ele = (OMElement) _iterator.next();
if (_ele.getLocalName().equalsIgnoreCase("userName")) {
_userName = _ele;
}
if (_ele.getLocalName().equalsIgnoreCase("fileName")) {
_fileName = _ele;
}
if (_ele.getLocalName().equalsIgnoreCase("fileType")) {
_fileType = _ele;
}
}
String userName = _userName.getText();
String fileName = _fileName.getText();
String fileType = _fileType.getText();
String filePath = TMP_PATH + "/" + userName + "/" + fileName + "."
+ fileType;
System.out.println("The filePath for download: " + filePath);
FileDataSource dataSource = new FileDataSource(filePath);
DataHandler expectedDH = new DataHandler(dataSource);
OMFactory fac = OMAbstractFactory.getoMFactory();
OMNamespace ns = fac.createOMNamespace("http://example.org/filedata",
"fd");
OMText textData = fac.createOMText(expectedDH,true);
OMElement ele = fac.createOMElement("response",ns);
ele.addChild(textData);
return ele;
}
}
|
4.services.xml
<?xml version="1.0" encoding="UTF-8"?>
<service name="FileOperation">
<description>
This is a sample Web Service with two operations,echo and ping.
</description>
<operation name="upload">
<actionMapping>urn:upload</actionMapping>
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
<operation name="download">
<actionMapping>urn:download</actionMapping>
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
</service>
|
5.测试
FileTransferClient.java
package sample;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import org.apache.axiom.attachments.utils.IoUtils;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMText;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
public class FileTransferClient {
private static EndpointReference targetEPR =
new EndpointReference("http://127.0.0.1:8080/axis2/services/FileOperation");
public static boolean upload(String fileName,File file,String fileType) {
try {
OMElement data = buildUploadEnvelope(fileName,file,fileType);
Options options = buildOptions();
ServiceClient sender = new ServiceClient();
sender.setoptions(options);
System.out.println("The data in method upload: "+data);
OMElement ome = sender.sendReceive(data);
System.out.println("Convert the data to element in method upload: "+ome);
String b = ome.getText();
return Boolean.parseBoolean(b);
}
catch(Exception e) {
e.printstacktrace();
}
return false;
}
public static boolean download(String userName,String fileName,String fileType) {
try {
OMElement data = buildDownloadEnvelope(userName,fileName,fileType);
Options options = buildOptions();
ServiceClient sender = new ServiceClient();
sender.setoptions(options);
System.out.println("The data in method download: "+data);
OMElement ome = sender.sendReceive(data);
System.out.println("Convert the data to element in method download: "+ome);
OMText binaryNode = (OMText) ome.getFirstOMChild();
DataHandler actualDH = (DataHandler) binaryNode.getDataHandler();
FileOutputStream imageOutStream = new FileOutputStream("D:/userTemp/xx.gif");
InputStream is = actualDH.getInputStream();
imageOutStream.write(IoUtils.getStreamAsByteArray(is));
return true;
}
catch(Exception e) {
e.printstacktrace();
}
return false;
}
private static OMElement buildUploadEnvelope(String fileName,String fileType) {
DataHandler expectedDH;
OMFactory fac = OMAbstractFactory.getoMFactory();
OMNamespace omNs = fac.createOMNamespace("http://example.org/filedata","fd");
OMElement data = fac.createOMElement("upload",omNs);
OMElement fileContent = fac.createOMElement("fileContent",omNs);
FileDataSource dataSource = new FileDataSource(file);
expectedDH = new DataHandler(dataSource);
OMText textData = fac.createOMText(expectedDH,true);
fileContent.addChild(textData);
OMElement _fileName = fac.createOMElement("fileName",omNs);
_fileName.setText(fileName);
OMElement _fileType = fac.createOMElement("fileType",omNs);
_fileType.setText(fileType);
data.addChild(_fileName);
data.addChild(_fileType);
data.addChild(fileContent);
return data;
}
private static OMElement buildDownloadEnvelope(String userName,String fileType) {
OMFactory fac = OMAbstractFactory.getoMFactory();
OMNamespace omNs = fac.createOMNamespace("http://example.org/filedata","fd");
OMElement data = fac.createOMElement("download",omNs);
OMElement _userName = fac.createOMElement("userName",omNs);
_userName.setText(userName);
OMElement _fileName = fac.createOMElement("fileName",omNs);
_fileName.setText(fileName);
OMElement _fileType=fac.createOMElement("fileType",omNs);
_fileType.setText(fileType);
data.addChild(_userName);
data.addChild(_fileName);
data.addChild(_fileType);
return data;
}
private static Options buildOptions() throws AxisFault {
Options options = new Options();
options.setSoapVersionURI(SOAP11Constants.soAP_ENVELOPE_NAMESPACE_URI);
options.setTo(targetEPR);
// enabling MTOM in the client side
options.setProperty(Constants.Configuration.ENABLE_MTOM,Constants.VALUE_TRUE);
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
return options;
}
public static void main(String agrs[]) {
String file = "D:/userTemp/ya.gif";
String fn = "testUser";
String ft="gif";
boolean rtv = upload(fn,new File(file),ft);
System.out.println("is upload success: "+rtv);
String un="zj";
String downfn="1";
if(download(un,downfn,ft)){
System.out.println("download success.");
}
else System.out.println("download fail.");
System.out.println("Client main end.");
}
}
|
6.结果
察看soap消息,我们可以发现
<fd:upload xmlns:fd="http://example.org/filedata">
<fd:fileName>testUser</fd:fileName>
<fd:fileType>gif</fd:fileType>
</fd:upload>
Convert the data to element in method upload:
<fd:response xmlns:fd=http://example.org/filedata xmlns:tns="http://ws.apache.org/axis2">true</fd:response>
The data in method download:
<fd:download xmlns:fd="http://example.org/filedata">
<fd:userName>zj</fd:userName>
<fd:fileName>1</fd:fileName>
<fd:fileType>gif</fd:fileType>
</fd:download>
Convert the data to element in method download:
<fd:response xmlns:fd="http://example.org/filedata" xmlns:tns="http://ws.apache.org/axis2">
</fd:response>
|
利用Axis2的Mtom发送附件应用了builder模式。要向一个webserive 发送请求,首先就要构建一个请求的Envelope,Axis2构建Envelope的时候是利用的Axis2的AXIOM api(就是axis2的java object和xml的映射处理机制),其编程模式和DOM差不多的.看这一段:
private static OMElement buildUploadEnvelope(String mailBoxnum,short greetingType,String FileType) {
DataHandler expectedDH;
OMFactory fac = OMAbstractFactory.getoMFactory();
...
return data;
}
|
这一段其实是构建的data对像是这样一段xml的java object代表:
<fd:upload xmlns:fd="http://example.org/filedata">
<fd:fileName>testUser</fd:fileName>
<fd:fileType>gif</fd:fileType>
</fd:upload>
|
OMElement data = fac.createOMElement("upload",omNs);
|
<operationname="upload">
<actionMapping>urn:upload</actionMapping>
<messageReceiverclass="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
|
要一致的。
再看看这一段,
private static Options buildOptions() {
Options options = new Options();
...
return options;
}
|
这里构建的Options对象,顾名思义就是调用webservice的相应的选项:比如这里就指定了Soap协议为1.1 版,指定了所请求的service 的EPR(就是地址),声明在client应用MTOM指定传输协议为HTTP。
ServiceClient sender = new ServiceClient();
//设定选项
sender.setoptions(options);
//打印要传送的数据,为一段xml
System.out.println(data);
//传送数据,得到返回值
OMElement ome = sender.sendReceive(data);
//打印返回值,为一段xml
System.out.println(ome);
//析取返回值中的数据
String b = ome.getText();
//返回
return Boolean.parseBoolean(b);
|
可以发现,server端和client的中间传递数据都是通过 org.apache.axiom.om.OMElement对象的,这个对象是一段xml的java 对象映射
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。