• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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" >
  <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();
        }
......


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#四舍五入进一法直接取整带小数位数发布时间:2022-07-18
下一篇:
C#异常处理机制初步发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap