Apache cxf не может отправить MULTIPART_BOUNDARY данные - PullRequest
0 голосов
/ 27 мая 2019

Я пытаюсь отправить MULTIPART_BOUNDARY с помощью apache cxf, но я не могу отправить его

У меня также включена поддержка MTOM в клиенте CXF

Ниже приведен мой пример

public class Test1 {

    public static void doInvoke() throws Exception {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:6060/ForAttachments", classLoader);

        ClientImpl clientImpl = (ClientImpl) client;
        Endpoint endpoint = clientImpl.getEndpoint();
        endpoint.put("mtom-enabled", "true");


        List<Attachment> attachments = new ArrayList<Attachment>();
        Map<String, List<String>> headers = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
        headers.put("Content-Type", Arrays.asList("application/pdf"));

        Attachment attach = AttachmentUtil
                .createAttachment(new FileDataSource(new File("D:\\images\\test.pdf")).getInputStream(), headers);
        attachments.add(attach);

        Object[] result = client.invoke("Attachments", attachments);
    }
}

как я могу добавить такие данные, как

------------MULTIPART_BOUNDARY_16ade2a21311----------
Content-Transfer-Encoding: binary
Content-Type: text/xml; charset=UTF-8
Content-ID: <40867d3d6a9ffdb4-3a510474a77de5c8-6d664d4b6ca2a535-1afefb4bea337cc4@systinet.com>

<?xml version="1.0" encoding="UTF-8"?>
<e:Envelope xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:e="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wn0="http://systinet.com/xsd/SchemaTypes/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><e:Body><n0:Attachments1 xmlns:n0="http://systinet.com/wsdl/com/localhost/ForAttachments/ForAttachmentsImpl#Attachments1?KClMb3JnL2lkb294L3dhc3Avc2VyaWFsaXphdGlvbi94c2RidWlsdGluL0RhdGU7"/></e:Body></e:Envelope>
------------MULTIPART_BOUNDARY_16ade2a21311----------
Content-Transfer-Encoding: binary
Content-Type: image/jpeg

1 Ответ

0 голосов
/ 09 июля 2019

После некоторого расследования я решил проблему, добавив следующий код внутри OutInterceptor.

Подключите ваш перехватчик к клиенту

Ex. client.getOutInterceptors().add(new OutInterceptor());

    public class OutInterceptor extends LoggingOutInterceptor {

    @Override
        public void handleMessage(Message message) throws Fault{
             Collection<Attachment> attachments=new ArrayList<Attachment>();

                    byte[] byteArray;// convert file into byte array
                    InputStream in= new ByteArrayInputStream(byteArray);
                    DataHandler dataHandler = new DataHandler(new InputStreamDataSource(in));
                    attachments.add(new AttachmentImpl("att-"+i,dataHandler)); 

            message.setAttachments(attachments);

        }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...