C#开发客户端、JAVA和tomcat开发服务端

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();
        }
......

上一篇:mmap直接控制底层【转】


下一篇:window 常用软件