• java对接c++发布的webservice接口,其中参数类型有base64Binary格式(无需将图片数据转化为c++中的结构体)


    接口名称:

    std::string SendVehiclePass(std::string VehiclePassInfo, struct xsd__base64Binary
    PlatePicData, struct xsd__base64Binary CarPic1, struct xsd__base64Binary CarPic2, 
    struct xsd__base64Binary CarPic3)
    

    参数说明:

    VehiclePassInfo:过车信息详情 xml,具体如下:

    <?xml version="1.0" encoding=" UTF-8 " standalone="yes" ?>
    <ROOT>
      <KKMY>卡口密钥<KKMY>
      <KKBH>卡口编号</KKBH> 
      <JGSK>经过时刻</JGSK>
      <CDBH>车道编号</CDBH> 
      <HPHM>号牌号码</HPHM > 
      <HPYS>号牌颜色</HPYS > 
      <HPZL>号牌种类</HPZL> 
      <CLSD>车辆速度</CLSD> 
      <CWKC>车外廓长</CWKC> 
      <CSYS>车身颜色</CSYS> 
      <CLLX>车辆类型</CLLX> 
      <SSYF>实时与否</SSYF> 
      <CLZPP1>车辆主品牌</CLZPP2> 
      <CLZPP2>车辆子品牌</CLZPP2> 
      <CLNK>车辆年款</CLNK> 
      <ZJSAQDZT>主驾驶安全带状态</ZJSAQDZT> 
      <FJSAQDZT>副驾驶安全带状态</FJSAQDZT> 
      <ZJSZYBZT>主驾驶遮阳板状态</ZJSZYBZT> 
      <FJSZYBZT>副驾驶遮阳板状态</FJSZYBZT> 
      <SFDDH>是否打电话</SFDDH> 
      <SFHBC>是否黄标车</SFHBC> 
      <SFWXPC>是否危险品车</SFWXPC> 
      <SFYGJ>是否有挂件</SFYGJ>
      <CDFX>车道方向</CDFX>
    </ROOT>
    

    PlatePicData:车牌特征小图片信息,最大 20K,可以为空。**

    CarPic1:车辆图片1,最大 1.5M,不能为空。

    CarPic2:车辆图片2,最大 1.5M,可以为空。

    CarPic3:车辆图片3,最大 1.5M,可以为空。

    所有以上图片信息若超过规定大小请自行压缩。

    车辆图片按照从左到右依次赋值,如果有一张图片,则赋值到 CarPic1,有两张则依次赋值

    CarPic1,CarPic2,依此类推。PlatePicData:只存车牌小图片。

    struct xsd__base64Binary

    {

    unsigned char* __ptr;//图片数据缓冲区

    int __size;//图片数据大小

    };

    我使用的Hutool,就可以在无任何依赖的情况下,实现简便的WebService请求,需要引入的pom文件:

                <dependency>
                    <groupId>cn.hutool</groupId>
                    <artifactId>hutool-http</artifactId>
                    <version>5.4.3</version>
                </dependency>
    

    核心代码:

    	    String c1 = ImageUtil.imageToBase64(CarPic1);
    			// 新建客户端
                SoapClient client = SoapClient.create("接口url")//例如:http://127.0.0.1:8888
                        // 设置要请求的方法,传入对应的命名空间
                        .setMethod("SendVehiclePass", "http://tempuri.org/ns.xsd")
                        // 设置参数
                        .setParam("VehiclePassInfo", msg)
                        .setParam("PlatePicData", null)
                        .setParam("CarPic1", c1)
                        .setParam("CarPic2", null)
                        .setParam("CarPic3", null);
                // 发送请求,参数true表示返回一个格式化后的XML内容
                // 返回内容为XML字符串,可以配合XmlUtil解析这个响应
                String result = client.send(true);
                logger.info("返回的数据:{}",result);
    

    根据图片的url地址转化为Base64编码的字符串:

    /**
         * 将图片编码为base64
         * @param url 图片的url地址
         * @return 返回Base64编码过的字节数组字符串
         * @throws IOException
         */
        public static String imageToBase64(String url) throws IOException {
            URL urlContent = new URL(url);
            HttpURLConnection con = (HttpURLConnection)urlContent.openConnection();
            con.setRequestMethod("GET");
            con.setConnectTimeout(5 * 1000);
            InputStream inStream = con .getInputStream();//通过输入流获取图片数据
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[2048];
            int len = 0;
            while( (len=inStream.read(buffer)) != -1 ){
                outStream.write(buffer, 0, len);
            }
            inStream.close();
            byte[] data =  outStream.toByteArray();
            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(data);
        }
    

    亲测有效

  • 相关阅读:
    基于JWT用户认证方式(前后端分离)
    git推送时报错:hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing
    (fields.E210) Cannot use ImageField because Pillow is not installed....解决方法
    python3.6报错:AttributeError: 'str' object has no attribute 'decode'
    于在Python3.6.7 +Ubuntu16.04下安装channels报错
    docsify 搭建优雅项目文档
    SQLSERVER 系统表查询
    数据立方建立-如何实现一对多,多对多
    从BI分析角度,数据立方的建立方法
    MYSQL库内所有表名及表结构获取
  • 原文地址:https://www.cnblogs.com/jiangwang001/p/13735384.html
Copyright © 2020-2023  润新知