我正在尝试使用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);

最佳答案

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

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

10-05 22:04