java后台搭建与客户端_C#开发客户端、JAVA和tomcat开发服务端

本文介绍了使用Hessian进行Java后台与C#客户端的远程调用。通过创建Java接口及实现,配置Tomcat,以及C#客户端的接口定义和调用,实现了简单的Hello World示例和文件上传功能。详细步骤包括Java接口定义、实现、Tomcat配置,以及C#代码的编写和执行。
摘要由CSDN通过智能技术生成

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:

hessian

hessian

com.caucho.hessian.server.HessianServlet

service-class

org.migle.hessian.impl.HelloImpl

hessian

/hessian

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:

......

upload

com.caucho.hessian.server.HessianServlet

service-class

org.migle.hessian.impl.UploadFileImpl

upload

/upload

......

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值