如何在multipart / form-data中嵌入multipart / mixed?

时间:2018-06-21 18:09:36

标签: rest jax-rs cxf multipart

在CXF JAX-RS中,我们如何从Java客户端产生此请求:

请注意multipart / form-data中嵌入的multipart / mixed。

--bqJky99mlBWa-ZuqjC53mG6EzbmlxB 
Content-Disposition: form-data; name="owner" 
Content-Type: text/plain 

Larry 
--bqJky99mlBWa-ZuqjC53mG6EzbmlxB 
Content-Disposition: form-data; name="files" 
Content-Type: multipart/mixed; boundary=_Part_4_701508.1145579811786 

--_Part_4_701508.1145579811786 
Content-Disposition: form-data; name="book1" 
Content-Type: application/json; charset=US-ASCII 
Content-Transfer-Encoding: 8bit 

{"Book":{"name":"CXF in Action - 1","id":123}} 
--_Part_4_701508.1145579811786 
Content-Disposition: form-data; name="book2" 
Content-Type: application/json; charset=US-ASCII 
Content-Transfer-Encoding: 8bit 

{"Book":{"name":"CXF in Action - 2%","id":124}} 
--_Part_4_701508.1145579811786-- 
--bqJky99mlBWa-ZuqjC53mG6EzbmlxB-- 

This is from the CXF doc here

  

最后,包含多个文件的多部分/表单数据请求(文件上传)可以   也被支持。例如,可以使用以下方法处理该请求:   签名如下:

@POST 
@Path("/books/filesform") 
@Produces("text/xml") 
@Consumes("multipart/form-data") 
public Response addBookFilesForm(@Multipart("owner") String name, 
                                 @Multipart("files") List<Book> books) {} 

但是如何从Java客户端生成该请求?

当然,您始终可以发布附件列表。 但是如何将multipart / mixed放在附件之一中呢?

我尝试在其中一个附件中放入附件列表(代表多部分/混合),但是没有用。

1 个答案:

答案 0 :(得分:0)

请参见文档中的"Writing attachments" section

引用的示例:

WebClient client = WebClient.create("http://books");
client.type("multipart/mixed").accept("multipart/mixed");
List<Attachment> atts = new LinkedList<Attachment>();
atts.add(new Attachment("root", "application/json", new JSONBook()));
atts.add(new Attachment("image", "application/octet-stream", getImageInputStream()));
List<Attachment> atts = client.postAndGetCollection(atts, Attachment.class);