本文介绍如何使用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上(略)。
相关推荐
axis2-idea-plugin-1.7.9
1、axis2相关jar包如下: axiom-api-1.2.10.jar axiom-dom-1.2.10.jar axiom-impl-1.2.10.jar axis2-adb-1.5.4.jar axis2-adb-codegen-1.5.4.jar axis2-codegen-1.5.4.jar axis2-corba-1.5.4.jar axis2-fastinfoset-...
axis2 webservice 服务端jar包: -->axis2-kernel-1.6.1.jar -->axis2-spring-1.6.1.jar -->axis2-transport-http-1.6.1.jar -->XmlSchema-1.4.7.jar -->wsdl4j-1.6.2.jar -->axiom-api-1.2.12.jar -->axiom...
axis2-std-1.0-bin.zip与axis2.war axis2-std-1.0-bin.zip与axis2.war
axis2-eclipse-codegen-plugin-1.6.2.zip和axis2-eclipse-service-plugin-1.6.2.zip,解压即可使用
axis2-1.5.1-bin.zip axis2-1.5.1-war.zip webservice部署axis所需要是包
共四个文件,都是最先版的,希望可以帮助大家。axis2-eclipse-service-archiver-wizard和axis2-eclipse-codegen-wizard和axis2-1.6.1-bin和axis2-1.6.1-war
绝对可以用,里面配有myeclipse10安装AXIS2插件的步骤,希望大家工作愉快,谢谢,给好评哦
subscribe to the `axis-c-dev@ws.apache.org' mailing list as described at http://ws.apache.org/axis2/c/mail-lists.html Acknowledgements ---------------- Apache Axis2/C relies heavily on the use ...
axis2-java2wsdl-1.5.4.jar axis2-jaxbri-1.5.4.jar axis2-jaxws-1.5.4.jar axis2-jibx-1.5.4.jar axis2-json-1.5.4.jar axis2-kernel-1.5.4.jar axis2-metadata-1.5.4.jar axis2-mtompolicy-1.5.4.jar axis2-saaj-...
目前axis2最高版本是2.0以上的版本,但是eclipse和myeclipse都不支持,无奈只能使用低版本的插件1.6.3;经实验,可以安装成功; 安装方法:右键解压到当前文件夹,Copy解压的文件到eclipse安装目录dropins下,重启...
axis2-1.6.2.zip, windows axis2工具,根据 WSDL生成java文件。 1、axis2客户端下载地址:http://mirror.esocc.com/apache//axis/axis2/java/core/1.6.2/axis2-1.6.2-bin.zip; 2、下载解压在D:\Work_Program_...
Axis2版本学习笔记Axis2版本学习笔记Axis2版本学习笔记Axis2版本学习笔记Axis2版本学习笔记Axis2版本学习笔记
axis2-1.5.3-bin.zip axis2-1.5.3-bin.zip axis2-1.5.3-bin.zip axis2-1.5.3-bin.zip
axis2-kernel-1.3.jar axis2-kernel-1.3.jar
axis2 soap技术 wsdl技术 java 的技术
web service axis2-1.7.4
soap-ws-consumer-axis2 使用Axis2的SOAP Web服务简单使用者。 从WSDL文件自动生成的源
axis2-1.7.4-bin、axis2-1.7.4-war完整包,可直接使用,其中支持java开发使用axis2调用webservice接口。
axis2-eclipse-codegen-plugin-1.6.2+axis2-eclipse-service-plugin-1.6.2