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)源代码
package com.yht.msg.p_w_upload;
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.p_w_uploads.Attachments;
import org.apache.axiom.p_w_uploads.ByteArrayDataSource;
import org.apache.axiom.p_w_uploads.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<String,String> contextTypeMap =
new HashMap<String,String>();
/**
* 文件输入流。
*/
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", "p_w_picpath/bmp");
contextTypeMap.put("gif", "p_w_picpath/gif");
contextTypeMap.put("jpg", "p_w_picpath/jpeg");
contextTypeMap.put("png", "p_w_picpath/png");
contextTypeMap.put("ico", "p_w_picpath/x-icon");
contextTypeMap.put("pic", "p_w_picpath/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 p_w_upload = new Attachments();
p_w_upload.addDataHandler(fileName, dataHandler);
return p_w_upload;
}
/**
* 读取指定文件的内容,将文件内容保存到一个byte[]的对象中。
* @param srcFile 需要读取的文件。
* @return 返回读取的byte[]型数据。
*/
private byte[] readByteList(File srcFile)
{
//获取文件的长度。
int length = (int)(srcFile.length());
byte[] byteList = null;
//打开文件输入流,并获取文件输入流的通道。
try
{
inputStream = new FileInputStream(srcFile);
fileChannel = inputStream.getChannel();
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//定义一个字节模板,将文件通道中的数据映射到模板中。
ByteBuffer dst = ByteBuffer.allocate(length);
try
{
fileChannel.read(dst);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//关闭输入流和文件通道。
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)
{
try
{
fileChannel.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
2、客户端携带附件
(1)重新打包AttachService-Axis2-1.6.2.jar
为了,是客户端能携带附件,需要在客户端类中增加携带附件的能力。因此,修改客户端类SendAttachServiceStub,重新打出jar包AttachService-Axis2-1.6.2.jar。
(2)修改客户端类SendAttachServiceStub
一、找到客户端中,发送消息的主体方法(第一篇中接口定义的方法)。
public com.yht.msg.SendAttachResponse sendAttach(
com.yht.msg.SendAttach sendAttach14)
throws java.rmi.RemoteException
修改该方法:
a:增加附件入参方法该为:
public com.yht.msg.SendAttachResponse sendAttach(
com.yht.msg.SendAttach sendAttach14, Attachments p_w_uploads)
throws java.rmi.RemoteException
b:消息中增加附件
方法中org.apache.axis2.client.OperationClient类型的对象_operationClient,增加消息内容前(_operationClient.addMessageContext(_messageContext);),加入附件。如下代码:
if(p_w_uploads != null)
{
_operationClient.getOptions().setProperty("enableSwA", "true");
_messageContext.setAttachmentMap(p_w_uploads);
}
// add the message contxt to the operation client
_operationClient.addMessageContext(_messageContext);
二、该方法前新增一个方法(原有不带附件的方法):
public SendAttachResponse sendAttach(SendAttach sendAttach14)
throws java.rmi.RemoteException
{
return sendAttach(sendAttach14, null);
}