加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – JAX-WS总是内联发送MTOM附件

发布时间:2020-12-14 16:23:43 所属栏目:Java 来源:网络整理
导读:基本上我想创建一个Web服务客户端通过代理方法发送一个mtom soap消息.我已经从Web服务wsdl创建了我的服务工件.消息已正确创建,但是当我启用mtom并添加附件时,附件始终是内联发送的,而不是单独的mime部分.它像mtom是启用,但由于某种原因它决定不优化消息,所以
基本上我想创建一个Web服务客户端通过代理方法发送一个mtom soap消息.我已经从Web服务wsdl创建了我的服务工件.消息已正确创建,但是当我启用mtom并添加附件时,附件始终是内联发送的,而不是单独的mime部分.它像mtom是启用,但由于某种原因它决定不优化消息,所以发送它内联.通过soapui运行相同的代码给出正确的结果,所以我知道服务本身将接受它.

这是我创建一个soap请求并发送它的基本代码.
我启用mtomfeature,但也尝试用soapBinding.setMTOMEnabled(true);
对于这两种方法,我已经使用((SOAPBinding)绑定).isMTOMEnabled()来检查它是否被设置为启用.

// initiate services....

// create service and enable mtom
WebServiceBlah service = new WebServiceBlah(new URL(wsdlURL),SERVICE_NAME);
WebServiceBlahPort port = service.getWebServiceBlahPort(new MTOMFeature(true,3072));

// load file
File file = new File("/home/mypdf.pdf");
FileInputStream fileinputstream = new FileInputStream(file);
int numberBytes = fileinputstream.available();
byte bytearray[] = new byte[numberBytes];
fileinputstream.read(bytearray);
fileinputstream.close();

// create uploadResult
UploadResult request = new UploadResult();

// create attachment
AttachmentType attachment = new AttachmentType();
attachment.setContentType("application/doc");
attachment.setValue(bytearray);

// create result and add attachment to it
RenderedResult result = new RenderedResult();
result.setResult(attachment);
result.setResultContentType("pdf");
result.setResultName("a pdf file");

// add result to request
request.getResult().add(result);

// send request
port.UploadResults(request);

我得到的是我的附件是内联发送的,如下所示. (用wireshark捕获)

POST /blah/ws/ HTTP/1.1
Content-type: multipart/related;start="<rootpart*15c3ee3b-60c7-4726-a52c-8080965e4536@example.jaxws.sun.com>";type="application/xop+xml";boundary="uuid:15c3ee3b-60c7-4726-a52c-8080965e4536";start-info="text/xml"
Soapaction: ""
Accept: text/xml,multipart/related,text/html,image/gif,image/jpeg,*; q=.2,*/*; q=.2
User-Agent: JAX-WS RI 2.1.6 in JDK 6
Host: 123.123.123.123
Connection: keep-alive
Content-Length: 12372

--uuid:15c3ee3b-60c7-4726-a52c-8080965e4536    
Content-Id: <rootpart*15c3ee3b-60c7-4726-a52c-8080965e4536@example.jaxws.sun.com>    
Content-Type: application/xop+xml;charset=utf-8;type="text/xml"    
Content-Transfer-Encoding: binary

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Header></S:Header>
<S:Body>
<ns2:uploadResult xmlns:xmime="http://www.w3.org/2005/05/xmlmime">
    <renderedResult>
        <result xmime:contentType="application/doc">JVBERi0xLjQKJaqrrK0KNCAwIG9iago8</result>
        <resultContentType>pdf</resultContentType>
        <resultName>a pdf file</resultName>
    </renderedResult>
</ns2:uploadResult>
</S:Body>
</S:Envelope>
--uuid:15c3ee3b-60c7-4726-a52c-8080965e4536

我想要的是将结果标签中的附件替换为在不同的MIME部分中添加到soap消息的内联标签和附件.例如

<result xmime:contentType='application/doc'>
    <inc:Include href="cid:myid3" xmlns:inc='http://www.w3.org/2004/08/xop/include'/>
</result>

然后将以下内容添加到soap消息中

------=_Part_10_28027205.1314348995670
Content-Type: application/pdf
Content-Transfer-Encoding: binary
Content-ID: cid:myid3
Content-Disposition: attachment; name="mypdf.pdf"
JVBERi0xLjQKJaqrrK0KNCAwIG9iago8

解决方法

许多事情可以影响是否实际使用MTOM附件.

在服务器上,首先是显而易见的:检查您的服务实现是否具有@MTOM注释.您还可以使用threshold()属性从此注释调整阈值(如SteveJ已经提到的).

有时服务器上的处理程序可能会干扰是否使用MTOM附件.任何处理程序将SOAP消息序列化为字符串或字节数组(通常用于将消息内容写入日志的调试样式处理程序)将阻止使用MTOM附件.如果可能,请尝试禁用您的处理程序链,并查看MTOM附件是否在此之后通过.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读