发新话题
打印

Java WS和C#调用

Java WS和C#调用

互操作性是Web Service的真正优势,虽然关于WS标准有很多,但不同厂家都是自己的实现,不同实现之间的互操作性因此很差,于是有了WS-I组织成立来管理这种差异,并提供统一的互操作性规范,到目前为止,这些工作仍在进行中,不过成果早已经有了:WS-I Attachment Profile 1.0并且也已经在Sun提供的WS设施上有了体现,通过使用wscompile 工具中的选项 -f wsi 即可生成兼容WS-I规范的java制品.本文通过修改前一篇http://blog.csdn.net/keepeye/services/trackbacks/408145.aspx文章中的WS服务使得这个服务可以在DotNet环境中调用.文件清单:SEI接口类IImage.java,实现SEI接口的类IIMageImpl.java,描述文件信息的值类型类FileInfo.java,Web应用部署描述文件web.xml,WS发布配置文件jaxrpc-ri.xml,WS编译配置文件config-interface.xml,构建客户端桩的config-wsdl.xml,构建文件builder.xml客户端测试类:Form1.cs.本例是从WSDL开始的,因此我们要有一个WSDL文件.第一步就是得到这个文件.(一) 得到WSDL文件.为达到互操作要求,这个文件的描述必须是文档样式的绑定,文字格式的编码.为得到此文件,首先介绍构建文件builder.xml:在这个构建文件中的build任务中, features的值指定为"wsi,documentliteral",这个任务需要的config-interface.xml:这个文件的需要的com.bin.IImage SEI接口文件及其实现文件com.bin.ImageImpl:public interface IImage extends Remote {//public DataHandler fetchImg(String sn) throws RemoteException;//public DataHandler[] fetchImgs(String[] sn) throws RemoteException;public SOAPMessage construcMsg(String[] fn) throws RemoteException;public byte[] fetchImage(String sn) throws RemoteException;public FileInfo[] fetchFileList() throws java.rmi.RemoteException;public FileInfo getFileList(String fn) throws java.rmi.RemoteException;}DataHandler,ArrayList属于java的类当然不允许出现在接口中了,因为C#不认识这些类,FileInfo值类型类倒支持,因为这个类没有方法,而且数据类型都是可以用SOAP来表示的,C#是可以理解的.实现文件,注意实现的方法是空方法.public class ImageImpl implements IImage {public SOAPMessage construcMsg(String[] fn) throws RemoteException {return null;}public byte[] fetchImage(String sn) throws RemoteException{return null;}public FileInfo[] fetchFileList() throws java.rmi.RemoteException{return null;}public FileInfo getFileList(String fn) throws RemoteException {return null;}}到这里,为互操作所做的修改只是在 features的值指定为"wsi,documentliteral",其它地方没有修改包括config-interface.xml接口配置文件.通过执行这个任务,可以得到我们想要的BinaryService.wsdl文件.位于nonclass文件夹中,把它移动到conf中.(二) 构造Web服务在这个conf文件夹中除了有BinaryService.wsdl外,还有两个文件:config-client.xml,用来根据WSDL文件生成客户端访问服务所需要的制品.config-server.xml则用来根据BinaryService.wsdl生成服务器端制品,包括重新生成的SEI接口文件,我们将利用这个文件编写服务实现,而不是前面提到的com.bin.IImage SEI接口文件及其实现文件com.bin.ImageImpl:到目前为止,还有两个文件没有提供:jaxrpc-ri.xml:web.xml文件:BinaryPath更改成你的本地文件路径index.htmindex.jspindex.jws下面就是构建过程,首先,执行wsi server service得到服务器端的java制品,包括重新生成的SEI,wscompile还生成了一个实现文件IImage_Impl ,后面要修改这个类,在其中加入有用的方法.wsidotnet.IImage清单:package wsidotnet;public interface IImage extends java.rmi.Remote { public wsidotnet.ConstrucMsgResponse construcMsg(wsidotnet.ConstrucMsg parameters) throws java.rmi.RemoteException; public wsidotnet.FetchFileListResponse fetchFileList(wsidotnet.FetchFileList parameters) throws java.rmi.RemoteException; public wsidotnet.FetchImageResponse fetchImage(wsidotnet.FetchImage parameters) throws java.rmi.RemoteException; public wsidotnet.GetFileListResponse getFileList(wsidotnet.GetFileList parameters) throws java.rmi.RemoteException;}和前面的com.bin.IImage接口相比很不同了.因为采用文档样式的绑定,所以这里就产生了所谓的包装类了.实现文件为:package wsidotnet;import java.io.File;import java.io.FileInputStream;import java.nio.MappedByteBuffer;import java.nio.channels.FileChannel;import java.util.Calendar;import java.util.Date;import javax.servlet.ServletContext;import javax.xml.rpc.ServiceException;import javax.xml.rpc.server.ServiceLifecycle;import javax.xml.rpc.server.ServletEndpointContext;public class IImage_Impl implements wsidotnet.IImage, java.rmi.Remote,ServiceLifecycle {ServletEndpointContext servletEndpointContext = null;String binarypath = "";ServletContext servletContext = null; public wsidotnet.ConstrucMsgResponse construcMsg(wsidotnet.ConstrucMsg parameters) throws java.rmi.RemoteException { wsidotnet.ConstrucMsgResponse _retVal = null; return _retVal; } public wsidotnet.FetchFileListResponse fetchFileList(wsidotnet.FetchFileList parameters) throws java.rmi.RemoteException { wsidotnet.FetchFileListResponse _retVal = new FetchFileListResponse(); _retVal.setResult(fetchFileList()); return _retVal; } public wsidotnet.FetchImageResponse fetchImage(wsidotnet.FetchImage parameters) throws java.rmi.RemoteException { wsidotnet.FetchImageResponse _retVal = new FetchImageResponse(); _retVal.setResult(fetchImage(parameters.getString_1())); return _retVal; } public wsidotnet.GetFileListResponse getFileList(wsidotnet.GetFileList parameters) throws java.rmi.RemoteException { wsidotnet.GetFileListResponse _retVal = null; return _retVal; }private FileInfo[] fetchFileList() {File file = new File(this.binarypath);System.out.println(this.binarypath);File[] c = file.listFiles();FileInfo[] fis = new FileInfo[c.length];for (int i = 0; i /// Form1 的摘要说明。/// public class Form1 : System.Windows.Forms.Form{private System.Windows.Forms.Button button1;private System.Windows.Forms.ListBox listBox1;private System.Windows.Forms.PictureBox pictureBox1;private System.Windows.Forms.StatusBar statusBar1;private System.Windows.Forms.StatusBarPanel statusBarPanel1;private System.Windows.Forms.StatusBarPanel statusBarPanel2;testws.binary.IImageBinding imagelist;private System.Windows.Forms.Panel panel1;private System.Windows.Forms.Panel panel2;private System.Windows.Forms.Panel panel3;private System.Windows.Forms.Panel panel4;private System.Windows.Forms.Splitter splitter1;/// /// 必需的设计器变量。/// private System.ComponentModel.Container components = null;public Form1(){//// Windows 窗体设计器支持所必需的//InitializeComponent();//// TODO: 在 InitializeComponent 调用后添加任何构造函数代码//}/// /// 清理所有正在使用的资源。/// protected override void Dispose( bool disposing ){if( disposing ){if (components != null) {components.Dispose();}}base.Dispose( disposing );}#region Windows 窗体设计器生成的代码/// /// 设计器支持所需的方法 - 不要使用代码编辑器修改/// 此方法的内容。/// private void InitializeComponent(){this.button1 = new System.Windows.Forms.Button();this.listBox1 = new System.Windows.Forms.ListBox();this.pictureBox1 = new System.Windows.Forms.PictureBox();this.statusBar1 = new System.Windows.Forms.StatusBar();this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel();this.panel1 = new System.Windows.Forms.Panel();this.panel2 = new System.Windows.Forms.Panel();this.panel3 = new System.Windows.Forms.Panel();this.panel4 = new System.Windows.Forms.Panel();this.splitter1 = new System.Windows.Forms.Splitter();((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).BeginInit();this.panel1.SuspendLayout();this.panel2.SuspendLayout();this.panel3.SuspendLayout();this.panel4.SuspendLayout();this.SuspendLayout();// // button1// this.button1.Location = new System.Drawing.Point(24, 8);this.button1.Name = "button1";this.button1.Size = new System.Drawing.Size(56, 23);this.button1.TabIndex = 0;this.button1.Text = "装入";this.button1.Click += new System.EventHandler(this.button1_Click);// // listBox1// this.listBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;this.listBox1.Dock = System.Windows.Forms.DockStyle.Fill;this.listBox1.ItemHeight = 12;this.listBox1.Location = new System.Drawing.Point(0, 0);this.listBox1.Name = "listBox1";this.listBox1.Size = new System.Drawing.Size(136, 230);this.listBox1.TabIndex = 1;this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);// // pictureBox1// this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;this.pictureBox1.Location = new System.Drawing.Point(0, 0);this.pictureBox1.Name = "pictureBox1";this.pictureBox1.Size = new System.Drawing.Size(261, 231);this.pictureBox1.TabIndex = 2;this.pictureBox1.TabStop = false;// // statusBar1// this.statusBar1.Location = new System.Drawing.Point(0, 271);this.statusBar1.Name = "statusBar1";this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] { this.statusBarPanel1, this.statusBarPanel2});this.statusBar1.ShowPanels = true;this.statusBar1.Size = new System.Drawing.Size(400, 22);this.statusBar1.TabIndex = 3;this.statusBar1.Text = "statusBar1";// // statusBarPanel1// this.statusBarPanel1.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents;this.statusBarPanel1.Width = 10;// // statusBarPanel2// this.statusBarPanel2.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;this.statusBarPanel2.Width = 374;// // panel1// this.panel1.Controls.Add(this.listBox1);this.panel1.Dock = System.Windows.Forms.DockStyle.Left;this.panel1.Location = new System.Drawing.Point(0, 0);this.panel1.Name = "panel1";this.panel1.Size = new System.Drawing.Size(136, 231);this.panel1.TabIndex = 4;// // panel2// this.panel2.Controls.Add(this.button1);this.panel2.Dock = System.Windows.Forms.DockStyle.Top;this.panel2.Location = new System.Drawing.Point(0, 0);this.panel2.Name = "panel2";this.panel2.Size = new System.Drawing.Size(400, 40);this.panel2.TabIndex = 5;// // panel3// this.panel3.Controls.Add(this.panel4);this.panel3.Controls.Add(this.splitter1);this.panel3.Controls.Add(this.panel1);this.panel3.Dock = System.Windows.Forms.DockStyle.Fill;this.panel3.Location = new System.Drawing.Point(0, 40);this.panel3.Name = "panel3";this.panel3.Size = new System.Drawing.Size(400, 231);this.panel3.TabIndex = 6;// // panel4// this.panel4.Controls.Add(this.pictureBox1);this.panel4.Dock = System.Windows.Forms.DockStyle.Fill;this.panel4.Location = new System.Drawing.Point(139, 0);this.panel4.Name = "panel4";this.panel4.Size = new System.Drawing.Size(261, 231);this.panel4.TabIndex = 5;// // splitter1// this.splitter1.Location = new System.Drawing.Point(136, 0);this.splitter1.Name = "splitter1";this.splitter1.Size = new System.Drawing.Size(3, 231);this.splitter1.TabIndex = 6;this.splitter1.TabStop = false;// // Form1// this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);this.ClientSize = new System.Drawing.Size(400, 293);this.Controls.Add(this.panel3);this.Controls.Add(this.statusBar1);this.Controls.Add(this.panel2);this.Name = "Form1";this.Text = "WS-I Demo---Access Sun WS";this.Load += new System.EventHandler(this.Form1_Load);((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).EndInit();this.panel1.ResumeLayout(false);this.panel2.ResumeLayout(false);this.panel3.ResumeLayout(false);this.panel4.ResumeLayout(false);this.ResumeLayout(false);}#endregion/// /// 应用程序的主入口点。/// [STAThread]static void Main() {Application.Run(new Form1());}private void Form1_Load(object sender, System.EventArgs e){listBox1.Items.Clear();panel4.AutoScroll =true;pictureBox1.SizeMode= PictureBoxSizeMode.AutoSize;imagelist=new testws.binary.IImageBinding();}private void button1_Click(object sender, System.EventArgs e){testws.binary.fetchFileList fl=new fetchFileList();listBox1.Items.Clear();//testws.binary.FileInfo[] fis=imagelist.fetchFileList(fl);if(fis!=null)Console.WriteLine(fis.Length);foreach(FileInfo info in fis){//Console.WriteLine(info.filepath+" "+ info.createdate);listBox1.Items.Add(info.filepath);}//testws.binary.HelloIFBinding bind=new HelloIFBinding();}private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e){ListBox lb=(ListBox)sender;if(lb.SelectedItem!=null){statusBar1.Panels[0].Text=lb.SelectedItem.ToString();testws.binary.fetchImage fn=new fetchImage();fn.String_1=lb.SelectedItem.ToString();testws.binary.fetchImageResponse res=imagelist.fetchImage(fn);try{System.IO.MemoryStream ms=new System.IO.MemoryStream(res.result);Image image=Image.FromStream(ms);pictureBox1.Image=image;panel4.AutoScrollMinSize=pictureBox1.Size;}catch(Exception ex){Console.WriteLine(ex.StackTrace);}}}}}测试界面:到此,在C#访问Java WS的过程结束了,实现互操作性关键在于要生成遵守WS-I的javaWeb服务制品,这样的服务才可以跨不同平台操作.

TOP

‹‹ 上一帖:java.util.Set翻译   |   下一帖:Java程序的编码规范(3) ››
发新话题
最近访问的版块