我来我网
https://5come5.cn
 
您尚未 登录  注册 | 菠菜 | 软件站 | 音乐站 | 邮箱1 | 邮箱2 | 风格选择 | 更多 » 
 

本页主题: Java WS和二进制数据传送 显示签名 | 打印 | 加为IE收藏 | 收藏主题 | 上一主题 | 下一主题

klo



性别: 帅哥 状态: 该用户目前不在线
等级: 品行端正
家族: YD一族
发贴: 473
威望: 0
浮云: 1105
在线等级:
注册时间: 2007-01-10
最后登陆: 2008-06-30

5come5帮你背单词 [ centigrade /'sentigreid/ n. 摄氏温度 ]


Java WS和二进制数据传送

Java WS和二进制数据传送
一) 有关介绍二进制数据在WS中传送,可以有两种方式:

1 把数据直接作为xml文档中某元素的字节流,作为XML解析器要解析的一部分,很明显这种方式比较低效.

2 二进制数据作为附件,作为带外(out of band)数据随同XML发送, 提高了效率.目前这类处理有几个规范:DIME(直接 Internet 消息封装),这个数据包装格式及其处理,只有微软在支持.
http://www.microsoft.com/china/msdn/archives/library/dnwebsrv/html/DIMEWSAttch.asp  
http://www.zdnet.com.cn/developer/code/story/0,3800066897,39358789,00.htm
MTOM(SOAP 消息传输优化机制)和XOP(二进制 XML 优化封装);在Sun规范JAX-RPC1.1中,要求使用SwA(SOAP with Attachments) 支持附件,为此Sun提供了SOAP with Attachments API for Java,带附件的SoapAPI(SAAJ),早期它和jaxm合在一起的,现在已经[屏蔽]开来形成了soap包,这个API专gate用来处理Soap附件的所有操作.JAXRPC 1.1 规范定义了MIME类型到Java类型的影射.

mime type
java type

image/gif
java.awt.image

image/jpeg
java.awt.image

text/plain
java.lang.string

multipart/*
javax.mail.internet.mimemultipart

text/xml or application/xml
javax.xml.transform.sourceJAXRPC

1.1 规范定义了这种情况:当绑定到上表中没有定义的类型影射或者是绑定到备用MIME类型时,它应该影射到 javax.activation.DataHandler.wscompile工具中的选项-f:用于类型影射的项:datahandleronly,该项指明总是把附件影射到DataHandler类型.

(二) 编写例子应用文件清单SEI接口类IImage.java,实现SEI接口的类IIMageImpl.java,描述文件信息的值类型类FileInfo.java,Web应用部署描述文件web.xml,WS发布配置文件jaxrpc-ri.xml,WS编译配置文件config-interface.xml,构建客户端桩的config-wsdl.xml,构建文件build.xml客户端测试类:MainBrowser.java,ImageListProvider.java和ImageLabelProvider.java,测试使用了SWT和JFace,请适当配置你的Eclipse环境,引入适当的库.这个例子是从SEI开始,绑定样式为 RPC.IImage.java清单:

package com.bin;import java.rmi.Remote;import java.rmi.RemoteException;import javax.activation.*;import javax.xml.soap.*;import java.util.*;public interface IImage extends Remote {public DataHandler fetchImg(String sn) throws RemoteException;public ArrayList fetchImgs(String[] sn) throws RemoteException;public SOAPMessage construcMsg(String[] fn) throws RemoteException;public ArrayList fetchFileList() throws java.rmi.RemoteException;public FileInfo getFileList(String fn) throws java.rmi.RemoteException;}实现类 ImageImpl.java清单:package com.bin;import javax.xml.soap.*;import java.net.*;import java.util.*;import java.io.*;import javax.activation.*;import java.rmi.*;import java.awt.*;import javax.xml.rpc.ServiceException;import javax.xml.rpc.server.*;import javax.xml.rpc.handler.soap.SOAPMessageContext;import javax.xml.rpc.handler.MessageContext;import javax.servlet.ServletContext;import com.sun.xml.rpc.server.*;public class ImageImpl implements IImage, ServiceLifecycle {ServletEndpointContext servletEndpointContext = null;String binarypath = "";ArrayList al;ServletContext servletContext = null;public void init(Object p0) throws ServiceException {// Some logic to do upon service creationservletEndpointContext = (ServletEndpointContext) p0;servletContext = servletEndpointContext.getServletContext();binarypath = servletContext.getInitParameter("BinaryPath");}public void destroy() {// Some logic to do on service destruction - e.g. clean up JDBCservletEndpointContext = null;servletContext = null;}public ArrayList fetchFileList() throws RemoteException {File file = new File(this.binarypath);//System.out.println(this.binarypath);if (al == null)al = new ArrayList();File[] c = file.listFiles();for (int i = 0; i BinaryPath更改成你的本地文件路径index.htmindex.jspindex.jwsconfig-interface.xml清单:config-wsdl.xml清单:jaxrpc-ri.xml清单:构建文件build.xml:依次运行build,create-war,deploy,genstaticstub等任务,这样本例中的WS所需要的文件全部生成.接下来,发布skysoft.war到你的web服务环境,本例使用的是Tomcat5,检查http://localhost:8080/skysoft/binary?WSDL是否装入.发布成功后,用Eclipse新建一个java项目,把静态桩文件悉数拷贝到该项目的src下,然后编写以下类:MainBrowser窗口类,显示图文件.
package swtui;import java.io.IOException;import java.rmi.RemoteException;import java.util.ArrayList;import javax.xml.rpc.Stub;import org.eclipse.jface.resource.ImageDescriptor;import org.eclipse.jface.viewers.ISelectionChangedListener;import org.eclipse.jface.viewers.IStructuredSelection;import org.eclipse.jface.viewers.SelectionChangedEvent;import org.eclipse.jface.viewers.TableViewer;import org.eclipse.jface.window.ApplicationWindow;import org.eclipse.swt.SWT;import org.eclipse.swt.custom.SashForm;import org.eclipse.swt.graphics.Image;import org.eclipse.swt.graphics.ImageData;import org.eclipse.swt.widgets.*;import com.binary.BinaryService_Impl;import com.binary.FileInfo;import com.binary.IImage;import com.binary.IImage_Stub;public class MainBrowser extends ApplicationWindow {Label label;ImageLabelProvider canvas;Image image;public MainBrowser(Shell arg0) {super(arg0);// TODO Auto-generated constructor stub}public MainBrowser() {super(null);addStatusLine();// TODO Auto-generated constructor stub}/** * ImageLoader Currently supported image formats are: * * BMP (Windows Bitmap) ICO (Windows Icon) JPEG GIF PNG */protected Control createContents(Composite parent) {getShell().setText("JFace File Explorer");SashForm sash_form = new SashForm(parent, SWT.HORIZONTAL | SWT.NULL);TableViewer tbv = new TableViewer(sash_form, SWT.BORDER| SWT.FULL_SELECTION | SWT.MULTI);ImageListProvider img = new ImageListProvider();tbv.setContentProvider(img);tbv.setInput(getFileInfo());tbv.addSelectionChangedListener(new ISelectionChangedListener() {public void selectionChanged(SelectionChangedEvent event) {IStructuredSelection selection = (IStructuredSelection) event.getSelection();FileInfo fi = (FileInfo) selection.getFirstElement();if (!fi.isIsdir()) {showImage(fi.toString());canvas.adjustSize();// canvas.redraw();}setStatus(fi.toString());}});canvas = new ImageLabelProvider(sash_form, SWT.SHELL_TRIM| SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL| SWT.H_SCROLL | SWT.CENTER);return sash_form;}public static void main(String[] args) {MainBrowser w = new MainBrowser();w.setBlockOnOpen(true);w.open();}public void showImage(String fn) {IImage_Stub stub = (IImage_Stub) createProxy();IImage hello = (IImage) stub;ImageData idata = null;try {idata = new ImageData(hello.fetchImg(fn).getInputStream());} catch (RemoteException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}ImageDescriptor id = ImageDescriptor.createFromImageData(idata);if (image != null)image.dispose();image = id.createImage();// label.setImage(id.createImage());// canvas.setData(image);canvas.setImage(image);}public Object getFileInfo() {IImage_Stub stub = (IImage_Stub) createProxy();IImage hello = (IImage) stub;ArrayList al = null;try {al = hello.fetchFileList();} catch (RemoteException e) {// TODO Auto-generated catch blocke.printStackTrace();}return al;}private Stub createProxy() {return (Stub) (new BinaryService_Impl().getIImagePort());}}ImageLabelProvider标签类,能滚动图象的组件.package swtui;import org.eclipse.swt.graphics.GC;import org.eclipse.swt.graphics.Image;import org.eclipse.swt.graphics.Point;import org.eclipse.swt.graphics.Rectangle;import org.eclipse.swt.widgets.Canvas;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Event;import org.eclipse.swt.widgets.Listener;import org.eclipse.swt.widgets.ScrollBar;import org.eclipse.swt.*;public class ImageLabelProvider extends Canvas {ScrollBar hBar = null;ScrollBar vBar = null;private Image image;public Image getImage() {return image;}public void setImage(Image image) {this.image = image;setData(image);}public ImageLabelProvider() {this(null, SWT.CENTER);// TODO Auto-generated constructor stub}public ImageLabelProvider(Composite arg0, int arg1) {super(arg0, arg1);hBar = getHorizontalBar();vBar = getVerticalBar();setListener();// TODO Auto-generated constructor stub}public void adjustSize() {if(image==null)return;final Point origin = new Point(0, 0);Rectangle rect = image.getBounds();Rectangle client = getClientArea();hBar.setMaximum(rect.width);vBar.setMaximum(rect.height);hBar.setThumb(Math.min(rect.width, client.width));vBar.setThumb(Math.min(rect.height, client.height));int hPage = rect.width - client.width;int vPage = rect.height - client.height;int hSelection = hBar.getSelection();int vSelection = vBar.getSelection();if (hSelection >= hPage) {if (hPage = vPage) {if (vPage = hPage) {if (hPage = vPage) {if (vPage??0) {gc.fillRectangle(rect.width, 0, marginWidth, client.height);}int marginHeight = client.height - rect.height;if (marginHeight > 0) {gc.fillRectangle(0, rect.height, client.width,marginHeight);}}});}}ImageListProvider.java代码清单:package swtui;import java.io.File;import java.rmi.RemoteException;import java.util.ArrayList;import javax.xml.rpc.Stub;import org.eclipse.jface.viewers.IStructuredContentProvider;import org.eclipse.jface.viewers.Viewer;import com.binary.*;public class ImageListProvider implements IStructuredContentProvider{public Object[] getElements1(Object element) {Object[] kids = ((ArrayList) element).toArray();return kids == null ? new Object[0] : kids;}private Stub createProxy() {return (Stub) (new BinaryService_Impl().getIImagePort());}public void dispose() {}public void inputChanged(Viewer arg0, Object arg1, Object arg2) {}}

到此,例子结束,应该说使用Eclipse,借助于插件,开发一个web服务还是相当快捷的. 本例可通过扩展客户端以处理其它格式的文件.下一篇将介绍如何在C#中调用这个服务
顶端 Posted: 2008-04-26 13:10 | [楼 主]
我来我网·5come5 Forum » 程序员之家

Total 0.006706(s) query 4, Time now is:11-22 04:53, Gzip enabled
Powered by PHPWind v5.3, Localized by 5come5 Tech Team, 黔ICP备16009856号