zoukankan      html  css  js  c++  java
  • (九)二进制文件在webservice中的处理(以DataHandler方式)

    一、需求

    • 1. 客户端从服务端下载附件
    • 2. 客户端上传附件到服务端

    二、案例

    • 本章通过DataHander的方式来进行传递。

           注意:   1:接口中要定义@MTOM

              2:方法中要使用@XmlMimeType(value = "application/octet-stream")
    • 服务端

      2.1  编写服务接口

    package com.webservice;
    
    import javax.activation.DataHandler;
    import javax.jws.WebParam;
    import javax.jws.WebResult;
    import javax.jws.WebService;
    import javax.xml.bind.annotation.XmlMimeType;
    import javax.xml.ws.soap.MTOM;
    
    /**
     * 设置AttachementPart中MIME的功能
     * 
     * 
     * 1:在接口中开启@MTOM注解。
     * 
     * 2:在方法中加上@XmlMimeType(value = "application/octet-stream")
     * 
     */
    @WebService
    @MTOM
    public interface IFileDataHandler {
        @WebResult(name = "sendServerImageResult")
        @XmlMimeType(value = "application/octet-stream")
        public DataHandler sendServerImage();
    
        public void receiveClientImage(@WebParam(name = "dataHandler")
        @XmlMimeType(value = "application/octet-stream")
        DataHandler dataHandler, @WebParam(name = "filename")
        String filename);
    }

      2.2  实现服务接口

    package com.webservice;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    
    import javax.activation.DataHandler;
    import javax.activation.DataSource;
    import javax.activation.FileDataSource;
    import javax.jws.WebService;
    
    import org.apache.commons.io.IOUtils;
    
    @WebService(endpointInterface = "com.webservice.IFileDataHandler")
    public class SendFileDataHandler implements IFileDataHandler {
    
        public DataHandler sendServerImage() {
            File file = new File("E:\server\jQuery1.4 API-20100204.chm");
    
            DataSource dataSource = new FileDataSource(file);
            DataHandler dataHandler = new DataHandler(dataSource);
            return dataHandler;
        }
    
        public void receiveClientImage(DataHandler dataHandler, String filename) {
            OutputStream outputStream = null;
            try {
                outputStream = new FileOutputStream("E:\server\" + filename + "");
                dataHandler.writeTo(outputStream);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                IOUtils.closeQuietly(outputStream);
            }
        }
    }

      2.3  发布服务

    package com.webservice;
    
    import javax.xml.ws.Endpoint;
    
    public class TestPublish {
        public static void main(String[] args) {
            Endpoint.publish("http://localhost:5050/sendDataHandler",
                    new SendFileDataHandler());
    
            System.out.println("服务发布成功...");
        }
    }
    •   客户端

      2.4  测试

    package com.file_handler;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import javax.activation.DataHandler;
    import javax.activation.DataSource;
    import javax.activation.FileDataSource;
    
    public class TestMain {
        public static void main(String[] args) {
            //TestMain.getServerFile();
    
            TestMain.sendClientFile();
        }
    
        /**
         * 接收服务端发送的文件
         */
        private static void getServerFile() {
            IFileDataHandler dataService = new SendFileDataHandlerService()
                    .getSendFileDataHandlerPort();
    
            DataHandler dataHandler = dataService.sendServerImage();
    
            try {
                dataHandler.writeTo(new FileOutputStream("E:\client\test.chm"));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    
        private static void sendClientFile() {
            IFileDataHandler dataService = new SendFileDataHandlerService()
                    .getSendFileDataHandlerPort();
            String filename = "Storm.zip";
    
            DataSource dataSource = new FileDataSource(new File("E:\client\"
                    + filename + ""));
            DataHandler dataHandler = new DataHandler(dataSource);
    
            dataService.receiveClientImage(dataHandler, filename);
        }
    
    }
  • 相关阅读:
    java 代码规范 sun 公司
    软引用、弱引用、虚引用
    socket
    httpURLConnection、URL、httpClient、httpPost、httpGet
    android service aidl 理解
    Python2.7-codecs
    Python2.7-textwrap
    Python2.7-StringIO和cStringIO
    Python2.7-difflib
    Python2.7-struct模块
  • 原文地址:https://www.cnblogs.com/shyroke/p/7659822.html
Copyright © 2011-2022 走看看