ID到多部分实体

鉴于我们提出了多部分请求。 我们现在需要添加内容ID。 以下是我们试图用来创建多部分请求的代码:

MultipartEntity mpEntity = new MultipartEntity();
StringBody body;
try
{
    body = new StringBody( xml, "application/xml", Charset.forName( "UTF-8" ) );
    byte[] data = getBytesFromFile( image );
    ByteArrayBody bab = new ByteArrayBody( data, "image/jpeg", "test_image_cid" );
    mpEntity.addPart( "body", body );
    mpEntity.addPart( "test_image_cid", bab );

} catch ( UnsupportedEncodingException e )
{
    e.printStackTrace();
}

HttpPost request = new HttpPost("http://10.1.1.1");
request.addHeader( "Authorization", authorization_header_values );
request.addHeader( "Content-Type", "Multipart/Related" );
request.setEntity( mpEntity );
return request;

这就是我们所调用的web服务所要求的:

<?xml version="1.0" encoding="utf-8"?> <request method="receipt.create"> 
   <receipt>
       <expense_id>1</expense_id>  <!-- id of expense -->
       <image>cid:xxxxxxxxxxxxx</image> <!-- content-id used on the related binary content -->
   </receipt>
</request>

这是我们从服务器获取以进行调试的内容:

POST / HTTP / 1.1授权:OAuth realm =“”,oauth_version =“1.0”,oauth_consumer_key =“key”,oauth_token =“token”,oauth_timestamp =“1358197676614”,oauth_nonce =“1111111”,oauth_signature_method =“PLAINTEXT”,oauth_signature =“签名”内容类型:多部分/相关用户代理:代理内容长度:2336363主机:10.1.1.1连接:保持活动

--HePeiFlrswQmM8Mi1uoWpzJRfrnp3AMtZjpCdt内容处理:表单数据; name =“body”Content-Type:application / xml; charset = UTF-8 Content-Transfer-Encoding:8bit

<?xml version='1.0' encoding='UTF-8' ?>
    <request method="receipt.create">
        <receipt>
            <expense_id>979</expense_id>
            <image>cid:test_image_cid</image>
        </receipt>
    </request>

--HePeiFlrswQmM8Mi1uoWpzJRfrnp3AMtZjpCdt内容处理:表单数据; NAME = “test_image_cid”; filename =“test_image_cid”Content-Type:image / jpeg Content-Transfer-Encoding:二进制

我们一直在思考如何将Content-ID添加到此请求中。 这次调用中是否有任何明显的缺失? 有没有另外一种方法来建立这个请求? 感谢您的任何建议!


要添加Content-Id或任何其他字段,您必须使用FormBodyPart。 简而言之,将这些行分开:

ByteArrayBody bab = new ByteArrayBody( data, "image/jpeg", "test_image_cid" );
mpEntity.addPart( "body", body );

进入这些线路:

ByteArrayBody bab = new ByteArrayBody( data, "image/png", "byte_array_image" );
FormBodyPart fbp = new FormBodyPart( "form_body_name", bab );
fbp.addField( "Content-Id", "ID_GOES_HERE" );
mpEntity.addPart( fbp );

这应该为你做!

链接地址: http://www.djcxy.com/p/46479.html

上一篇: ID to multipart entity

下一篇: Ajax file upload in mvc application