WebService Axis2 MTOM大数据传输实现
2016-04-21 14:34
381 查看
WebService大数据传输原理采用MTOM(消息优化机制)处理。大概原理是二进制文件与SOAP报文分开,SOAP报文body里面引用文件二进制(跟邮件传输机制差不多)。
MTOM服务端代码为
@MTOM //服务的标志MTOM协议发布
@WebService
public class MTOMServer {
/**
* 下载文件
*
* @return 数据句柄
*/
@XmlMimeType("application/octet-stream") //流形式返回
public DataHandler download(@WebParam(name = "fileName") String fileName) {
System.out.println(".......");
return new DataHandler(
new FileDataSource(new File("F:\\os\\win7.rar")));
}
public static void main(String[] args) {
Endpoint.publish("http://localhost:45600/file", new MTOMServer());
}
}
axis2客户端代码为
public class WebServiceTest {
public static void main(String[] args) throws XMLStreamException {
try {
ServiceClient wsClient = new ServiceClient();
Options clientOptions = new Options();
clientOptions.setTo(new EndpointReference("http://localhost:45600/file"));
clientOptions.setAction("http://test.component.access.sunsheen.com/");
//开启MTOM协议
clientOptions.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
//设置缓存目录,不然MTOM会加载所有二进制就内存,或内存溢出。
clientOptions.setProperty(Constants.Configuration.CACHE_ATTACHMENTS,Constants.VALUE_TRUE);
clientOptions.setProperty(Constants.Configuration.ATTACHMENT_TEMP_DIR,"d:/demo");
clientOptions.setProperty(Constants.Configuration.FILE_SIZE_THRESHOLD, "102400");
wsClient.setOptions(clientOptions);
OMFactory fac = OMAbstractFactory.getOMFactory();
OMElement method = fac.createOMElement("download","http://test.component.access.sunsheen.com/","test");
OMElement params = fac.createOMElement("fileName",null);
params.setText("xx");
method.addChild(params);
OMElement response;
System.out.println(method.toString());
response = wsClient.sendReceive(method);
OMElement dataElement = response.getFirstElement();
OMText binNode = (OMText) dataElement.getFirstOMChild();
DataHandler dataHandler = (DataHandler) binNode.getDataHandler();
InputStream in = dataHandler.getInputStream();
FileOutputStream fos = new FileOutputStream("e:/e.txt");
byte[] offer = new byte[1024];
while (in.read(offer) != -1) {
fos.write(offer);
}
in.close();
fos.close();
} catch (AxisFault e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//
// try {
// test2();
// } catch (IOException e) {
// // TODO 自动生成的 catch 块
// e.printStackTrace();
// }
}
}
还有种方式是通过生成代码客服端进行访问
1.先用wsimport 生成客服端
2.运行代码
public class TestClient {
public static void main(String[] args) throws IOException {
MTOMServerService s = new MTOMServerService();
MTOMServer s1 = s.getMTOMServerPort();
DataHandler x = s1.download("xx");
InputStream in = x.getInputStream();
FileOutputStream fos = new FileOutputStream("e:/e.txt");
byte[] offer = new byte[1024];
while (in.read(offer) != -1) {
fos.write(offer);
}
in.close();
fos.close();
}
}
MTOM服务端代码为
@MTOM //服务的标志MTOM协议发布
@WebService
public class MTOMServer {
/**
* 下载文件
*
* @return 数据句柄
*/
@XmlMimeType("application/octet-stream") //流形式返回
public DataHandler download(@WebParam(name = "fileName") String fileName) {
System.out.println(".......");
return new DataHandler(
new FileDataSource(new File("F:\\os\\win7.rar")));
}
public static void main(String[] args) {
Endpoint.publish("http://localhost:45600/file", new MTOMServer());
}
}
axis2客户端代码为
public class WebServiceTest {
public static void main(String[] args) throws XMLStreamException {
try {
ServiceClient wsClient = new ServiceClient();
Options clientOptions = new Options();
clientOptions.setTo(new EndpointReference("http://localhost:45600/file"));
clientOptions.setAction("http://test.component.access.sunsheen.com/");
//开启MTOM协议
clientOptions.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
//设置缓存目录,不然MTOM会加载所有二进制就内存,或内存溢出。
clientOptions.setProperty(Constants.Configuration.CACHE_ATTACHMENTS,Constants.VALUE_TRUE);
clientOptions.setProperty(Constants.Configuration.ATTACHMENT_TEMP_DIR,"d:/demo");
clientOptions.setProperty(Constants.Configuration.FILE_SIZE_THRESHOLD, "102400");
wsClient.setOptions(clientOptions);
OMFactory fac = OMAbstractFactory.getOMFactory();
OMElement method = fac.createOMElement("download","http://test.component.access.sunsheen.com/","test");
OMElement params = fac.createOMElement("fileName",null);
params.setText("xx");
method.addChild(params);
OMElement response;
System.out.println(method.toString());
response = wsClient.sendReceive(method);
OMElement dataElement = response.getFirstElement();
OMText binNode = (OMText) dataElement.getFirstOMChild();
DataHandler dataHandler = (DataHandler) binNode.getDataHandler();
InputStream in = dataHandler.getInputStream();
FileOutputStream fos = new FileOutputStream("e:/e.txt");
byte[] offer = new byte[1024];
while (in.read(offer) != -1) {
fos.write(offer);
}
in.close();
fos.close();
} catch (AxisFault e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//
// try {
// test2();
// } catch (IOException e) {
// // TODO 自动生成的 catch 块
// e.printStackTrace();
// }
}
}
还有种方式是通过生成代码客服端进行访问
1.先用wsimport 生成客服端
2.运行代码
public class TestClient {
public static void main(String[] args) throws IOException {
MTOMServerService s = new MTOMServerService();
MTOMServer s1 = s.getMTOMServerPort();
DataHandler x = s1.download("xx");
InputStream in = x.getInputStream();
FileOutputStream fos = new FileOutputStream("e:/e.txt");
byte[] offer = new byte[1024];
while (in.read(offer) != -1) {
fos.write(offer);
}
in.close();
fos.close();
}
}
相关文章推荐
- HDU 1848 Fibonacci again and again SG函数详解!
- socket服务器开发中的SO_REUSEADDR选项与让人心烦的TIME_WAIT
- 人工智能与信息爆炸
- 关于未来的目标以及 对人工智能的一些幻想
- GoLang WaitGroup使用
- mac没有安装airport卡以及win7无法上网
- drop表提示Waiting for table metadata lock 解决
- NSBundle 的理解和 mainBundle 类方法详解,nsbundlemainbundle
- “阿尔法狗”能成功挑战“星际”吗?
- C#:几种数据库的大数据批量插入
- hdfs的基本原理和基本操作总结
- saltstack(三) grains、pillar的使用
- 对于mysql表大数据量迁移
- 无法解决 equal to 运算中 "Chinese_PRC_CI_AS" 和 "Chinese_PRC_90_CI_AI" 之间的排序规则冲突。的解决方法
- Failed to install *.apk on device 'emulator-5554': timeout
- Android编程中利用AudioTrack播放PCM数据在音频的最后出现重复回声现象的解决方案
- 只有MainActivity怎么够?教你如何启动另一个Activity
- 《Batch Normalization Accelerating Deep Network Training by Reducing Internal Covariate Shift》阅读笔记与实现
- 70. Climbing Stairs
- CDISC SDTM PR domain 学习笔记