Previously, I used the original stringbuilder to splice strings to process multipart in C #-Based MMS. Now, when I was working on a project, because the previous technical routes used jersey to process HTTP, I studied how to use jersey to process the multipart in the HTTP protocol in order to keep the technical routes consistent.
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.CodeAs 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, then a lifetime of gas, decompile the jersey-multipart-1.0.3.1.jar package code to see the next, to understand ):
Private Static 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: 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 packet captured by a tool:
Post/BA/resources/bossserver HTTP/1.1content-type: multipart/form-data; boundary = Boundary_1_30911772_1367997277472MIME-Version: 1.0user-AGENT: Java/1.6.0 _ 10-rc2host: 192.168.245.18: 8082 accept: text/html, image/GIF, image/JPEG, *; q =. 2, */*; q =. 2 connection: Keep-alivecontent-length: 1600 -- Boundary_1_30911772_1367997277472Content-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_1367997277472Content-Disposition: Form-data; name = "xmlbody" Content-Type: multipart/form-Data <? XML version = "1.0" encoding = "UTF-8" ?> < Interboss > < Svccont > <! [CDATA [ <Subscribeservicereq> <msgtransactionid> response </msgtransactionid> <subscribeservinfo> <oprtime> 20130508151436 </oprtime> <actionid> 06 </actionid> <efftime> 20130508151437 </efftime>> 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_201730911772_1367997277472 --