java跨系统调用webservice接口,xml形式传输

//请求webservice 的接口处理
    @Override
public net.sf.json.JSONObject proSalePrice(String url, QueryListDto queryDto) throws Exception {

       // String url = "http://10.1.225.155:80/SPH_MOBILE_ERP/salePrice/ProxyServices/selectSalePrice_PS?wsdl";
        net.sf.json.JSONObject rt = null;
        //第一步:创建服务地址,不是WSDL地址  
        URL path = new URL(url);  
        //第二步:打开一个通向服务地址的连接  
        HttpURLConnection connection = (HttpURLConnection) path.openConnection();  
        //第三步:设置参数  
        //3.1发送方式设置:POST必须大写  
        connection.setRequestMethod("POST");  
        //3.2设置数据格式:content-type  
        connection.setRequestProperty("content-type", "text/xml;charset=utf-8");  
        //3.3设置输入输出,因为默认新创建的connection没有读写权限,  
        connection.setDoInput(true);  
        connection.setDoOutput(true);  
 
        //第四步:组织SOAP数据,发送请求  
        //InputStream in = this.getClass().getResourceAsStream("salePrice.xml");//本地测试可以
        InputStream in1 = ProductServiceImpl.class.getClassLoader().getResourceAsStream("./webService/salePrice.xml");//本地测试可以 ,服务器不可以
        InputStream in2 = ProductServiceImpl.class.getClassLoader().getResourceAsStream("/webService/salePrice.xml"); //本地测试不可以 ,服务器可以,通过CLASSPATH读取包内文件,注意以“/”开头
        InputStream in = new ClassPathResource("webService/salePrice.xml").getInputStream();//本地测试可以 ,服务器可以
        System.out.println("ProductServiceImpl.class.getClassLoader().getResourceAsStream(\"./webService/salePrice.xml\") :"+in1);
        System.out.println("ProductServiceImpl.class.getClassLoader().getResourceAsStream(\"/webService/salePrice.xml\") :"+in2);
        System.out.println("new ClassPathResource(\"webService/salePrice.xml\").getInputStream() :"+in);
        //ServletContext().getRealPath("路径名A");好像这里不可以用此方法
        String path2 = ProductServiceImpl.class.getClassLoader().getResource("").getPath();
         System.out.println("ProductServiceImpl.class.getClassLoader().getResource(\"\").getPath():"+path);
        // license.xml应放在..\WebRoot\WEB-INF\classes路径下
       // InputStream is = Convert2PdfUtil.class.getClassLoader().getResourceAsStream("license.xml");
          
        byte[] data = this.toByteArray(in);
        System.out.println("this.toByteArray(in): "+data);
        String soapXML = new String(data);
        System.out.println("proSalePrice请求头:"+ soapXML);
         Map<String, String> param_map = queryDto.getAttr();
         String al_party_id = param_map.get("al_party_id");
         String al_goods_id = param_map.get("al_goods_id");
         String al_client_id = param_map.get("al_client_id");
         String al_payment_day = param_map.get("al_payment_day");
         String dbSource = param_map.get("dbSource");
         soapXML = soapXML.replaceAll("#al_party_id", al_party_id)
                 .replaceAll("#al_goods_id", al_goods_id)
                 .replaceAll("#al_client_id", al_client_id)
                 .replaceAll("#al_payment_day", al_payment_day)
                 .replaceAll("#dbSource", dbSource);
        OutputStream os = connection.getOutputStream();
        os.write(soapXML.getBytes());  
        //第五步:接收服务端响应,打印  
        int responseCode = connection.getResponseCode();  
        if(200 == responseCode){//表示服务端响应成功  
            InputStream is = connection.getInputStream();  
            InputStreamReader isr = new InputStreamReader(is);  
            BufferedReader br = new BufferedReader(isr);  
              
            StringBuilder sb = new StringBuilder();  
            String temp = null;  
            while(null != (temp = br.readLine())){  
                sb.append(temp);  
            }
            String resultStr = sb.toString();
            System.out.println(resultStr);
            JSONObject mapResult = this.xml2Json(resultStr);
            rt = net.sf.json.JSONObject.fromObject(mapResult);
            is.close();  
            isr.close();  
            br.close();
            System.out.println(rt);
            return rt;
        }  
        os.close();    
        return null;

    }



配置的xml(传输数据)

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://www.shaphar.com/SoapService">
   <soapenv:Header/>
   <soapenv:Body>
      <soap:REQUEST_DATA>
         <soap:commonHeader>
            <soap:BIZTRANSACTIONID>?</soap:BIZTRANSACTIONID>
            <soap:COUNT>?</soap:COUNT>
            <soap:CONSUMER>?</soap:CONSUMER>
            <soap:SRVLEVEL>?</soap:SRVLEVEL>
            <soap:ACCOUNT>?</soap:ACCOUNT>
            <soap:PASSWORD>?</soap:PASSWORD>
            <soap:COMMENTS>?</soap:COMMENTS>
         </soap:commonHeader>
         <soap:LIST>
            <soap:AL_PARTY_ID>#al_party_id</soap:AL_PARTY_ID>
            <soap:AL_GOODS_ID>#al_goods_id</soap:AL_GOODS_ID>
            <soap:AL_CLIENT_ID>#al_client_id</soap:AL_CLIENT_ID>
            <soap:AL_PAYMENT_DAY>#al_payment_day</soap:AL_PAYMENT_DAY>
            <soap:transSource>#dbSource</soap:transSource>
         </soap:LIST>
      </soap:REQUEST_DATA>
   </soapenv:Body>
</soapenv:Envelope>




  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您可以按照以下步骤在Java中实现通过Web服务接口传输压缩XML的ZIP文件: 1. 生成要传输XML文件并将其压缩为ZIP文件。 2. 创建一个Web服务接口,将ZIP文件作为输入参数传递到该接口。 3. 在Web服务接口的实现中,使用Java的压缩文件API将ZIP文件解压缩,并读取XML内容。 4. 对XML内容执行所需的操作,并将结果作为输出返回到Web服务接口调用方。 以下是一个基本的示例代码,用于在Java中实现通过Web服务接口传输压缩XML的ZIP文件: ``` @WebService public class MyWebService { @WebMethod public String processZipFile(byte[] zipFile) { // 解压缩ZIP文件 try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(zipFile); ZipInputStream zipInputStream = new ZipInputStream(byteArrayInputStream)) { ZipEntry entry = zipInputStream.getNextEntry(); if (entry != null) { // 读取XML内容 byte[] xmlBytes = new byte[(int) entry.getSize()]; zipInputStream.read(xmlBytes); // 对XML内容执行所需的操作 String result = processXml(xmlBytes); return result; } } catch (IOException e) { e.printStackTrace(); } return ""; } private String processXml(byte[] xmlBytes) { // 对XML内容执行所需的操作 return "XML processing result"; } } ``` 在此示例中,`processZipFile`方法接收一个`byte[]`类型的ZIP文件,使用Java的压缩文件API解压缩ZIP文件,并读取XML内容。然后对XML内容执行所需的操作,并将结果作为输出返回到Web服务接口调用方。 请注意,此示例仅用于演示目的,您需要根据自己的具体需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值