A detailed explanation of multipart in HTTP protocol based on Jersey processing in Java _java

Source: Internet
Author: User

So what is the multipart in the HTTP protocol? Here is a paragraph that extracts HTTP protocol 1.1:
In an example of a multipart entity (multi-part entity), one or more different sets of data and in a single body, a "multipart" (multi-part) Type field (field) must appear in the header (header field) of the entity. The body must include one or more body parts, each preceded by the boundary (boundary) delimiter line, and the last one followed by an ending boundary delimiter line. After its boundary delimiter line, each individual part is composed of the header domain, the empty line, and the body.
The description above is a bit clumsy, simple understanding can be: A POST request, you can define a number of parts according to a certain specification;

The following is a mobile mesh protocol (in fact, a request includes 2 separate XML content, one head XML, one body XML) to illustrate how to use jersey to handle multipart, The main code is as follows (at the beginning of the server side of the code received anyway do not know how to write and did not find how others write, and later an angry, reverse-compiled Jersey-multipart-1.0.3.1.jar package code looked down, just understand):

Copy Code code as follows:

private static WebResource WebResource = Client.resource ("http://xxx.xx.xx:xxx");

public static final String headfieldname = "Xmlhead";
public static final String bodyfieldname = "Xmlbody";


Client Send Code
public static string post (string head, String body) throws Businessexception {
Formdatamultipart MultiPart = new Formdatamultipart ();
Multipart.field (Requestfield.headfieldname, Head, Mediatype.multipart_form_data_type);
Multipart.field (Requestfield.bodyfieldname, body, mediatype.multipart_form_data_type);
Return Webresource.type ("Multipart/form-data"). Post (String.class, multipart);
}

Server-side Receive code
@POST
@Produces ({mediatype.application_xml, mediatype.multipart_form_data})
@Consumes ({mediatype.application_xml, mediatype.multipart_form_data})
Public String Service (Formdatamultipart MultiPart) throws exception{
if (MultiPart = = null) {
if (_logger.iserrorenabled ()) {
_logger.error ("The request Formdatamultipart is null");
}

throw new Exception ("The request Formdatamultipart is null");
}

list<requestfield> requestfields = new arraylist<requestfield> ();
For (BodyPart bodyPart:multiPart.getBodyParts ()) {
String FieldName = ((Formdatabodypart) bodypart). GetName (). Trim ();
if (Fieldname.equalsignorecase (Requestfield.headfieldname)) {
Requestfields.add (New Requestfield (FieldName, Bodypart.getentityas (String.class)));
}
else if (Fieldname.equalsignorecase (Requestfield.bodyfieldname)) {
Requestfields.add (New Requestfield (FieldName, Bodypart.getentityas (String.class)));
}
else{
if (_logger.iswarnenabled ()) {
_logger.warn ("Invalid FieldName:" + FieldName + ", Originxml:" + Bodypart.getentityas (string.class));
}
}
}

.....
}

The actual post message with the tool grasping the package:
Copy Code code as follows:

Post/ba/resources/bossserver http/1.1
content-type:multipart/form-data;boundary=boundary_1_30911772_1367997277472
mime-version:1.0
User-agent:java/1.6.0_10-rc2
host:192.168.245.18:8082
Accept:text/html, Image/gif, Image/jpeg, *; Q=.2, */*; q=.2
Connection:keep-alive
content-length:1600
--boundary_1_30911772_1367997277472
Content-disposition:form-data;name= "Xmlhead"
Content-type:multipart/form-data

<?xml version= "1.0" encoding= "UTF-8"?>
<InterBOSS>
<Version>0100</Version>
<TestFlag>0</TestFlag>
<BIPType>
<BIPCode>BIP2B543</BIPCode>
<ActivityCode>T2001543</ActivityCode>
<ActionCode>0</ActionCode>
</BIPType>
<RoutingInfo>
<OrigDomain>IMPS</OrigDomain>
<RouteType>01</RouteType>
<Routing>
<HomeDomain>BOSS</HomeDomain>
<RouteValue>13810494631</RouteValue>
</Routing>
</RoutingInfo>
<TransInfo>
<SessionID>2013050815143783928824</SessionID>
<TransIDO>2013050815143783928824</TransIDO>
<TransIDOTime>20130508151437</TransIDOTime>
</TransInfo>
</InterBOSS>
--boundary_1_30911772_1367997277472
Content-disposition:form-data;name= "Xmlbody"
Content-type:multipart/form-data

<?xml version= "1.0" encoding= "UTF-8"?>
<InterBOSS>
<svccont><! [cdata[<subscribeservicereq>
<msgTransactionID>210001BIP2B543130508151437477294</msgTransactionID>
<subscribeServInfo>
<oprTime>20130508151436</oprTime>
<actionID>06</actionID>
<effTime>20130508151437</effTime>
<expireTime>30000101000000</expireTime>
<feeUser_ID>13810494631</feeUser_ID>
<destUser_ID>13810494631</destUser_ID>
<actionReasonID>1</actionReasonID>
<servType>210001</servType>
<subServType>FXCJHY</subServType>
<SPID>901508</SPID>
<SPServID>FXCJHY</SPServID>
<accessMode>01</accessMode>
<feeType>2</feeType>
</subscribeServInfo>
</subscribeServiceReq>]]></SvcCont>
</InterBOSS>
--boundary_1_30911772_1367997277472--

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.