WebService Axis2 MTOM大数据传输实现

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();
    }
}

你可能感兴趣的:(webservice,java,webservice,java)