Detailed explanation of how Jersey processes Multipart in Http Based on JAVA

Source: Internet
Author: User

So what is Multipart in Http? The following is a Excerpt from http 1.1:
In the multipart entity (multi-part entity) example, one or more different datasets are merged into a single body, and one "multipart" (multi-part) fields of the type must appear in the header of the object ). The body must contain one or more body parts. Each part is located before the boundary (boundary) line, and the last part is followed by an ending boundary line. After its boundary line, each part consists of a header domain, a blank line, and a body.
The description above indicates a bit of logging. A simple understanding can be as follows: a post request can define multiple parts according to certain specifications;

The following uses the mobile mesh Protocol (in fact, a request contains two independent xml content, a head xml, and a body xml) to illustrate how to use Jersey to process Multipart, the main code is as follows (at the beginning of the server side to receive the Code life and death do not know how to write did not find others how to write, and then a lifetime of gas, decompile the jersey-multipart-1.0.3.1.jar package code to read the next, ):

Copy codeThe Code is 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 ";

// The Client sends the 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 );
}

// The Server receives the 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. inclusignorecase (RequestField. HeadFieldName )){
RequestFields. add (new RequestField (fieldName, bodyPart. getEntityAs (String. class )));
}
Else if (fieldName. inclusignorecase (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 packet captured by a tool:Copy codeThe Code is as follows: POST/ba/resources/bossServer HTTP/1.1
Content-Type: multipart/form-data; boundary = boundary_000030911772_1367997277472
MIME-type: 1.0
User-Agent: Java/1.6.0 _ 10-rc2
Host: 192.168.245.17: 8082
Accept: text/html, image/gif, image/jpeg, *; q =. 2, */*; q =. 2
Connection: keep-alive
Content-Length: 1600
-- Boundary_201730911772_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_201730911772_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>
<OPR time> 20130508151436 </oprTime>
<ActionID> 06 </actionID>
<EffTime> 20130508151437 </effTime>
& Lt; expireTime & gt; 30000101000000 & lt;/expireTime & gt;
<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_201730911772_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.