1、附件类File2Attachments

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

(1)分装过程

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

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

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

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

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

(2)支持的文件

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

(3)源代码


  1. package com.yht.msg.p_w_upload;  

  2.   

  3. import java.io.File;  

  4. import java.io.FileInputStream;  

  5. import java.io.FileNotFoundException;  

  6. import java.io.IOException;  

  7. import java.nio.ByteBuffer;  

  8. import java.nio.channels.FileChannel;  

  9. import java.util.HashMap;  

  10. import java.util.Map;  

  11.   

  12. import org.apache.axiom.p_w_uploads.Attachments;  

  13. import org.apache.axiom.p_w_uploads.ByteArrayDataSource;  

  14. import org.apache.axiom.p_w_uploads.ConfigurableDataHandler;  

  15.   

  16. /** 

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

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

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

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

  21.  * @author Administrator 

  22.  * 

  23.  */  

  24. public class File2Attachments   

  25. {  

  26.     /** 

  27.      * 文件内容的编码方式。 

  28.      */  

  29.     public static final String TRANSFERENCODING = "8bit";  

  30.       

  31.     /** 

  32.      * 文件内容的类型。 

  33.      */  

  34.     public static final Map<String,String> contextTypeMap =   

  35.             new HashMap<String,String>();  

  36.       

  37.     /** 

  38.      * 文件输入流。 

  39.      */  

  40.     private FileInputStream inputStream;  

  41.       

  42.     /** 

  43.      * 文件输入流通道。 

  44.      */  

  45.     private FileChannel fileChannel;  

  46.       

  47.     /** 

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

  49.      */  

  50.     public File2Attachments()  

  51.     {  

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

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

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

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

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

  57.         contextTypeMap.put("bmp""p_w_picpath/bmp");  

  58.         contextTypeMap.put("gif""p_w_picpath/gif");  

  59.         contextTypeMap.put("jpg""p_w_picpath/jpeg");  

  60.         contextTypeMap.put("png""p_w_picpath/png");  

  61.         contextTypeMap.put("ico""p_w_picpath/x-icon");  

  62.         contextTypeMap.put("pic""p_w_picpath/pict");  

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

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

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

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

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

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

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

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

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

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

  73.     }  

  74.       

  75.     /** 

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

  77.      * @param srcFile 目标文件。 

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

  79.      */  

  80.     public Attachments file2Attach(File srcFile)  

  81.     {  

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

  83.         if(!srcFile.isFile())  

  84.         {  

  85.             return null;  

  86.         }  

  87.           

  88.         String contentType = null;  

  89.         byte[] byteList = null;  

  90.         String fileName = srcFile.getName();  

  91.           

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

  93.         String fileType = getFileType(fileName);  

  94.         contentType = contextTypeMap.get(fileType);  

  95.         if(contentType == null)  

  96.         {  

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

  98.         }  

  99.           

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

  101.         byteList = readByteList(srcFile);  

  102.         ByteArrayDataSource source = new ByteArrayDataSource(byteList);  

  103.         ConfigurableDataHandler dataHandler = new ConfigurableDataHandler(source);  

  104.           

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

  106.         dataHandler.setContentType(contentType);  

  107.         dataHandler.setTransferEncoding(TRANSFERENCODING);  

  108.           

  109.         //生成附件。  

  110.         Attachments p_w_upload = new Attachments();  

  111.         p_w_upload.addDataHandler(fileName, dataHandler);  

  112.         return p_w_upload;  

  113.     }  

  114.       

  115.     /** 

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

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

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

  119.      */  

  120.     private byte[] readByteList(File srcFile)  

  121.     {  

  122.         //获取文件的长度。  

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

  124.         byte[] byteList = null;  

  125.           

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

  127.         try  

  128.         {  

  129.             inputStream = new FileInputStream(srcFile);  

  130.             fileChannel = inputStream.getChannel();  

  131.         }  

  132.         catch (FileNotFoundException e)  

  133.         {  

  134.             // TODO Auto-generated catch block  

  135.             e.printStackTrace();  

  136.         }  

  137.           

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

  139.         ByteBuffer dst = ByteBuffer.allocate(length);  

  140.         try  

  141.         {  

  142.             fileChannel.read(dst);  

  143.         }  

  144.         catch (IOException e)  

  145.         {  

  146.             // TODO Auto-generated catch block  

  147.             e.printStackTrace();  

  148.         }  

  149.           

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

  151.         close();  

  152.           

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

  154.         byteList = dst.array();  

  155.         return byteList;  

  156.     }  

  157.       

  158.     /** 

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

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

  161.      * @param fileName 文件名。 

  162.      * @return 文件的后缀。 

  163.      */  

  164.     private String getFileType(String fileName)  

  165.     {  

  166.         String fileType = "txt";  

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

  168.         if(stringList != null)  

  169.         {  

  170.             int size = stringList.length;  

  171.             fileType = stringList[size - 1];  

  172.         }  

  173.           

  174.         return fileType;  

  175.     }  

  176.       

  177.     /** 

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

  179.      */  

  180.     private void close()  

  181.     {  

  182.         if(inputStream != null)  

  183.         {  

  184.             try  

  185.             {  

  186.                 inputStream.close();  

  187.             }  

  188.             catch (IOException e)  

  189.             {  

  190.                 // TODO Auto-generated catch block  

  191.                 e.printStackTrace();  

  192.             }  

  193.         }  

  194.           

  195.         if(fileChannel != null)  

  196.         {  

  197.             try  

  198.             {  

  199.                 fileChannel.close();  

  200.             }  

  201.             catch (IOException e)  

  202.             {  

  203.                 // TODO Auto-generated catch block  

  204.                 e.printStackTrace();  

  205.             }  

  206.         }  

  207.     }  

  208. }  


2、客户端携带附件

(1)重新打包AttachService-Axis2-1.6.2.jar

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


(2)修改客户端类SendAttachServiceStub

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


  1. public com.yht.msg.SendAttachResponse sendAttach(  

  2.   

  3. com.yht.msg.SendAttach sendAttach14)  

  4.   

  5. throws java.rmi.RemoteException  


修改该方法:

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

  1. public com.yht.msg.SendAttachResponse sendAttach(  

  2.   

  3. com.yht.msg.SendAttach sendAttach14, Attachments p_w_uploads)  

  4.   

  5. throws java.rmi.RemoteException  


b:消息中增加附件

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

  1. if(p_w_uploads != null)  

  2. {  

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

  4.     _messageContext.setAttachmentMap(p_w_uploads);  

  5. }  

  6.   

  7. // add the message contxt to the operation client  

  8. _operationClient.addMessageContext(_messageContext);  


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

  1. public SendAttachResponse sendAttach(SendAttach sendAttach14)  

  2.         throws java.rmi.RemoteException   

  3.         {  

  4.     return sendAttach(sendAttach14, null);  

  5. }