Apache cxf无法发送MULTIPART_BOUNDARY数据

时间:2019-05-27 11:15:53

标签: java soap cxf soap-client

我正在尝试使用apache cxf发送MULTIPART_BOUNDARY数据,但我无法发送

我还在CXF客户端中启用了MTOM支持

以下是我的示例

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 :(得分:0)

经过一些调查,我通过在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);

        }
}