加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

jdk1.4客户端调用jdk1.6或更高Axis2 webservice问题

(2013-12-02 17:31:33)
标签:

webservice

java

axis2

axis1

it

分类: 接口

服务端在jdk1.6上,webservice使用的是Axis2。

jdk1.5或更低调用webservice需使用Axis1.*(注:Axis2基于jdk1.6,低版本运行报错)。

客户端:例如

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.rmi.RemoteException;

import javax.activation.DataHandler;

import javax.xml.namespace.QName;

import javax.xml.rpc.ParameterMode;

import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

import org.apache.axis.encoding.XMLType;

import org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory;

import org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory;

public class FileReceiveClient {

         public static void main(String[] args) {

                  // TODO Auto-generated method stub

                  String url = "http://127.0.0.1:8090/myAxis/services/FileSendServer";

                  Service service = new Service();

                  Call call;

                  DataHandler handler;

                  InputStream input = null;

                  FileOutputStream fos = null;

                  String filepath = "d:/upload/";

                  String filename = filepath + "tests.txt";

                  File file = new File(filename);

                  try {

                          call = (Call) service.createCall();

                          QName qn = new QName("ns1:DataHandler", "DataHandler");

                          call.setTargetEndpointAddress(url);

                          call.setOperationName(new QName("FileSendServer", "getFile"));

                         

                          call.registerTypeMapping(DataHandler.class, qn,

                                            JAFDataHandlerSerializerFactory.class,

                                            JAFDataHandlerDeserializerFactory.class);

//                       call.addParameter("source", XMLType.XSD_STRING ,ParameterMode.IN); //设置服务调用方法的传入参数类型

                            call.setReturnType(qn,DataHandler.class);//需要返回的QName以及返回的类型是DataHandler.class

                          handler = (DataHandler) call.invoke(new Object[] {});

                          input = handler.getInputStream();

                          fos = new FileOutputStream(file);

                          byte[] buffer = new byte[1024];

                          while (input.read(buffer) != -1) {

                                   fos.write(buffer);

                          }

                  } catch (ServiceException e) {

                          // TODO Auto-generated catch block

                          e.printStackTrace();

                  } catch (RemoteException e) {

                          // TODO Auto-generated catch block

                          e.printStackTrace();

                  } catch (IOException e) {

                          // TODO Auto-generated catch block

                          e.printStackTrace();

                  } finally {

                          if (input != null) {

                                   try {

                                            input.close();

                                   } catch (Exception e2) {

                                            // TODO: handle exception

                                   }

                          }

                          if (fos != null) {

                                   try {

                                            fos.close();

                                   } catch (Exception e2) {

                                            // TODO: handle exception

                                   }

                          }

                  }

 

         }

 

}

0

阅读 收藏 喜欢 打印举报/Report
前一篇:FreeMarker
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有