Hessian entry, Hello and file upload example, C # client + Java Tomcat background

Source: Internet
Author: User

. Contains Hello examples and file upload examples

2. Hello example
1) backend -- Define the Java interface:
Package org. migle. hessian;
Public interface Hello {
Public String sayHello (String smt );
Public void printHello (String smt );
}
2) backend -- Implement the Java interface:
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) background -- configure Tomcat/HessianServer/WEB-INF/web. xml, the premise is that lib contains 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) foreground -- C # code, defining the interface:
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) foreground -- C # code to remotely call the Java class, provided that hessianCsharp. dll is referenced:
......
Using hessiancsharp. client;
Using hessiancsharp. io;

Namespace WindowsFormsApplication3
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

Private void button#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"); // print the string obtained from the server
Test. printHello ("Hessian"); // print "Hello Hessian" on the server console"
}
......
6) run the C # program.

3. File Upload example, implemented on the basis of 2
1) backend -- Define the Java interface:
Package org. migle. hessian;
Import java. io. InputStream;
Public interface UploadFile {
Public boolean uploadFile (String fileName, InputStream data );
}
2) backend -- Implement the Java interface:
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) backend -- configure Tomcat/HessianServer/WEB-INF/web. xml, add a 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) foreground -- C # code, defining the interface:
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 // This is a part of the newly added interface based on hello
{
Bool uploadFile (string fileName, Stream srOutput );
}
}
5) foreground -- C # code for remote calling of Java class. Based on the Hello example, an event Response Processing for file uploading is added:
......
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 ();
}
......

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.