-
[JAVA]应用Jersey处理惩罚Http和谈中的Multipart
添加时间:2013-5-9 点击量:之前在基于C#开辟彩信用最原始的StringBuilder拼接字符串体式格式处理惩罚过Multipart。如今在做一个项目标时辰,因为之前的技巧路线都是应用Jersey处理惩罚Http这块,为了对峙技巧路线一致,研究了一下如何应用Jersey处理惩罚Http和谈中的Multipart。
那么Http和谈中的Multipart是个什么东东?下面是摘抄http和谈1.1的一段话:
在multipart entity(多项目组实体)的例子中,一个或多个不合的数据集归并在一个单一的body(体)中,一个multipart(多项目组)类型 field的(域)必须呈如今实体的header(头域)。body(体)必须包含一个或多个body part(体项目组),每一个位于boundary(鸿沟)定界符线之前,最后一个则跟着一个停止鸿沟定界符线。在它的鸿沟定界符线后,每一个别项目组由头域、空行、体构成。
上方的描述写的有点拗口,简单的懂得可认为:一个post的恳求,可以按照必然规范去定义多个项目组;下面用移动网状网和谈(其实就是一个恳求中包含2个自力的xml内容,一个head的xml,一个body的xml)去举例申明如何哄骗Jersey处理惩罚Multipart,首要代码如下(开端的时辰server端接管的代码死活不知道如何写也没查到别人怎么写的,后来一朝气,反编译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发送代码
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));
}
}
}
.....
}用对象抓包的实际post报文:
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--
我们永远不要期待别人的拯救,只有自己才能升华自己。自己已准备好了多少容量,方能吸引对等的人与我们相遇,否则再美好的人出现、再动人的事情降临身边,我们也没有能量去理解与珍惜,终将擦肩而过。—— 姚谦《品味》
