如何从SOAPEnvelope响应

时间:2017-09-08 10:59:45

标签: java soap axis

我正在尝试使用SOAP服务下载pdf文件。 wsdl导入存在一些问题,因此存根没有正确的方法,我试图使用Apache Axis服务调用方法下载文件。

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <ns2:getFileStreamResponse xmlns:ns2="http://spring.io/guides/gs-producing-web-service">
            <ns2:FileByteStream>L1VzZXJzL3BrdW1hci9Eb2N1bWVudHMvTmFyZW5kcmFTaW5naC5wZGY=</ns2:FileByteStream>
        </ns2:getFileStreamResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

这是我尝试使用的java代码。

  String SOAP_REQUEST = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
          "\t\t\t\t  xmlns:gs=\"http://spring.io/guides/gs-producing-web-service\">\n" +
          "   <soapenv:Header/>\n" +
          "   <soapenv:Body>\n" +
          "      <gs:getFileStreamRequest>\n" +
          "         <gs:id>12313</gs:id>\n" +
          "         <gs:token>Ratakkdajd</gs:token>\n" +
          "         <gs:path>/Users/pkumar/Documents/NarendraSingh.pdf</gs:path>\n" +
          "      </gs:getFileStreamRequest>\n" +
          "   </soapenv:Body>\n" +
          "</soapenv:Envelope>";

    String HOST_ADDRESS = "http://localhost:8080/ws";

    SOAPEnvelope resp = null;
    try {
        byte[] reqBytes = SOAP_REQUEST.getBytes();
        ByteArrayInputStream bis = new ByteArrayInputStream(reqBytes);
        StreamSource ss = new StreamSource(bis);
        MessageFactoryImpl messageFactory = new MessageFactoryImpl();
        SOAPMessage msg = messageFactory.createMessage();
        SOAPPart soapPart = msg.getSOAPPart();
        soapPart.setContent(ss);
        Service service = new Service();
        org.apache.axis.client.Call call = (org.apache.axis.client.Call)service.createCall();
        call.setTargetEndpointAddress(HOST_ADDRESS);
        call.setProperty(call.CHECK_MUST_UNDERSTAND, false);
        resp = call.invoke(((org.apache.axis.SOAPPart)soapPart).getAsSOAPEnvelope());
        byte[] output = resp.getBodyElements().get(0).toString().getBytes();
        return output;

    } catch (Exception ex) {
        throw new Exception(ex.getMessage());
    }

我找不到从byte[] FileByteStream获取SOAPEnvelope resp的方法。有没有人像这样使用SOAP api并创建文件。

如果wsdl导入工作正常,我可以使用此代码轻松下载文件

CountriesPortServiceLocator locator = new CountriesPortServiceLocator();
CountriesPort service = locator.getCountriesPortSoap11(new URL("http://localhost:8080/ws"));
GetFileStreamRequest request = new GetFileStreamRequest(123,
        "321323",
        "remote_file_path.pdf");
GetFileStreamResponse response = service.getFileStream(request);

byte[] fileStream = response.getFileByteStream();
new FileOutputStream("output.pdf").write(fileStream);

1 个答案:

答案 0 :(得分:0)

经过几天的努力,来自客户的好人帮我解决了问题。

SOAPEnvelope类支持DOM类似方法,我们可以使用它们导航到结果标记(fileByteStream),文本节点中的字符串表示实际上是编码Byte64,因此我们需要将其解码回来获取原始字节[]。

//Invoke the WebService.
SOAPEnvelope resp = call.invoke(((org.apache.axis.SOAPPart) soapPart).getAsSOAPEnvelope());
//Extract and decode the fileStream from the response
byte[] output = Base64.decode(resp.getBody().getElementsByTagName("FileByteStream").item(0).getLastChild().getNodeValue());