C#開發用戶端、JAVA和tomcat開發服務端

來源:互聯網
上載者:User

標籤:

hessian入門,Hello和檔案上傳範例,C#用戶端+Java Tomcat後台

2.Hello範例
1)後台--定義Java介面:
package org.migle.hessian; 
public interface Hello {  
    public String sayHello(String smt);  
    public void printHello(String smt);  
}
2)後台--實現Java介面:
package org.migle.hessian.impl;
import org.migle.hessian.Hello;
public class HelloImpl implements Hello {  
    public String sayHello(String smt) {  
        return smt != null ? "hello " + smt : "hello hessian";  
    }  
    public void printHello(String smt) {  
        System.out.println("Hello " + smt);  
    }  

3)後台--配置 Tomcat/HessianServer/WEB-INF/web.xml,前提條件是lib下包含hessian-4.0.7.jar:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>hessian</display-name>
 <servlet> 
  <servlet-name>hessian</servlet-name> 
  <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> 
  <init-param> 
  <param-name>service-class</param-name> 
  <param-value>org.migle.hessian.impl.HelloImpl</param-value> 
  </init-param>
  </servlet>
 <servlet-mapping>
  <servlet-name>hessian</servlet-name> 
  <url-pattern>/hessian</url-pattern> 
 </servlet-mapping>
</web-app>
4)前台--C#代碼,定義介面:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace WindowsFormsApplication3
{
    public interface Hello
    {

        string sayHello(string smt);
        void printHello(string smt);
    }
}
5)前台--C#代碼,實現遠程調用Java類,前提條件是引用hessianCsharp.dll:
......
using hessiancsharp.client;
using hessiancsharp.io;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string url = "http://localhost/HessianServer/hessian";
            CHessianProxyFactory factory = new CHessianProxyFactory();

            Hello test = (Hello)factory.Create(typeof(Hello), url);
            MessageBox.Show(test.sayHello("migle"));//列印從伺服器端擷取的字串  
            test.printHello("Hessian"); //在伺服器端控制台列印 "Hello Hessian"  
        }
......
6)運行C#程式。

3.檔案上傳範例,在2基礎上實現
1)後台--定義Java介面:
package org.migle.hessian;
import java.io.InputStream;
public interface UploadFile {
 public boolean uploadFile(String fileName, InputStream data);
}
2)後台--實現Java介面:
package org.migle.hessian.impl;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.migle.hessian.UploadFile;
public class UploadFileImpl implements UploadFile {

 @Override
 public boolean uploadFile(String fileName, InputStream in) {
  try
  {
   OutputStream out = new FileOutputStream("D:/temp/"+fileName);
   int nLength = 0;
   byte[] bData = new byte[1024];
   while( -1!=(nLength=in.read(bData)) )
   {
    out.write(bData, 0, nLength);
   }
  
   out.close();
   return true;
  } catch (FileNotFoundException e) {
   e.printStackTrace();
   return false;
  } catch (IOException e) {
   e.printStackTrace();
   return false;
  }
  finally
  {
   try {
    in.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
}
3)後台--配置 Tomcat/HessianServer/WEB-INF/web.xml,新增一個servlet:
......
 <servlet> 
  <servlet-name>upload</servlet-name> 
  <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> 
  <init-param> 
  <param-name>service-class</param-name> 
  <param-value>org.migle.hessian.impl.UploadFileImpl</param-value> 
  </init-param> 
 </servlet>
 <servlet-mapping>
  <servlet-name>upload</servlet-name> 
  <url-pattern>/upload</url-pattern> 
 </servlet-mapping>
......
4)前台--C#代碼,定義介面:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace WindowsFormsApplication3
{
    public interface Hello
    {

        string sayHello(string smt);
        void printHello(string smt);
    }

    public interface UploadFile //這是在hello基礎上新增的部分介面
    {
        bool uploadFile(string fileName, Stream srOutput);
    }
}
5)前台--C#代碼,實現遠程調用Java類,在Hello範例基礎上,新增一個上傳檔案的事件響應處理:
......
        private void buttonUpload_Click(object sender, EventArgs e)
        {
            Stream os = new FileStream(textBoxUpload.Text, FileMode.Open, FileAccess.Read);

            string url = "http://localhost/HessianServer/upload";
            CHessianProxyFactory factory = new CHessianProxyFactory();
            UploadFile test = (UploadFile)factory.Create(typeof(UploadFile), url);
            test.uploadFile("test.xml", os);
            MessageBox.Show("222");
            os.Close();
        }
......

C#開發用戶端、JAVA和tomcat開發服務端

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.