惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Help Net Security
Help Net Security
G
Google Developers Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
F
Full Disclosure
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
V
V2EX
C
Cisco Blogs
博客园 - 司徒正美
Project Zero
Project Zero
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
S
Securelist
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
T
Threatpost
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
S
Security Archives - TechRepublic
博客园 - Franky
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
A
Arctic Wolf
B
Blog

博客园 - netshuai

免安装Oracle客户端使用PL/SQL连接Oracle的2种方法 优化MyEclipse7.5 WebService大讲堂之Axis2(9):编写Axis2模块(Module) WebService大讲堂之Axis2(10):使用soapmonitor模块监视soap请求与响应消息 WebService大讲堂之Axis2(7):将Spring的装配JavaBean发布成WebService WebService大讲堂之Axis2(8):异步调用WebService WebService大讲堂之Axis2(5):会话(Session)管理 WebService大讲堂之Axis2(6):跨服务会话(Session)管理 WebService大讲堂之Axis2(3):使用services.xml文件发布WebService WebService大讲堂之Axis2(2):复合类型数据的传递 (转)WebService大讲堂之Axis2(1):用POJO实现0配置的WebService J2EE项目代码编写规范 Dom4j入门 编码 GBK 的不可映射字符 ognl表达式用法笔记 - netshuai - 博客园 struts2中减少action数量(通配符使用) struts2校验框架之Visitor校验器 struts2.1.6 datetimepicker标签不能使用的问题解决 加快MyEclipse启动速度
WebService大讲堂之Axis2(4):二进制文件传输
netshuai · 2009-09-20 · via 博客园 - netshuai

    在《WebService大讲堂之Axis2(2):复合类型数据的传递》中讲过,如果要传递二进制文件(如图像、音频文件等),可以使用byte[]作为数据类型进行传递,然后客户端使用RPC方式进行调用。这样做只是其中的一种方法,除此之外,在客户端还可以使用wsdl2java命令生成相应的stub类来调用WebServicewsdl2java命令的用法详见《WebService大讲堂之Axis2(1):用POJO实现0配置的WebService》。
    WebService类中包含byte[]类型参数的方法在wsdl2java生成的stub类中对应的数据类型不再是byte[]类型,而是javax.activation.DataHandlerDataHandler类是专门用来映射WebService二进制类型的。
    在
WebService类中除了可以使用byte[]作为传输二进制的数据类型外,也可以使用javax.activation.DataHandler作为数据类型。 不管是使用byte[],还是使用javax.activation.DataHandler作为WebService方法的数据类型,使用wsdl2java命令生成的stub类中相应方法的类型都是javax.activation.DataHandler。而象使用.netdelphi生成的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;
    }
}

上面代码在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>


    如果使用
wsdl2java命令生成调用Java客户端代码,则需要创建DataHandler类的对象实例,代码如下:

DataHandler dh = new DataHandler(new FileDataSource(imagePath));

wsdl2java命令会为每一个方法生成一个封装方法参数的类,类名为方法名(第一个字符大写),如uploadWithByte方法生成的类名为UploadWithByte。如果要设置file参数的值,可以使用UploadWithByte类的setFile方法,代码如下:

UploadWithByte uwb = new UPloadWithByte();
uwb.setFile(dh);

最后是调用uploadWithByte方法,代码如下(FileServiceStubwsdl2java生成的stub类名):

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());
 

其中picUpdateImagec#中加载图像文件的picturebox控件。