如果要传递二进制文件(如图像、音频文件等),可以使用byte[]作为数据类型进行传递,然后客户端使用RPC方式进行调用。这样做只是其中的一种方法,除此之外,在客户端还可以使用wsdl2java命令生成相应的stub类来调用WebService,wsdl2java命令的用法详见《WebService大讲堂之Axis2(1):用POJO实现0配置的WebService》。
WebService类中包含byte[]类型参数的方法在wsdl2java生成的stub类中对应的数据类型不再是byte[]类型,而是javax.activation.DataHandler。DataHandler类是专门用来映射WebService二进制类型的。
在WebService类中除了可以使用byte[]作为传输二进制的数据类型外,也可以使用javax.activation.DataHandler作为数据类型。 不管是使用byte[],还是使用javax.activation.DataHandler作为WebService方法的数据类型,使用wsdl2java命令生成的stub类中相应方法的类型都是javax.activation.DataHandler。而象使用.net、delphi生成的stub类的相应方法类型都是byte[]。这是由于javax.activation.DataHandler类是Java特有的,对于其他语言和技术来说,并不认识javax.activation.DataHandler类,因此,也只有使用最原始的byte[]了。
下面是一个上传二进制文件的例子,WebService类的代码如下:
package service;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import javax.activation.DataHandler;
public class FileService
{
// 使用byte[]类型参数上传二进制文件
public boolean uploadWithByte(byte[] file, String filename)
{
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(filename);
fos.write(file);
fos.close();
}
catch (Exception e)
{
return false;
}
finally
{
if (fos != null)
{
try
{
fos.close();
}
catch (Exception e)
{
}
}
}
return true;
}
private void writeInputStreamToFile(InputStream is, OutputStream os) throws Exception
{
int n = 0;
byte[] buffer = new byte[8192];
while((n = is.read(buffer)) > 0)
{
os.write(buffer, 0, n);
}
}
// 使用DataHandler类型参数上传文件
public boolean uploadWithDataHandler(DataHandler file, String filename)
{
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(filename);
// 可通过DataHandler类的getInputStream方法读取上传数据
writeInputStreamToFile(file.getInputStream(), fos);
fos.close();
}
catch (Exception e)
{
return false;
}
finally
{
if (fos != null)
{
try
{
fos.close();
}
catch (Exception e)
{
}
}
}
return true;
}
}
package service;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import javax.activation.DataHandler;
public class FileService
{
// 使用byte[]类型参数上传二进制文件
public boolean uploadWithByte(byte[] file, String filename)
{
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(filename);
fos.write(file);
fos.close();
}
catch (Exception e)
{
return false;
}
finally
{
if (fos != null)
{
try
{
fos.close();
}
catch (Exception e)
{
}
}
}
return true;
}
private void writeInputStreamToFile(InputStream is, OutputStream os) throws Exception
{
int n = 0;
byte[] buffer = new byte[8192];
while((n = is.read(buffer)) > 0)
{
os.write(buffer, 0, n);
}
}
// 使用DataHandler类型参数上传文件
public boolean uploadWithDataHandler(DataHandler file, String filename)
{
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(filename);
// 可通过DataHandler类的getInputStream方法读取上传数据
writeInputStreamToFile(file.getInputStream(), fos);
fos.close();
}
catch (Exception e)
{
return false;
}
finally
{
if (fos != null)
{
try
{
fos.close();
}
catch (Exception e)
{
}
}
}
return true;
}
}
上面代码在services.xml文件的配置代码如下:
<service name="fileService">
<description>
文件服务
</description>
<parameter name="ServiceClass">
service.FileService
</parameter>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</messageReceivers>
</service>
<service name="fileService">
<description>
文件服务
</description>
<parameter name="ServiceClass">
service.FileService
</parameter>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</messageReceivers>
</service>
如果使用wsdl2java命令生成调用Java客户端代码,则需要创建DataHandler类的对象实例,代码如下:
DataHandler dh = new DataHandler(new FileDataSource(imagePath));
wsdl2java命令会为每一个方法生成一个封装方法参数的类,类名为方法名(第一个字符大写),如uploadWithByte方法生成的类名为UploadWithByte。如果要设置file参数的值,可以使用UploadWithByte类的setFile方法,代码如下:
FileServiceStub fss = new FileServiceStub();
fss.uploadWithByte(uwb);
FileServiceStub fss = new FileServiceStub();
fss.uploadWithByte(uwb);
如果使用C#调用FileService,则file参数类型均为byte[],代码如下:
MemoryStream ms = new MemoryStream();
Bitmap bitmap = new Bitmap(picUpdateImage.Image);
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
service.fileService fs = new WSC.service.fileService();
fs.uploadWithDataHandler(ms.ToArray());
fs.uploadWithByte(ms.ToArray());
MemoryStream ms = new MemoryStream();
Bitmap bitmap = new Bitmap(picUpdateImage.Image);
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
service.fileService fs = new WSC.service.fileService();
fs.uploadWithDataHandler(ms.ToArray());
fs.uploadWithByte(ms.ToArray());
其中picUpdateImage为c#中加载图像文件的picturebox控件。
UploadWithByte uwb = new UPloadWithByte();
uwb.setFile(dh);
UploadWithByte uwb = new UPloadWithByte();
uwb.setFile(dh);
最后是调用uploadWithByte方法,代码如下(FileServiceStub为wsdl2java生成的stub类名):
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/crazystone4/archive/2009/06/20/4285730.aspx
分享到:
相关推荐
webservice4 二进制文件读取 webservice4 二进制文件读取
WebService 大讲堂之 Axis2(4):二进制文件传输 在本文中,我们将讨论如何使用 Axis2 实现二进制文件传输。Axis2 是一个基于 Java 的WebService框架,它提供了许多强大的功能来实现WebService的开发和部署。在...
axis2-eclipse-service-archiver-wizard.zip
共四个文件,都是最先版的,希望可以帮助大家。axis2-eclipse-service-archiver-wizard和axis2-eclipse-codegen-wizard和axis2-1.6.1-bin和axis2-1.6.1-war
目前axis2最高版本是2.0以上的版本,但是eclipse和myeclipse都不支持,无奈只能使用低版本的插件1.6.3;经实验,可以安装成功;...axis2-eclipse-service-plugin-1.6.3.zip axis2-eclipse-codegen-plugin-1.6.3.zip
axis2-eclipse-codegen-plugin-1.6.2.zip和axis2-eclipse-service-plugin-1.6.2.zip,解压即可使用
Axis开发Web Service实例,介绍AXIS的三种方法
通过AXIS技术发布一个接口,供调用者上传图片等二进制数据
绝对可以用,里面配有myeclipse10安装AXIS2插件的步骤,希望大家工作愉快,谢谢,给好评哦
axis2-eclipse-codegen-plugin-1.6.2+axis2-eclipse-service-plugin-1.6.2
axis2-idea-plugin-1.7.9
基于AXIS2实现Web Service开发
axis2-eclipse-service-plugin-1.7.4.zip eclipse插件
tools的工具包,axis2,apache-cxf
axis2-eclipse-service-plugin-1.5.4.zip
axis2-eclipse-codegen-plugin-1.6.0和axis2-eclipse-service-plugin-1.6.0有关jar包下载,解压后直接将这两个jar包放到eclips下的plugins目录下,重启eclipse即可
用axis2开发web service,过程比用axis1开发web service简单
axis2 eclipse 插件! axis2-eclipse-codegen-plugin-1.6.1.zip axis2-eclipse-service-plugin-1.6.1.zip 附件已经把两个插件压缩在一起了,只需一次下载
webservice 所需的jar包,包括axis.jar axis-saaj-1.4.jar,解压后导入即可