爪哇哇

          一個(gè)軟件分析師的博客

          XFIRE的一個(gè)例子,發(fā)現(xiàn)左web service真是簡(jiǎn)單得要命!! 側(cè)面印證了寫代碼越來(lái)越?jīng)]前途了,哈

          接口 interface
          import J2EE技術(shù)的爬蟲(chóng).util.Collection;
          import J2EE技術(shù)的爬蟲(chóng).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);
          /**
          上傳二進(jìn)制文件
          */
            public String sendFile(String fileName,byte[] file );
           
          }

          實(shí)現(xiàn) implements

          import J2EE技術(shù)的爬蟲(chóng).io.File;
          import J2EE技術(shù)的爬蟲(chóng).io.FileOutputStream;
          import J2EE技術(shù)的爬蟲(chóng).sql.Connection;
          import J2EE技術(shù)的爬蟲(chóng).sql.ResultSet;
          import J2EE技術(shù)的爬蟲(chóng).sql.Statement;
          import J2EE技術(shù)的爬蟲(chóng).util.ArrayList;
          import J2EE技術(shù)的爬蟲(chóng).util.Collection;
          import J2EE技術(shù)的爬蟲(chóng).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>

          其實(shí)是借鑒了Spring的寫法,用過(guò)Spring不會(huì)對(duì)著陌生,(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;

          三行字建立連接請(qǐng)求,

          太輕松了

          傳輸文件速度也可以,二進(jìn)制,2M多的文件,也能輕松傳遞

          用.net和delphi平臺(tái)測(cè)試,兼容性沒(méi)問(wèn)題(按道理 soap,也不應(yīng)該有問(wèn)題)

          這是為客戶搭建的在 檔案系統(tǒng)和OA審批間作文件歸檔的嘗試項(xiàng)目

          哈,完整的項(xiàng)目代碼,就不方便講了。

          posted on 2007-12-29 14:59 李立波 閱讀(2510) 評(píng)論(2)  編輯  收藏 所屬分類: JAVA

          Feedback

          # re: XFIRE的一個(gè)例子,發(fā)現(xiàn)左web service真是簡(jiǎn)單得要命!! 側(cè)面印證了寫代碼越來(lái)越?jīng)]前途了,哈 2008-09-27 15:23 楊培海

          請(qǐng)教個(gè)問(wèn)題:您的客戶端和服務(wù)端都在同一個(gè)工程下面,要是客戶端和服務(wù)端不在同一個(gè)工程下,您要怎樣轉(zhuǎn)換為IMathService類型的接口呢?我的郵箱是472579211@qq.com  回復(fù)  更多評(píng)論   

          # re: XFIRE的一個(gè)例子,發(fā)現(xiàn)左web service真是簡(jiǎn)單得要命!! 側(cè)面印證了寫代碼越來(lái)越?jīng)]前途了,哈[未登錄](méi) 2010-01-18 08:41 BS

          你都傻的,客戶端的不能這樣做的,你的客戶端要在別的機(jī)器上測(cè)試才得
          這樣寫大把都有啦  回復(fù)  更多評(píng)論   



          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          My Links

          Blog Stats

          News

          常用鏈接

          留言簿(5)

          隨筆分類

          隨筆檔案

          文章檔案

          相冊(cè)

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 静乐县| 平阴县| 昆明市| 金乡县| 庆云县| 白城市| 云安县| 仙游县| 江陵县| 五指山市| 城口县| 峨眉山市| 工布江达县| 乾安县| 红桥区| 新龙县| 防城港市| 田阳县| 海盐县| 城市| 兴宁市| 喀喇沁旗| 锡林郭勒盟| 昌都县| 宁津县| 阳曲县| 黔西县| 清流县| 得荣县| 军事| 逊克县| 内黄县| 青河县| 阜宁县| 金乡县| 虞城县| 远安县| 定陶县| 潜山县| 汶上县| 长海县|