`

soap学习笔记(六)----使用Axis2传输附件(AXIS2 MTOM) (上)

    博客分类:
  • java
 
阅读更多

本文介绍如何使用Axis2传递附件。

 
1.工作环境
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.zip_axis2_axis2-idea-plugin_idea导入axis2_

    标题中的"axis2-idea-plugin-1.7.9.zip_axis2_axis2-idea-plugin_idea导入axis2_"提到了几个关键元素,分别是"axis2"、"idea-plugin"和"idea导入axis2",这暗示了这个压缩包是用于在IntelliJ IDEA这款集成开发环境...

    axis2-eclipse-codegen-plugin-1.6.2.zip和axis2-eclipse-service-plugin-1.6.2.zip

    Axis2允许开发者以XML方式处理服务,提供了丰富的功能,包括事务管理、安全、MTOM(消息传输优化机制)和WS-ReliableMessaging等。 2. **Eclipse插件**:Eclipse是一个流行的开源集成开发环境(IDE),支持多种编程...

    axis2-1.5.1-bin.zip axis2-1.5.1-war.zip axis2部署使用

    1. 对于`axis2-1.5.1-bin.zip`,你需要解压文件,然后在命令行中使用提供的脚本启动Axis2服务。 2. 创建Web服务,可以使用Java代码或者WSDL(Web Service Description Language)文件。 3. 将Web服务打包为AAR(Axis...

    axis2-eclipse-codegen-plugin-1.6.2和axis2-eclipse-service-plugin-1.6.2

    标题中的"axis2-eclipse-codegen-plugin-1.6.2和axis2-eclipse-service-plugin-1.6.2"指的是两个与Apache Axis2相关的Eclipse插件:Axis2代码生成插件和Axis2服务插件,它们是版本1.6.2的。Apache Axis2是一个流行的...

    axis2相关jar包~eclipse支持插件包

    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 附件传输

    在Axis2中,附件传输是通过MTOM(Message Transmission Optimization Mechanism)和SwA(SOAP with Attachments)两种技术实现的。MTOM是一种优化机制,用于高效地传输二进制数据,如图片或大文件,它能够将二进制...

    完整的axis2 jar包包含实例.zip

    axis2 webservice 服务端jar包: --&gt;axis2-kernel-1.6.1.jar --&gt;axis2-spring-1.6.1.jar --&gt;axis2-transport-http-1.6.1.jar --&gt;XmlSchema-1.4.7.jar --&gt;wsdl4j-1.6.2.jar --&gt;axiom-api-1.2.12.jar --&gt;axiom...

    Axis2版本学习笔记

    本笔记将深入探讨Axis2的核心概念、版本差异、安装配置以及在实际项目中的应用。 1. **核心概念** - **SOAP**: Axis2是基于SOAP(Simple Object Access Protocol)协议的,它允许应用程序通过HTTP进行通信,实现跨...

    axis2-std-1.0-bin.zip与axis2.war

    Apache Axis2是基于Java的Web服务引擎,它用于创建和部署Web服务以及处理SOAP消息。在给定的信息中,我们关注的是"axis2-std-1.0-bin.zip"和"axis2.war"这两个文件。 1. **axis2-std-1.0-bin.zip**: 这是一个包含了...

    axis2c-bin-1.6.0 axis2c-src-1.6.0

    axis2c-bin-1.6.0-linux.tar.gz axis2c-bin-1.6.0-win32.zip axis2c-src-1.6.0.tar.gz axis2c-src-1.6.0.zip 加md5

    axis2_demo实战 复杂对象传输 文件传输

    Axis2是Apache基金会开发的一个高效、灵活的SOAP(Simple Object Access Protocol)和RESTful Web服务框架,它构建在 Axis1.x 的基础上,提供了更加强大和优化的功能。本实战案例将关注使用Axis2进行复杂对象传输...

    axis2相关的jar包

    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-1.6.1

    - 多协议支持:除了基本的SOAP 1.1和1.2,Axis2还支持REST、MTOM(Message Transmission Optimization Mechanism)和SwA(Soap with Attachments)等传输方式。 - 高效的消息处理:使用了基于内存的数据结构,Axis2...

    axis2-1.4.1-bin.zip axis2-1.4.1-war.zip

    标题中的"axis2-1.4.1-bin.zip"和"axis2-1.4.1-war.zip"指的是Apache Axis2的两个不同版本的发行包,分别代表了Axis2的可执行二进制版本和Web应用程序版本。Apache Axis2是一个高度可扩展且功能强大的Web服务引擎,...

    axis2-idea-plugin-1.7.8

    Axis2支持多种Web服务标准,如SOAP、WSDL、WS-Addressing等,并提供了多种传输协议,如HTTP、SMTP等。通过使用Axis2,开发者可以方便地创建服务端和客户端Web服务,同时还能利用其模块化架构来扩展功能。 而"axis2-...

    axis2-1.7.5-bin.zip

    此外,Axis2支持MTOM(Message Transmission Optimization Mechanism)和SwA(Soap with Attachments),提高处理大型附件的效率。 8. **互操作性**:由于对Web服务标准的全面支持,Axis2与其他Web服务框架(如.NET...

    axis2-1.5.6完整版

    在实际开发中,开发人员通常会先解压 `axis2-1.5.6-bin.zip`,配置 Axis2 环境,然后使用 `axis2-1.5.6-war.zip` 在服务器上部署 Web 服务。如果需要更深入地理解 Axis2 的工作原理或者开发自定义模块,`axis2-1.5.6...

    axis2-1.6.2.zip

    2. **支持多种协议和消息格式**:Axis2支持HTTP、HTTPS、SMTP等多种传输协议,并且能够处理SOAP、RESTful、XML-RPC等不同格式的消息。 3. **WS-*兼容性**:Axis2实现了许多WS-*标准,如WS-Security、WS-...

    axis2-1.8.0apache-cxf-3.4.4.rar

    1. **SOAP处理**:Axis2能够处理各种SOAP消息,支持SOAP 1.1和1.2规范,包括处理附件(MTOM和SwA)和WS-Addressing等扩展。 2. **模块化架构**:Axis2采用模块化设计,允许开发者按需选择和组合功能,比如安全、...

Global site tag (gtag.js) - Google Analytics