作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 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/
我正在尝试使用 apache cxf 发送 MULTIPART_BOUNDARY 数据,但无法发送 我还在 CXF 客户端中启用了 MTOM 支持 下面是我的例子 public class Test1
我是一名优秀的程序员,十分优秀!