本文介绍如何使用Axis2传递附件。
IDE: Eclipse 3.1.2
jdk: jdk1.5.0_04
Tomcat: apache-tomcat-5.0.28
AXIS2:1.0(war版本和bin版本)
2.实现
在Eclipse新建一个动态web工程,在WEB-INF\lib下加入axis2所需的jar包。
本例的是一个系统的用户上传下载图片格式文件的例子,每次上传出携带附件外,还包括文件名, 文件类型。此webservice实现的2个功能就是upload, download.
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 _fileContent = null;//文件内容
OMElement _fileName = null;//文件名
OMElement _fileType = null;//文件类型
System.out.println("The element for upload: " + element);
for (Iterator _iterator = element.getChildElements(); _iterator
.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;
for (Iterator _iterator = element.getChildElements(); _iterator
.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>
<parameter name="ServiceClass" locked="false">sample.FileTransferServer</parameter>
<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>
|
将这两个文件打包并部署到Tomcat上(略)。