文章目录
- [第三十章 使用 MTOM 进行附件 - 控制 MTOM 打包](#第三十章 使用 MTOM 进行附件 - 控制 MTOM 打包)
 - [控制 `MTOM` 打包](#控制 
MTOM打包) - 示例
 - 
- [Web Service](#Web Service)
 - [`Web Client`](#
Web Client) 
 
第三十章 使用 MTOM 进行附件 - 控制 MTOM 打包
控制 MTOM 打包
默认情况下,创建 MTOM 包时,它使用以下规则:
- 它以内联方式输出二进制字符串( 
%Binary或在%xsd.base64Binary)。 - 它使用附件输出二进制流。
 
可以使用 MTOM 属性参数来更改此默认值:
1表示将此属性作为附件输出。0表示以内联方式输出此属性。
当 Web 服务或 Web 客户端未使用 MTOM 时,MTOM 属性参数不起作用。
此外,此属性参数对 Web 服务的 WSDL 没有影响。
示例
此示例展示了 Web 服务接收二进制文件并将其发送回调用者。
相应的 Web 客户端发送一个带有硬编码文件名的文件,从 Web 服务接收相同的文件,然后使用新名称保存它以证明它已成功发送。
Web Service
Web服务如下:
            
            
              java
              
              
            
          
          /// 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 方法。此方法最初如下:
            
            
              java
              
              
            
          
          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)
}
        由于我们发送的文件可能超出字符串长度限制,因此我们对方法签名进行如下调整:
            
            
              java
              
              
            
          
          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 参数。
为了使用这个代理客户端,我们创建以下类:
            
            
              java
              
              
            
          
          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
}
}