工作环境:IDE: Eclipse 3.1.2
jdk: jdk1.5.0_06
Tomcat: apache-tomcat-5.5.15
AXIS2:1.0(war版本和bin版本)
环境准备:
http://ws.apache.org/axis2/download/1_0/download.cgi去下载AXIS2的
Binary Distribution
url: http://apache.justdn.org/ws/axis2/1_0/axis2-std-1.0-bin.zip
和
war
Distribution url: http://apache.justdn.org/ws/axis2/1_0/axis2-1.0-docs.zip
。把这两个文件解压,比如解压缩的后得目录为
C:/axis2-std-1.0-bin
和
C:/axis2.war.
在
Eclipse
下通过菜单
window—preferences…--Java—Build Path—User Libraries
新建一个
user library,
比如名字就叫
axis2
把
C:/axis2-std-1.0-bin/lib
下的所有
jar
文件包含进来。把
axis2.war
拷贝到
%TOMCAT-HOME%/webapps
下面。
实现:
在
Eclipse
新建一个工程,装了
Eclipse tomcat plugin
的就新建一个
tomcat project
好了,
build path
包含上面创建的
user library:axis2.
我的例子的场景是一个语音信箱系统的用户上传下载问候语文件(
greeting)
的,每个语音信箱系统的用户拥有一个唯一的
mailbox number,
问候语有不同的类型,比如忙的时候问候语,出差时候问候语,不同时段的问候语,问候语文件支持的类型有
wav,mp3
等等。
所以我的
webservice
要实现的
2
个功能就是
upload, download.
AXIS2
的
webservice
发布的时候是打包成
xxx.aar
发布的,
xxx.aar
展开后的目录结构为
--
--META-INF
services.xml
--
包含
server
端实现的
class(
目录跟
package
是一样的结构
)
新建
2
个类:
FileTransferClient.java, interopService.java.
两个类内容如下:
FileTransferClient(
调用
webservice
的客户端代码
)
package sample.mtom.interop.client;
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.axiom.soap.SOAP11Constants;
import org.apache.axiom.soap.SOAP12Constants;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import java.io.File;
import java.io.InputStream;
public class FileTransferClient {
private static EndpointReference targetEPR = new EndpointReference("http://127.0.0.1:8080/axis2/services/interop");
public static boolean upload(String mailboxnum, short greetingType, File file, String fileType) {
try {
OMElement data = buildUploadEnvelope(mailboxnum, greetingType, file, fileType);
Options options = buildOptions();
ServiceClient sender = new ServiceClient();
sender.setOptions(options);
System.out.println(data);
OMElement ome = sender.sendReceive(data);
System.out.println(ome);
String b = ome.getText();
return Boolean.parseBoolean(b);
}
catch(Exception e) {
}
return false;
}
public static InputStream download(String mailboxnum, short greetingType, String FileType) {
try {
OMElement data = buildDownloadEnvelope(mailboxnum, greetingType, FileType);
Options options = buildOptions();
ServiceClient sender = new ServiceClient();
sender.setOptions(options);
System.out.println(data);
OMElement ome = sender.sendReceive(data);
System.out.println(ome);
OMText binaryNode = (OMText) ome.getFirstOMChild();
DataHandler actualDH = (DataHandler) binaryNode.getDataHandler();
return actualDH.getInputStream();
}
catch(Exception e) {
}
return null;
}
private static OMElement buildUploadEnvelope(String mailboxnum, short greetingType, File file, String FileType) {
DataHandler expectedDH;
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace("http://example.org/mtom/data", "x");
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 mboxnum = fac.createOMElement("mailboxnum", omNs);
mboxnum.setText(mailboxnum);
OMElement gtType = fac.createOMElement("greetingType", omNs);
gtType.setText(greetingType+"");
OMElement fileType=fac.createOMElement("fileType", omNs);
fileType.setText(FileType);
data.addChild(mboxnum);
data.addChild(gtType);
data.addChild(fileType);
data.addChild(fileContent);
return data;