WebService大讲堂之Axis2(4):二进制文件传输
本文为原创,如需转载,请注明作者和出处,谢谢! ??? 在《WebService大讲堂之Axis2(2):复合类型数据的传递》中讲过,如果要传递二进制文件(如图像、音频文件等),可以使用byte[]作为数据类型进行传递,然后客户端使用RPC方式进行调用。这样做只是其中的一种方法,除此之外,在客户端还可以使用wsdl2java命令生成相应的stub类来调用WebService,wsdl2java命令的用法详见《WebService大讲堂之Axis2(1):用POJO实现0配置的WebService》。
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> package ?service; import ?java.io.InputStream; ?java.io.OutputStream; ?java.io.FileOutputStream; ?javax.activation.DataHandler; public ? class ?FileService { ??? // ??使用byte[]类型参数上传二进制文件 ???? 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? != ) ?????????????{ ????????????????? ?????????????????{ ?????????????????????fos.close(); ?????????????????} ????????????????? ?(Exception?e) ?????????????????{ ?????????????????} ?????????????} ?????????} ????????? true ; ????} ???? private void ?writeInputStreamToFile(InputStream?is,?OutputStream?os)? throws ?Exception ????{ ????????? int ?n? 0 []?buffer? [ 8192 ]; ????????? while ((n? ?is.read(buffer))? > ) ?????????{ ?????????????os.write(buffer,? ,?n); ?????????} ????} ???? ??使用DataHandler类型参数上传文件 ?uploadWithDataHandler(DataHandler?file,?String?filename) ????{ ???????? ?????????FileOutputStream?fos? ?????????{???????????? ?????????????fos? ?FileOutputStream(filename);??? ????????????? ??可通过DataHandler类的getInputStream方法读取上传数据 ?????????????writeInputStreamToFile(file.getInputStream(),?fos); ?????????????fos.close(); ?????????} ????????? ; ????} }
上面代码在services.xml文件的配置代码如下: <
service?
name
="fileService"
>
???? description ????????文件服务 ???? </ parameter? ="ServiceClass" ????????service.FileService? ???? parameter ???????? messageReceivers messageReceiver? mep ="http://www.w3.org/2004/08/wsdl/in-out" ????????????class ="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> service
DataHandler?dh?
?DataHandler(
?FileDataSource(imagePath));
wsdl2java命令会为每一个方法生成一个封装方法参数的类,类名为方法名(第一个字符大写),如uploadWithByte方法生成的类名为UploadWithByte。如果要设置file参数的值,可以使用UploadWithByte类的setFile方法,代码如下: UploadWithByte?uwb?
?UPloadWithByte();
uwb.setFile(dh); 最后是调用uploadWithByte方法,代码如下(FileServiceStub为wsdl2java生成的stub类名): FileServiceStub?fss?
?FileServiceStub();
fss.uploadWithByte(uwb);
如果使用C#调用FileService,则file参数类型均为byte[],代码如下: MemoryStream?ms?
?MemoryStream();
其中picUpdateImage
为c#
中加载图像文件的picturebox
控件。Bitmap?bitmap? ?Bitmap(picUpdateImage.Image); bitmap.Save(ms,?System.Drawing.Imaging.ImageFormat.Jpeg); service.fileService?fs? ?WSC.service.fileService(); fs.uploadWithDataHandler(ms.ToArray()); fs.uploadWithByte(ms.ToArray());? 下一篇: WebService大讲堂之Axis2(5):会话(Session)管理 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |