天天看点

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专门用来处理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

public class FileInfo {

 private String filename;

 private String filepath;

 private long filelength;

 private Date createdate;

 private boolean isdir = false;

 public FileInfo() {

 }

 public Date getCreatedate() {

  return createdate;

 }

 public void setCreatedate(Date createdate) {

  this.createdate = createdate;

 }

 public long getFilelength() {

  return filelength;

 }

 public void setFilelength(long filelength) {

  this.filelength = filelength;

 }

 public String getFilename() {

  return filename;

 }

 public void setFilename(String filename) {

  this.filename = filename;

 }

 public String getFilepath() {

  return filepath;

 }

 public void setFilepath(String filepath) {

  this.filepath = filepath;

 }

 public String toString() {

  return this.filename + this.getFilepath() + this.getFilelength();

 }

 public boolean isIsdir() {

  return isdir;

 }

 public void setIsdir(boolean isdir) {

  this.isdir = isdir;

 }

}

web.xml清单:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"

 version="2.4">

 <context-param>

  <param-name>BinaryPath</param-name>

  <param-value>更改成你的本地文件路径</param-value>

 </context-param>

 <welcome-file-list>

  <welcome-file>index.htm</welcome-file>

  <welcome-file>index.jsp</welcome-file>

  <welcome-file>index.jws</welcome-file>

 </welcome-file-list>

</web-app>

config-interface.xml清单:

<?xml version="1.0" encoding="UTF-8"?>

<configuration xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config">

 <service name="BinaryService" targetNamespace="urn:binary"

  typeNamespace="urn:binary" packageName="com.binary">

  <interface name="com.bin.IImage"

   servantName="com.bin.ImageImpl" />

 </service>

</configuration>

config-wsdl.xml清单:

<?xml version="1.0" encoding="UTF-8"?>

<configuration xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config">

 <wsdl location="http://localhost:8080/skysoft/binary?WSDL"

  packageName="com.binary" />

</configuration>

jaxrpc-ri.xml清单:

<?xml version="1.0" encoding="UTF-8"?>

<webServices xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/dd"

 version="1.0"

 targetNamespaceBase="http://java.sun.com/xml/ns/jax-rpc/wsi/wsdl"

 typeNamespaceBase="http://java.sun.com/xml/ns/jax-rpc/wsi/types"

 urlPatternBase="/ws">

 <endpoint name="binaryservice" displayName="Stock Example"

  description="Binary Web Service endpoint"

  inter implementation="com.bin.ImageImpl"

  model="/WEB-INF/model.xml.gz" />

 <endpointMapping endpointName="binaryservice" urlPattern="/binary" />

</webServices>

构建文件build.xml:依次运行build,create-war,deploy,genstaticstub等任务,这样本例中的WS所需要的文件全部生成.

<?xml version="1.0" encoding="GBK"?>

<project name="webservice" default="build" basedir=".">

 <property name="jaxrpc.lib.dir2" value="D:/jwsdp-1.5/jaxrpc/lib">

 </property>

 <property name="jaxrpc.lib.dir" value="I:/jwsdp-1.6/jaxrpc/lib">

 </property>

 <property name="jaxrpc.lib.dir1" value="D:/Sun/AppServer/lib">

 </property>

 <property name="classes.dir" value="./build/classes">

 </property>

 <property name="src.dir" value="./build/src">

 </property>

 <property name="war.file" value="hello_raw.war">

 </property>

 <property name="mapping.file" value="mapping.xml">

 </property>

 <property name="tmp.dir" value="./tmp">

 </property>

 <property name="nonclass.dir" value="./build/nonclass">

 </property>

 <property name="build" value="${nonclass.dir}">

 </property>

 <property name="assemble" value="./assemble">

 </property>

 <property name="assemble.war" value="./assemble/war">

 </property>

 <property name="assemble.ear" value="./assemble/ear">

 </property>

 <path id="jaxrpc-classpath">

  <!--fileset dir="${jaxrpc.lib.dir}">

   <include name="***.jar" />

  </fileset>

 </path>

 <path id="compile.classpath">

  <!--fileset dir="${jaxrpc.lib.dir}">

   <include name="***.jar" />

  </fileset>

  <fileset dir="i:/jwsdp-1.6/jaxrpc/lib">

   <include name="***.class" excludes="***.wsdl, ***.jar" excludes="***.wsdl, **

 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 block

   e.printStackTrace();

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.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 block

   e.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 <= 0)

    hSelection = 0;

   origin.x = -hSelection;

  }

  if (vSelection >= vPage) {

   if (vPage <= 0)

    vSelection = 0;

   origin.y = -vSelection;

  }

  redraw();

 }

 private void setListener() {

  final Point origin = new Point(0, 0);

  hBar.addListener(SWT.Selection, new Listener() {

   public void handleEvent(Event e) {

    int hSelection = hBar.getSelection();

    int destX = -hSelection - origin.x;

    Rectangle rect = image.getBounds();

    scroll(destX, 0, 0, 0, rect.width, rect.height, false);

    origin.x = -hSelection;

   }

  });

  vBar.addListener(SWT.Selection, new Listener() {

   public void handleEvent(Event e) {

    int vSelection = vBar.getSelection();

    int destY = -vSelection - origin.y;

    Rectangle rect = getBounds();

    scroll(0, destY, 0, 0, rect.width, rect.height, false);

    origin.y = -vSelection;

   }

  });

  addListener(SWT.Resize, new Listener() {

   public void handleEvent(Event e) {

    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 <= 0)

      hSelection = 0;

     origin.x = -hSelection;

    }

    if (vSelection >= vPage) {

     if (vPage <= 0)

      vSelection = 0;

     origin.y = -vSelection;

    }

    redraw();

   }

  });

  addListener(SWT.Paint, new Listener() {

   public void handleEvent(Event e) {

    GC gc = e.gc;

    gc.drawImage(image, origin.x, origin.y);

    Rectangle rect = image.getBounds();

    Rectangle client = getClientArea();

    int marginWidth = client.width - rect.width;

    if (marginWidth > 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#中调用这个服务.

版权声明:本文为CSDN博主「wannaberer」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/wannaberer/article/details/516752

更多相关推荐

二进制

基础知识

补码

二进制机器运算使用的是二进制补码机器数一个数在机器中的二进制表现形式就是这个数的机器数。机器数都是带符号的,最高位作为符号位,0代表正,1代表负包括3种表现形式:原码、反码、补码以机器字长为8位为例,-10....

关于二进制

基础知识

这篇文章还是转载的我们都知道,计算机的底层都是使用二进制数据进行数据流传输的,那么为什么会使用二进制表示计算机呢?或者说,什么是二进制数呢?再拓展一步,如何使用二进制进行加减乘除?什么是二进制数那么什...

使用Java操作二进制文件 (转)

Java基础

java

buffer

file

byte

image

string

使用Java操作二进制文件(转)在从File类开始IO系统介绍一文中,我们详细的介绍了File类。这个类非常有用,我们可以用它作桥梁把文件和流轻松的联系起来。在JavaIO专题中,我准备先介绍一些实用的关于JavaIO编程方法,...

二进制数据形式保存和获取图片

专业

image

null

sql

string

file

insert

  //以二进制形式保存文件到数据库  publicvoidsetImages(Filefile){      Connectionconn=null;      Stringsql="insertintoimageTab(IMAGE)values(?)";      PreparedStatementps=null;      FileInputStreamis=nu...

使用Java操作二进制文件

J2SE

java

buffer

file

byte

image

string

在从File类开始IO系统介绍一文中,我们详细的介绍了File类。这个类非常有用,我们可以用它作桥梁把文件和流轻松的联系起来。在JavaIO专题中,我准备先介绍一些实用的关于JavaIO编程方法,而不是先从整体来把握IO,因...

    猜您喜欢

  • 计算机二进制
  • 读取图片文件将其转为二进制,再生成新的图片
  • java基础知识(包含二进制、八进制、十进制和十六进制...
  • Java读取二进制文件
  • -128 二进制变化

    文章随机推荐

  • 如何进行单元测试(三)
  • Flutter 03: 图解第一个程序 Hello World!
  • dir在php中的使用方法图解,phpclosedir()函数的简单使...
  • N1 小钢炮docker安装迅雷方法
  • zookeeper的简单使用
  • Android支持的图片格式
  • 使用Java实现简单的用户登录
  • MySQL 水平拆分(读书笔记整理)
  • writel底层函数实现相关
  • 更新excel中的多条数据
  • zabbix监控系统入坑必看
  • fabric.js 怎么将 Group 中的元素包装在一个容器元素...
  • UILabel,UITextField,UIButton,UIimageView
  • 多路选择器(multiplexer)简介
  • Vue3-生命周期函数
  • 首页
  • 技术博客
  • 联系我们
  • 版权申明
  • 隐私条款
© 2023 All rights reserved by CodeAntenna.com.