Webservice传送文件的实现(AXIS2 MTOM)

工作环境:IDE: Eclipse 3.1.2
                    jdk: jdk1.5.0_06
                    Tomcat: apache-tomcat-5.5.15
                    AXIS2:1.0(war版本和bin版本)
 
环境准备:
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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要使用Axis2在Java中实现Web服务接口,可以按照以下步骤进行: 1. 下载和安装Axis2:首先,你需要下载并安装Axis2框架。你可以从Apache官方网站(https://axis.apache.org/axis2/java/core/download.cgi)上下载最新版本的Axis2。 2. 创建Java项目:使用你喜欢的Java开发工具(如Eclipse、IntelliJ IDEA等),创建一个新的Java项目。 3. 导入Axis2库:将Axis2库导入到你的项目中。你可以将下载的Axis2文件添加到项目的构建路径中,或者使用构建工具(如Maven、Gradle等)来管理依赖项。 4. 创建Web服务接口:在项目中创建一个Java接口,定义你的Web服务接口。这个接口将包含你希望暴露给客户端的操作。 ```java public interface MyWebService { public String processRequest(String request); } ``` 5. 实现Web服务接口:创建一个Java类来实现你的Web服务接口。 ```java public class MyWebServiceImpl implements MyWebService { public String processRequest(String request) { // 处理Web服务请求并返回响应 return "Hello, " + request + "! This is a response from the web service."; } } ``` 6. 创建服务端:使用Axis2创建一个服务端来发布你的Web服务。 ```java import org.apache.axis2.transport.http.server.HttpServiceProcessor; public class WebServiceServer { public static void main(String[] args) { try { // 创建服务端配置 ConfigurationContext configContext = ConfigurationContextFactory.createDefaultConfigurationContext(); // 创建服务端 Axis2Server server = new Axis2Server(configContext); // 注册Web服务实现类 server.addService(MyWebService.class.getName(), new MyWebServiceImpl()); // 启动服务端 server.start(); System.out.println("Web service is running."); } catch (AxisFault e) { e.printStackTrace(); } } } ``` 7. 构建和运行:构建并运行你的Java项目。服务端将会启动,并在默认端口(一般为8080)上监听来自客户端的请求。 8. 创建客户端:使用Axis2创建一个客户端来调用你的Web服务。 ```java import org.apache.axis2.client.ServiceClient; import org.apache.axis2.client.Options; import org.apache.axis2.addressing.EndpointReference; public class WebServiceClient { public static void main(String[] args) { try { // 创建服务客户端 ServiceClient client = new ServiceClient(); // 创建服务端点引用 EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/MyWebService"); // 设置服务端点地址 Options options = new Options(); options.setTo(targetEPR); client.setOptions(options); // 创建请求操作 QName operationName = new QName("http://example.com/MyWebService", "processRequest"); Object[] operationParams = new Object[] { "John" }; Class[] operationReturnTypes = new Class[] { String.class }; // 调用Web服务操作 Object[] response = client.invokeBlocking(operationName, operationParams, operationReturnTypes); String result = (String) response[0]; System.out.println("Response from web service: " + result); } catch (AxisFault e) { e.printStackTrace(); } } } ``` 9. 构建和运行:构建并运行你的Java客户端项目。客户端将会调用服务端的Web服务,并接收并打印响应。 通过以上步骤,你可以使用Axis2在Java中实现和调用Web服务接口。请注意,根据你的实际需求,可能需要配置和调整Axis2的一些参数和设置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值