gpt4 book ai didi

java - Apache cxf 无法发送 MULTIPART_BOUNDARY 数据

转载 作者:行者123 更新时间:2023-12-02 05:11:07 30 4
gpt4 key购买 nike

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

最佳答案

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

}
}

关于java - Apache cxf 无法发送 MULTIPART_BOUNDARY 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56325009/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com