Message Transmission Optimization Mechanism (MTOM)——向或从 web services 高效地发送二进制数据的一种办法。
要在 JAX-WS services 中使用 MTOM,首先,需要使用 BindingType 注解将 binding type 设置为 SOAP11 MTOM。参数和返回类型要用 javax.activation.DataHandler 来表示你的二进制数据。
让我们看一个利用了MTOM的JAX-WS service。
@WebService(serviceName = "MTOMSampleService", targetNamespace = "http://mtom.jaxws.apache.org")
@BindingType(value = SOAPBinding.SOAP11HTTP_MTOM_BINDING)
public class MTOMService {
@WebMethod(action = "urn:uploadFile")
public String uploadFile(DataHandler data) {
try {
InputStream is = data.getInputStream();
String msg = "File " + data.getName() + " of type " +
data.getContentType() + " successfully received";
return msg;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@WebMethod(action = "urn:getTestData")
public DataHandler getBinaryTestData(String stmtId) {
byte[] testData = new byte[10240];
for (int i = 0; i < testData.length; i++) {
testData[i] = 0x7f;
}
ByteArrayDataSource bds =
new ByteArrayDataSource(testData, "application/octet-stream");
return new DataHandler(bds);
}
}
Java bean type classes can also contain DataHandlers.
![](/qrcode.jpg)