天天看点

Axis2用法:soap消息携带附件

soap消息携带附件时,soap消息需要携带符合axis2规范的文件。那么我们需要将普通的文件转化成符合axis2的附件对象的类Attachments。这里主要提供一个File类型的对象转化成Attachments类型的对象的方法。

一、判断输入文件是否是标准文件,如果非标准文件直接返回null。

二、根据文件后缀,获取文件内容的类型。默认为txt文件的内容类型。

三、将文件数据已byte[]的形式读到ConfigurableDataHandler类型的对象dataHandler中。

四、设置数据对象dataHandler的数据内容的类型和文件内容的编码方式。

五、new一个附件类Attachments,调用方法addDataHandler。

txt、mp3、wav、book、m4p、bmp、gif、jpg、 png、ico、pic、avi、mp4、mpeg、mp2、wmv、wvx、mv、smil、3gp、3gp2

<code>&lt;br&gt;</code>

package com.yht.msg.attachment;  

import java.io.File;  

import java.io.FileInputStream;  

import java.io.FileNotFoundException;  

import java.io.IOException;  

import java.nio.ByteBuffer;  

import java.nio.channels.FileChannel;  

import java.util.HashMap;  

import java.util.Map;  

import org.apache.axiom.attachments.Attachments;  

import org.apache.axiom.attachments.ByteArrayDataSource;  

import org.apache.axiom.attachments.ConfigurableDataHandler;  

/** 

 * 提供将文件File转化成符合axis2规范的附件的方法。 

 * axis2提供的携带附件对象时Attachments类型,那么我们如何将文件File转化成Attachments再 

 * 放到soap消息中发送出去。支持的文件后缀名有:txt、mp3、wav、book、m4p、bmp、gif、jpg、 

 * png、ico、pic、avi、mp4、mpeg、mp2、wmv、wvx、mv、smil、3gp、3gp2。 

 * @author Administrator 

 * 

 */  

public class File2Attachments   

{  

    /** 

     * 文件内容的编码方式。 

     */  

    public static final String TRANSFERENCODING = "8bit";  

     * 文件内容的类型。 

    public static final Map&lt;String,String&gt; contextTypeMap =   

            new HashMap&lt;String,String&gt;();  

     * 文件输入流。 

    private FileInputStream inputStream;  

     * 文件输入流通道。 

    private FileChannel fileChannel;  

     * 构造函数,根据文件的后缀初始化文件内容的类型。 

    public File2Attachments()  

    {  

        contextTypeMap.put("txt", "text/plain;charset=UTF-8");  

        contextTypeMap.put("mp3", "audio/mpeg");  

        contextTypeMap.put("wav", "audio/wav");  

        contextTypeMap.put("book", "audio/x-m4b");  

        contextTypeMap.put("m4p", "audio/x-m4b");  

        contextTypeMap.put("bmp", "image/bmp");  

        contextTypeMap.put("gif", "image/gif");  

        contextTypeMap.put("jpg", "image/jpeg");  

        contextTypeMap.put("png", "image/png");  

        contextTypeMap.put("ico", "image/x-icon");  

        contextTypeMap.put("pic", "image/pict");  

        contextTypeMap.put("avi", "video/avs-video");  

        contextTypeMap.put("mp4", "video/mp4");  

        contextTypeMap.put("mpeg", "video/mpeg");  

        contextTypeMap.put("mp2", "video/x-mpeq2a");  

        contextTypeMap.put("wmv", "video/x-ms-wmv");  

        contextTypeMap.put("wvx", "video/x-ms-wvx");  

        contextTypeMap.put("mv", "video/x-sgi-movie");  

        contextTypeMap.put("smil", "application/smil");  

        contextTypeMap.put("3gp", "video/3gpp");  

        contextTypeMap.put("3gp2", "video/3gpp2");  

    }  

     * 将File文件转化成符合soap传输的Attachments的附件。 

     * @param srcFile 目标文件。 

     * @return 符合soap传输的Attachments的附件。 

    public Attachments file2Attach(File srcFile)  

        //判断输入文件是否是标准文件,如果非标准文件直接返回null。  

        if(!srcFile.isFile())  

        {  

            return null;  

        }  

        String contentType = null;  

        byte[] byteList = null;  

        String fileName = srcFile.getName();  

        //根据文件后缀,获取文件内容的类型。默认为txt文件的内容类型。  

        String fileType = getFileType(fileName);  

        contentType = contextTypeMap.get(fileType);  

        if(contentType == null)  

            contentType = "text/plain;charset=UTF-8";  

        //将文件数据已byte[]的形式读到对象dataHandler中。  

        byteList = readByteList(srcFile);  

        ByteArrayDataSource source = new ByteArrayDataSource(byteList);  

        ConfigurableDataHandler dataHandler = new ConfigurableDataHandler(source);  

        //设置数据对象dataHandler的数据内容的类型和文件内容的编码方式。  

        dataHandler.setContentType(contentType);  

        dataHandler.setTransferEncoding(TRANSFERENCODING);  

        //生成附件。  

        Attachments attachment = new Attachments();  

        attachment.addDataHandler(fileName, dataHandler);  

        return attachment;  

     * 读取指定文件的内容,将文件内容保存到一个byte[]的对象中。 

     * @param srcFile 需要读取的文件。 

     * @return 返回读取的byte[]型数据。 

    private byte[] readByteList(File srcFile)  

        //获取文件的长度。  

        int length = (int)(srcFile.length());  

        //打开文件输入流,并获取文件输入流的通道。  

        try  

            inputStream = new FileInputStream(srcFile);  

            fileChannel = inputStream.getChannel();  

        catch (FileNotFoundException e)  

            // TODO Auto-generated catch block  

            e.printStackTrace();  

        //定义一个字节模板,将文件通道中的数据映射到模板中。  

        ByteBuffer dst = ByteBuffer.allocate(length);  

            fileChannel.read(dst);  

        catch (IOException e)  

        //关闭输入流和文件通道。  

        close();  

        //从数据模板中获取字节型数据。  

        byteList = dst.array();  

        return byteList;  

     * 根据文件名,获取文件的后缀。 

     * 将文件名以“.”号分割,取分割后的字符串数组的最后一个字符串。默认为“txt”。 

     * @param fileName 文件名。 

     * @return 文件的后缀。 

    private String getFileType(String fileName)  

        String fileType = "txt";  

        String[] stringList = fileName.split("\\.");  

        if(stringList != null)  

            int size = stringList.length;  

            fileType = stringList[size - 1];  

        return fileType;  

     * 关闭文件输入流和文件映射通道。 

    private void close()  

        if(inputStream != null)  

            try  

            {  

                inputStream.close();  

            }  

            catch (IOException e)  

                // TODO Auto-generated catch block  

                e.printStackTrace();  

        if(fileChannel != null)  

                fileChannel.close();  

}  

为了,是客户端能携带附件,需要在客户端类中增加携带附件的能力。因此,修改客户端类SendAttachServiceStub,重新打出jar包AttachService-Axis2-1.6.2.jar。

一、找到客户端中,发送消息的主体方法(第一篇中接口定义的方法)。

public com.yht.msg.SendAttachResponse sendAttach(  

com.yht.msg.SendAttach sendAttach14)  

throws java.rmi.RemoteException  

修改该方法:

a:增加附件入参方法该为:

com.yht.msg.SendAttach sendAttach14, Attachments attachments)  

b:消息中增加附件

方法中org.apache.axis2.client.OperationClient类型的对象_operationClient,增加消息内容前(_operationClient.addMessageContext(_messageContext);),加入附件。如下代码:

if(attachments != null)  

    _operationClient.getOptions().setProperty("enableSwA", "true");  

    _messageContext.setAttachmentMap(attachments);  

// add the message contxt to the operation client  

_operationClient.addMessageContext(_messageContext);  

二、该方法前新增一个方法(原有不带附件的方法):

public SendAttachResponse sendAttach(SendAttach sendAttach14)  

        throws java.rmi.RemoteException   

    return sendAttach(sendAttach14, null);  

本文转自 www19 51CTO博客,原文链接:http://blog.51cto.com/doujh/1706161,如需转载请自行联系原作者