程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 基於JAVA中Jersey處置Http協定中的Multipart的詳解

基於JAVA中Jersey處置Http協定中的Multipart的詳解

編輯:關於JAVA

基於JAVA中Jersey處置Http協定中的Multipart的詳解。本站提示廣大學習愛好者:(基於JAVA中Jersey處置Http協定中的Multipart的詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是基於JAVA中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--

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved