.包含Hello范例和文件上傳范例
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/javaee http://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();
}
......