Вложение работает в SoapUI, но не в Java, используя SAAJ API? - PullRequest
0 голосов
/ 03 мая 2019

Я могу прикрепить zip-файл с точно таким же SOAP-запросом, используя SoapUI, но не с Java, использующим SAAJ Api.Вот SOAP-запрос, который я использую в SOAPUI и JAVA:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:dep="endpoint">
   <soapenv:Header/>
   </soapenv:Header>
   <soapenv:Body>

      <dep:projectName>projectName</dep:projectName>
     <dep:content>cid:myFile.zip</dep:content>

   </soapenv:Body>
</soapenv:Envelope>

Теперь вот код, который я использую в JAVA для создания SOAP-запроса и прикрепления файла.Код может успешно отправить запрос SOAP, но не может прикрепить к нему zip-файл.

        SOAPConnection connection = null;
        MessageFactory factory = null;
        SOAPMessage message = null;
        SOAPHeader header = null;
        SOAPPart soapPart = null;
        SOAPEnvelope envelope = null;
        SOAPElement headerElement = null;
        SOAPBody body = null;
        SOAPElement initialBody = null;
        SOAPElement projectName = null;
        SOAPElement version = null;
        AttachmentPart attachment = null;
        SOAPElement content = null;
        SOAPConnectionFactory soapConnectionFactory = null;
        MessageContext context = null;
        SOAPMessage response = null;
        URL endpoint = null;

        try {
        factory = MessageFactory.newInstance();
        message = factory.createMessage();
        header = message.getSOAPHeader();
        soapPart = message.getSOAPPart();

        //Namespaces
        envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration("dep", "NAMESPACE");


        //SOAP Body
        body = message.getSOAPBody();
        projectName = body.addChildElement("projectName", "dep");
        projectName.addTextNode("projectName");


        //Attachment
        File uploadFile = new File("myFile.zip");
        DataHandler dh = new DataHandler(new FileDataSource(uploadFile)); 
        attachment = message.createAttachmentPart(dh);

        //InputStream targetStream = new FileInputStream(uploadFile);
        //attachment.setRawContent(targetStream, Files.probeContentType(uploadFile.toPath()));

        System.out.println("The file size is " + attachment.getSize());
        attachment.setContentType("application/octet-stream");
        attachment.setContentId("myFile.zip");
        message.addAttachmentPart(attachment);

        content = initialBody.addChildElement("content","dep"); 
        content.addTextNode("cid:" + attachment.getContentId());

        message.saveChanges();

        //Response
        soapConnectionFactory = SOAPConnectionFactory.newInstance();
        connection = soapConnectionFactory.createConnection(); 
        endpoint = new URL("endpoint");
        response = connection.call(message, endpoint); 
        response.writeTo(System.out);

        }
        catch(Exception e) {
            e.printStackTrace();
        }

        finally{
            try {
                connection.close();
            }
            catch(Exception e){

            }
        }

Эта строка: "response.writeTo (System.out)" выдает следующий вывод.

The file size is 21426

<S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope">
<faultcode>S:Server</faultcode>
<faultstring> Project upload failed. No file was attached. </faultstring>

У кого-нибудь получалось прикрепить zip-файл к своему SOAP-запросу?Я буду готов использовать другую библиотеку, если кто-либо сможет прикрепить файл с помощью своего SOAP-запроса с использованием Java, curl или чего-либо еще.

...