【问题标题】:How to embed multipart/mixed in a multipart/form-data?如何将 multipart/mixed 嵌入到 multipart/form-data 中?
【发布时间】:2018-12-01 04:09:42
【问题描述】:

在 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:

最后,多文件的 multipart/form-data 请求(文件上传)可以 也得到支持。例如,这个请求可以通过一个方法来处理 像这样的签名:

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

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

当然,您可以随时发布 List of Attachment 。 但是如何将多部分/混合放在其中一个附件中?

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

【问题讨论】:

  • 我不使用CXF,只是猜测,MultipartBody怎么样
  • 您好,谢谢。我也试过了,但生成的请求根本不是预期的。

标签: rest jax-rs cxf multipart


【解决方案1】:

请参阅文档中的"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);

【讨论】:

    猜你喜欢
    • 2014-10-31
    • 2015-08-27
    • 2017-01-14
    • 2016-01-31
    • 1970-01-01
    • 2014-10-31
    • 2014-06-10
    • 2018-12-31
    • 1970-01-01
    相关资源
    最近更新 更多