So what is the Multipart in the Http protocol? The following is an excerpt from http protocol 1.1:
In the case of a multipart entity, where one or more different data sets are combined into a single body, a "multipart" type field must appear in the entity's header(header field). The body must consist of one or more body parts, each preceding a boundary delimiter line and the last one followed by an ending boundary delimiter line. After its boundary delimiter line, each body part consists of header fields, blank lines, and body.
The above description is a bit confusing to write, but 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 (actually, a request includes two independent xml contents, a head xml and a body xml) to illustrate how to use Jersey to process Multipart. The main code is as follows (the server receives it at the beginning I didn’t know how to write the code, and I didn’t find out how others wrote it. Later, when I got angry, I decompiled the code of jersey-multipart-1.0.3.1.jar package and looked at it, and then I understood):
public static final String HeadFieldName = "xmlhead";
public static final String BodyFieldName = "xmlbody";
//Client sends 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 receives 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));
}
}
}
.....
}
<?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--