爪哇哇

一个软件分析师的博客

XFIRE的一个例子,发现左web service真是简单得要命!! 侧面印证了写代码越来越没前途了,哈

接口 interface
import J2EE技术的爬虫.util.Collection;
import J2EE技术的爬虫.util.List;

public interface IMathService {
 /**
  * 加
  * @param a
  * @param b
  * @return
  */
 public int add(int a,int b);
 /**
  * 减
  * @param a
  * @param b
  * @return
  */
 public int sub(int a,int b);
/**
上传二进制文件
*/
  public String sendFile(String fileName,byte[] file );
 
}

实现 implements

import J2EE技术的爬虫.io.File;
import J2EE技术的爬虫.io.FileOutputStream;
import J2EE技术的爬虫.sql.Connection;
import J2EE技术的爬虫.sql.ResultSet;
import J2EE技术的爬虫.sql.Statement;
import J2EE技术的爬虫.util.ArrayList;
import J2EE技术的爬虫.util.Collection;
import J2EE技术的爬虫.util.List;

import com.newsoft.oa.bean.User;
import com.newsoft.oa.uitl.Connector;
import com.thoughtworks.xstream.XStream;

public class MathServiceImpl implements IMathService{
  public int add(int a,int b){
   return a+b;
  }
 
  public int sub(int a,int b){
   return a-b;
  }
  public String getWelComeStr(String name){
   return "hi "+name+"! 欢迎你";
  }
  public List getUsers(){
   List l=new ArrayList();
   l.add("name");
   l.add("password");
   l.add("sex");
   return l;
  }

public String sendFile(String fileName, byte[] filebytes) {
 try{
  String path="";
   if(filebytes!=null&&filebytes.length>0){
    File file=new File("/"+fileName);
   
    file.createNewFile();
    FileOutputStream fos=new FileOutputStream(file);
    fos.write(filebytes);
    fos.close();
    path=file.getAbsolutePath();
    System.out.println(path);
   
    file=null;
   
   }
  
   return path;
 }catch(Exception ex){
  return "false";
 }
}

}

配置文件

放在 Classes/META-INF/xfire/service.xml;里面

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
 <service>
  <name>MathService</name>
  <namespace>newsoft/oa/MathService</namespace>
  <serviceClass>
   com.newsoft.oa.services.IMathService
  </serviceClass>
  <implementationClass>
   com.newsoft.oa.services.MathServiceImpl
  </implementationClass>

 </service>
</beans>

其实是借鉴了Spring的写法,用过Spring不会对着陌生,(Application-context.xml)

WEB-XML加上

<servlet>
  <servlet-name>XFireServlet</servlet-name>
  <servlet-class>
   org.codehaus.xfire.transport.http.XFireConfigurableServlet
  </servlet-class>
 </servlet>

 <servlet-mapping>
  <servlet-name>XFireServlet</servlet-name>
  <url-pattern>/servlet/XFireServlet/*</url-pattern>
 </servlet-mapping>

 <servlet-mapping>
  <servlet-name>XFireServlet</servlet-name>
  <url-pattern>/services/*</url-pattern>
 </servlet-mapping>


最后就是客户端了

/ /Create a metadata of the service             
 Service serviceModel = new ObjectServiceFactory().create(IMathService.class);
// Create a proxy for the deployed service     
 
 XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());  
 String serviceUrl = "http://localhost:8080/ws/services/MathService";
  client = null;      
 try {         
   client = (IMathService) factory.create(serviceModel, serviceUrl); 
   File file=new File("c:\\SUPERMAP 白皮书.pdf");
   FileInputStream fis=new FileInputStream(file);
   byte[] b=new byte[fis.available()];
   fis.read(b);
  System.out.println(client.sendFile(file.getName(), b));
 } catch (Exception ex) {        
   ex.printStackTrace();
 }                           //Invoke the service   
 int serviceResponse = 0;
 int a=10,b=20;

就是

Service serviceModel = new ObjectServiceFactory().create(IMathService.class); 
 XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());  
 String serviceUrl = http://localhost:8080/ws/services/MathService;

三行字建立连接请求,

太轻松了

传输文件速度也可以,二进制,2M多的文件,也能轻松传递

用.net和delphi平台测试,兼容性没问题(按道理 soap,也不应该有问题)

这是为客户搭建的在 档案系统和OA审批间作文件归档的尝试项目

哈,完整的项目代码,就不方便讲了。

posted on 2007-12-29 14:59 李立波 阅读(2488) 评论(2)  编辑  收藏 所属分类: JAVA

Feedback

# re: XFIRE的一个例子,发现左web service真是简单得要命!! 侧面印证了写代码越来越没前途了,哈 2008-09-27 15:23 杨培海

请教个问题:您的客户端和服务端都在同一个工程下面,要是客户端和服务端不在同一个工程下,您要怎样转换为IMathService类型的接口呢?我的邮箱是472579211@qq.com  回复  更多评论   

# re: XFIRE的一个例子,发现左web service真是简单得要命!! 侧面印证了写代码越来越没前途了,哈[未登录] 2010-01-18 08:41 BS

你都傻的,客户端的不能这样做的,你的客户端要在别的机器上测试才得
这样写大把都有啦  回复  更多评论   



只有注册用户登录后才能发表评论。


网站导航:
 

My Links

Blog Stats

News

常用链接

留言簿(5)

随笔分类

随笔档案

文章档案

相册

搜索

最新评论

阅读排行榜

评论排行榜