第三十章 使用 MTOM 进行附件 - 控制 MTOM 打包

第三十章 使用 MTOM 进行附件 - 控制 MTOM 打包

控制 MTOM 打包

默认情况下,创建 MTOM 包时,它使用以下规则:

  • 它以内联方式输出二进制字符串( %Binary或在 %xsd.base64Binary)。
  • 它使用附件输出二进制流。

可以使用 MTOM 属性参数来更改此默认值:

  • 1 表示将此属性作为附件输出。
  • 0 表示以内联方式输出此属性。

Web 服务或 Web 客户端未使用 MTOM 时,MTOM 属性参数不起作用。

此外,此属性参数对 Web 服务的 WSDL 没有影响。

示例

此示例展示了 Web 服务接收二进制文件并将其发送回调用者。

相应的 Web 客户端发送一个带有硬编码文件名的文件,从 Web 服务接收相同的文件,然后使用新名称保存它以证明它已成功发送。

Web Service

Web服务如下:

/// Receive an attachment and send it back
Class MTOM.RoundTripWS Extends %SOAP.WebService
{

///  Name of the web service.
Parameter SERVICENAME = "RoundTrip";

///  SOAP namespace for the web service
Parameter NAMESPACE = "https://www.roundtrip.org";

///  Receive an attachment and send it back
Method ReceiveFile(attachment As %GlobalBinaryStream) As %GlobalBinaryStream [ WebMethod ]
{
  Set ..MTOMRequired=1
  Quit attachment
}

}

Web Client

生成的 Web 客户端 (MTOMClient.RoundTripSoap) 包含方法 ReceiveFile(),该方法调用同名的 Web 方法。此方法最初如下:

Method ReceiveFile(attachment As %xsd.base64Binary) As %xsd.base64Binary 
[ Final, SoapBindingStyle = document, SoapBodyUse = literal, WebMethod ]
{
 Quit ..WebMethod("ReceiveFile").Invoke($this,"https://www.roundtrip.org/MTOM.RoundTripWS.ReceiveFile",
 .attachment)
}

由于我们发送的文件可能超出字符串长度限制,因此我们对方法签名进行如下调整:

Method ReceiveFile(attachment As %GlobalBinaryStream) As %GlobalBinaryStream 
[ Final, SoapBindingStyle = document, SoapBodyUse = literal, WebMethod ]
{
 Quit ..WebMethod("ReceiveFile").Invoke($this,"https://www.roundtrip.org/MTOM.RoundTripWS.ReceiveFile",
 .attachment)
}

默认情况下,Web 客户端不需要 MTOM;也就是说,未定义 MTOMREQUIRED 参数。

为了使用这个代理客户端,我们创建以下类:

Include %systemInclude

Class MTOMClient.UseClient
{

/// For this example, hardcode what we are sending
ClassMethod SendFile() As %GlobalBinaryStream
{
  Set client=##class(MTOMClient.RoundTripSoap).%New()
  Set client.MTOMRequired=1

  //reset location to port 8080 to enable tracing
  Set client.Location="https://devsys:8080/csp/mysamples/MTOM.RoundTripWS.cls"

  //create file
  Set filename="c:\sample.pdf"
  Set file=##class(%Library.FileBinaryStream).%New()
  Set file.Filename=filename

  //create %GlobalBinaryStream
  Set attachment=##class(%GlobalBinaryStream).%New()
  Do attachment.CopyFrom(file)
  
  //call the web service
  Set answer=client.ReceiveFile(attachment)
  
  //save the received file to prove we made the round trip successfully
  Set newfilename="c:\roundtrip"_$h_"sample.pdf"
  Set newfile=##class(%Library.FileBinaryStream).%New()
  Set newfile.Filename=newfilename
  Do newfile.CopyFromAndSave(answer)
  
  Quit answer
}

}
  • 21
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yaoxin521123

谢谢您的支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值