In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to understand the MTOM of the file transferred in WebService CXF. Many people may not know much about it. In order to make you understand better, the editor summarized the following content for you. I hope you can get something according to this article.
First, several related concepts
1. Basic concepts of MTOM
Official introduction: http://cxf.apache.org/docs/mtom-attachments-with-jaxb.html
MTOM (Message Transmission Optimization Mechanism) message transmission mechanism is optimized.
The model proposed by it is suitable for the interaction of large amounts of data. A solution is proposed to solve the overhead caused by Base64 coding. When the amount of data is small, SOAP still uses XML for message delivery.
The message Transport Optimization Mechanism (MTOM) standard allows large data elements contained in a message to be externalized and transmitted with the message as binary data without any special encoding. MTOM messages are packaged into multipart / related MIME sequences and sent together in SOAP messages.
However, in the case of a large amount of data, if the data is still Base64 encoded, it will bring 33% additional overhead, which is intolerable for a large amount of data exchange. MTOM is an improved method based on SOAP message transmission. For the transmission of a large amount of data, it will not be encoded by Base64, but directly encapsulated in the MIME part of the SOAP message in the form of binary raw data of the attachment. The SOAP message refers to the binary content by pointing to the MIME part that is sent with it, in addition to the basic XML data of SOAP, which is also Base64 encoding. Because this model is basically consistent with the simple mail protocol SMTP model.
MTOM improves the efficiency of data processing by simplifying the coding process of a large number of data. MTOM also has some necessary overhead because of the necessary information, such as SOAP messages. MTOM can only show its advantages when the size of a binary data element is larger than about 1 KB.
What is BASE64 encoding, MTOM message optimized transport mechanism, MIME. These are very necessary for us to understand the optimized transmission mechanism of MTOM messages.
2. BASE64 coding
The principle of BASE64 coding is very simple. The method is to 6bit the input data stream each time (each bit represents 1-bit binary), which is less than 0 of 6bit, so that every 3 8-bit bytes will be encoded into 4 6-bit bytes (3 × 8 → 4 × 6); less than 4 bytes will be filled with "=". In fact, the four six-bit bytes are still 8 bits, except that the high two bits are set to 0. When a byte is only valid for 6 bits, its value space is 0 to 2 to the sixth power minus 1, that is, 63, that is, the value space for each code of the converted Base64 code is (0,63).
In this way, 3 8-bit bytes can be converted into 4 bytes, all of which can be mapped to characters. That is, data can be replaced by character encoding. Because the converted string is one more byte than the original, and it is 1 more than 3 bytes longer. As a result, the length of the encoded data is increased to 4pm 3x. This is also why using SOAP messages is less efficient than MTOM. Because SOAP uses the BASE64 language for messaging, XML is based on XML coding.
3 、 MIME
MIME means multi-purpose Internet mail expansion agreement. MIME expands the basic text-oriented Internet mail system so that binary attachments can be included in messages. MIME (Multipurpose Internet Mail Extentions), generally translated as "multi-purpose web mail extension protocol". As the name implies, it can transfer multimedia files. MIME (Multipurpose Internet Mail Extensions, Multi-purpose Internet Mail extension) is a specification for creating file formats for e-mail exchange, web documents, and other applications on the enterprise network and Internet.
II. A trip to MTOM
1 、 POJO
Java code
Package org.wy.pojo
Import javax.activation.DataHandler
Import javax.xml.bind.annotation.XmlAccessType
Import javax.xml.bind.annotation.XmlAccessorType
Import javax.xml.bind.annotation.XmlMimeType
Import javax.xml.bind.annotation.XmlRootElement
Import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter
@ XmlRootElement
@ XmlAccessorType (XmlAccessType.FIELD)
Public class User {
Private String name = "wy"
Private String sex = "man"
Public int age = 20
/ / private Address address
/ / Note this is an attachment type of data
@ XmlMimeType ("application/octet-stream")
Private DataHandler dataHandler
Public String getName () {
Return name
}
Public void setName (String name) {
This.name = name
}
/ / dealing with complex objects
/ * @ XmlJavaTypeAdapter (AddressAdapter.class)
Public Address getAddress () {
Return address
}
Public void setAddress (Address address) {
This.address = address
} * /
Public DataHandler getDataHandler () {
Return dataHandler
}
Public void setDataHandler (DataHandler dataHandler) {
This.dataHandler = dataHandler
}
}
Attachments to be transferred in MTOM mode must use the javax.activation.DataHandler class, and note that the @ XmlAccessorType (XmlAccessType.FIELD) annotation must be used on the class, and the annotation JAXB focuses only on fields and not on properties (the getXXX () method) when converting between JAVA objects and XML.
Then use @ XmlMimeType annotation to mark this is an attachment type of data, here we mark imageData is a binary file, of course, you can also use specific MIME types, such as: image/jpg, image/gif, etc., but to consider whether the client supports it or not.
2. Interface class
Java code
Package org.wy.service
Import javax.jws.WebService
Import javax.jws.soap.SOAPBinding
Import javax.xml.ws.soap.MTOM
Import org.wy.pojo.User
The @ WebService (name= "userService") / / name attribute is marked on the interface class, and you can specify the interface name in wsdl, that is, the name of the interface class in the generated client code.
@ SOAPBinding (style = SOAPBinding.Style.RPC) / / specify SOAP message style
@ MTOM / / enable MTOM function
Public interface IUserService {
Public User getUser ()
}
The @ MTOM annotation is used to enable the MTOM function.
The name attribute in the @ WebService annotation is marked on the interface class, and you can specify the interface name in wsdl, that is, the name of the interface class in the generated client code.
@ SOAPBinding (style = SOAPBinding.Style.RPC) specifies the SOAP message style with two enumerated values: SOAPBinding.Style.DOCUMENT (the default) and SOAPBinding.Style.RPC. You can compare the wsdl generated by the two methods will be different, and the generated client code will be different.
Implementation class:
Java code
Package org.wy.service.impl
Import java.io.File
Import javax.activation.DataHandler
Import javax.activation.FileDataSource
Import javax.jws.WebService
Import org.wy.pojo.User
Import org.wy.service.IUserService
/ * *
*
* @ author wy
*
, /
@ WebService
Public class UserServiceImpl implements IUserService {
Public User getUser ()
{
User user = new User ()
User.setName ("wy")
User.setDataHandler (new DataHandler (new FileDataSource (new File ("D:\ resume\\ logo.gif")
Return user
}
}
3. Server configuration
ApplicationContext-cxf.xml
Xml code
Type "wsimport-p org.wy.client-keep http://localhost:8080/WebServiceCXF/services/UserService?wsdl" to generate the client code on the command line, and copy it to the appropriate folder of the project."
Of course, you can also use the wsdl2java command in CXF to generate the client.
At this point, you can invoke the service:
Test class:
Java code
Package org.wy.jdkclienttest
Import java.io.FileOutputStream
Import java.io.IOException
Import java.io.InputStream
Import javax.activation.DataHandler
Import org.wy.jdkclient.User
Import org.wy.jdkclient.UserServiceImplService
/ * *
*
* @ author wy
*
, /
Public class Test {
Public static void main (String [] args) throws IOException {
UserServiceImplService userService = new UserServiceImplService ()
User user = userService.getUserServiceImplPort () .getUser ()
String name = user.getName ()
Int age = user.getAge ()
String sex = user.getSex ()
System.out.println (name+ "\ r\ n" + age+ "\ r\ n" + sex)
/ / output the passed file
DataHandler dataHandler = user.getDataHandler ()
String fileName = dataHandler.getName ()
String fileType = dataHandler.getContentType ()
Object content = dataHandler.getContent ()
System.out.println (fileName+ "\ r\ n" + fileType+ "\ r\ n" + content.toString ())
/ / Streaming Mode
InputStream is = dataHandler.getInputStream ()
FileOutputStream fos = new FileOutputStream ("D:\\ logo.gif")
Byte [] bytes = new byte [2048]
Int len = 0
While ((len = is.read (bytes))! =-1) {
Fos.write (bytes, 0, len)
}
Fos.flush ()
Fos.close ()
Is.close ()
}
}
Let's look at the results:
Java code
2011-12-25 14:03:49 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL
Information: Creating Service {http://impl.service.wy.org/}UserServiceImplService from WSDL: http://localhost:8080/WebServiceCXF/services/UserService?wsdl
Name= wy
Age= 20
Sex= man
FileName= null
FileType= image/gif
FileContent= org.apache.cxf.attachment.DelegatingInputStream@11e1bbf
After reading the above, do you have any further understanding of how to understand the transfer file MTOM in WebService CXF? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.