基于JAVA中Jersey處理Http協(xié)議中的Multipart的詳解
那么Http協(xié)議中的Multipart是個(gè)什么東東?下面是摘抄h(huán)ttp協(xié)議1.1的一段話:
在multipart entity(多部分實(shí)體)的例子中,一個(gè)或多個(gè)不同的數(shù)據(jù)集合并在一個(gè)單一的body(體)中,一個(gè)"multipart"(多部分)類型 field的(域)必須出現(xiàn)在實(shí)體的header(頭域)。body(體)必須包括一個(gè)或多個(gè)body part(體部分),每一個(gè)位于boundary(邊界)定界符線之前,最后一個(gè)則跟著一個(gè)結(jié)束邊界定界符線。在它的邊界定界符線后,每一個(gè)體部分由頭域、空行、體組成。
上面的描述寫(xiě)的有點(diǎn)拗口,簡(jiǎn)單的理解可以為:一個(gè)post的請(qǐng)求,可以根據(jù)一定規(guī)范去定義多個(gè)部分;
下面用移動(dòng)網(wǎng)狀網(wǎng)協(xié)議(其實(shí)就是一個(gè)請(qǐng)求中包括2個(gè)獨(dú)立的xml內(nèi)容,一個(gè)head的xml,一個(gè)body的xml)去舉例說(shuō)明如何利用Jersey處理Multipart,主要代碼如下(開(kāi)始的時(shí)候server端接收的代碼死活不知道如何寫(xiě)也沒(méi)查到別人怎么寫(xiě)的,后來(lái)一生氣,反編譯jersey-multipart-1.0.3.1.jar包的代碼看了下,才明白):
private static WebResource webResource = client.resource("http://xxx.xx.xx:xxx");
public static final String HeadFieldName = "xmlhead";
public static final String BodyFieldName = "xmlbody";
// Client發(fā)送代碼
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端接收代碼
@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));
}
}
}
.....
}
用工具抓包的實(shí)際post報(bào)文:
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--
相關(guān)文章
詳解Spring中使用@within與@target的區(qū)別
這篇文章主要介紹了Spring中使用@within與@target的一些區(qū)別,本文通過(guò)項(xiàng)目案例給大家詳細(xì)分析,給大家介紹的非常詳細(xì),代碼簡(jiǎn)單易懂,需要的朋友可以參考下2021-09-09解決mybatis-plus新增數(shù)據(jù)自增ID變無(wú)序問(wèn)題
這篇文章主要介紹了解決mybatis-plus新增數(shù)據(jù)自增ID變無(wú)序問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2023-07-07Java中Controller、Service、Dao/Mapper層的區(qū)別與用法
在Java開(kāi)發(fā)中,通常會(huì)采用三層架構(gòu)(或稱MVC架構(gòu))來(lái)劃分程序的職責(zé)和功能,分別是Controller層、Service層、Dao/Mapper層,本文將詳細(xì)給大家介紹了三層的區(qū)別和用法,需要的朋友可以參考下2023-05-05SpringCloud+RocketMQ實(shí)現(xiàn)分布式事務(wù)的實(shí)踐
分布式事務(wù)已經(jīng)成為了我們的經(jīng)常使用的。所以我們來(lái)一步一步的實(shí)現(xiàn)基于RocketMQ的分布式事務(wù)。感興趣的可以了解一下2021-10-10動(dòng)態(tài)代理模擬實(shí)現(xiàn)aop的示例
下面小編就為大家?guī)?lái)一篇?jiǎng)討B(tài)代理模擬實(shí)現(xiàn)aop的示例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望對(duì)大家有所幫助2017-11-11詳解springboot+aop+Lua分布式限流的最佳實(shí)踐
這篇文章主要介紹了詳解springboot+aop+Lua分布式限流的最佳實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06MyBatis-Plus 之selectMaps、selectObjs、selectCount、selectO
本文主要介紹了MyBatis-Plus 之selectMaps、selectObjs、selectCount、selectOne的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03