1. 简单认识MTOM
MTOM: Message Transmission Optimization Mechanism W3C消息传输优化机制
以JAX-WS使用此技术为例,讲client端和server端之间传送图片
传统的图片传输,一般是采用BASE64编码传输,传输体积很大。采用MTOM,会将图片作为MIME格式消息发送,即原始的二进制图片传输,放在header中传输。
2. 实践
2.1 客户端请求图片,server端以MTOM机制发送图片
server 端enable MTOM,在webservice实现类上添加注解@MTOM
@MTOM
@WebService(endpointInterface = "com.mkyong.ws.ImageServer")
public class ImageServerImpl implements ImageServer{
部分response 格式
--uuid:c73c9ce8-6e02-40ce-9f68-064e18843428
Content-Id: <rootpart*c73c9ce8-6e02-40ce-9f68-064e18843428@example.jaxws.sun.com>
Content-Type: application/xop+xml;charset=utf-8;type="text/xml"
Content-Transfer-Encoding: binary
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:downloadImageResponse xmlns:ns2="http://ws.mkyong.com/">
<return>
<xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include"
href="cid:012eb00e-9460-407c-b622-1be987fdb2cf@example.jaxws.sun.com">
</xop:Include>
</return>
</ns2:downloadImageResponse>
</S:Body>
</S:Envelope>
--uuid:c73c9ce8-6e02-40ce-9f68-064e18843428
Content-Id: <012eb00e-9460-407c-b622-1be987fdb2cf@example.jaxws.sun.com>
Content-Type: image/png
Content-Transfer-Encoding: binary
Binary data here.............
2.2 客户端以MTOM机制发送图片到server
client端enable MTOM
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8888/ws/image?wsdl");
QName qname = new QName("http://ws.mkyong.com/", "ImageServerImplService");
Service service = Service.create(url, qname);
ImageServer imageServer = service.getPort(ImageServer.class);
/************ test upload ****************/
Image imgUpload = ImageIO.read(new File("c:\\images\\rss.png"));
//enable MTOM in client
BindingProvider bp = (BindingProvider) imageServer;
SOAPBinding binding = (SOAPBinding) bp.getBinding();
binding.setMTOMEnabled(true);
String status = imageServer.uploadImage(imgUpload);
System.out.println("imageServer.uploadImage() : " + status);
}
refer : http://www.mkyong.com/webservices/jax-ws/jax-ws-attachment-with-mtom/