java 通过 RESTful CXF 使用多部分/表单数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15271573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:03:42  来源:igfitidea点击:

Consuming a multipart/form-data via RESTful CXF

javarestcxfHymansonmultipartform-data

提问by fcm

I've been working in a webservice that consumes and produces JSON files using Apache CXF in conjuction with Hymanson.
However, one of the service's methods should be able to save an uploaded image from a mobile application that makes a multipart/form-data POST request to my webservice, and I don't know how to treat this kind of content-type within my context. We usually create "Request" and "Response" objects to consume and produce the JSON, however, I'm afraid this would not work for this case.

我一直在使用 Apache CXF 与 Hymanson 结合使用和生成 JSON 文件的 web 服务工作。
但是,该服务的一种方法应该能够保存来自移动应用程序的上传图像,该应用程序向我的 Web 服务发出多部分/表单数据 POST 请求,而我不知道如何在我的语境。我们通常会创建“请求”和“响应”对象来使用和生成 JSON,但是,恐怕这不适用于这种情况。

This is the Request format:

这是请求格式:

Content-type: multipart/form-data
"Description": text/plain
"Path": text/plain
"Image": image/jpeg

How to correctly consume this kind of request and save the image server-side?

如何正确消费这种请求并保存图像服务器端?



[EDIT]

[编辑]

I managed to consume multipart/form-data by using this:

我设法通过使用这个来使用 multipart/form-data:

public returnType savePicture(
                @Multipart(value = "mode", type = "text/plain") String mode,
                @Multipart(value = "type", type = "text/plain") String type,
                @Multipart(value = "path", type = "text/plain") String path
                @Multipart(value = "image", type = "image/jpeg") Attachment image
            ) 
    {

However, when trying to consume the following POST request:

但是,当尝试使用以下 POST 请求时:

Content-type: multipart/form-data, boundary=AaB03x

--AaB03x
content-disposition: form-data; name="mode"

T
--AaB03x
content-disposition: form-data; name="type"

M
--AaB03x
content-disposition: form-data; name="path"

c:/img/
--AaB03x
content-disposition: form-data; name="image"; filename="image.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: binary

imgdata
--AaB03x--

I'm getting the following error:

我收到以下错误:

javax.ws.rs.BadRequestException: org.apache.cxf.jaxrs.utils.multipart.MultipartReadException: No multipart with content id typefound, request content type : multipart/form-data;boundary=AaB03x

javax.ws.rs.BadRequestException: org.apache.cxf.jaxrs.utils.multipart.MultipartReadException: 没有找到具有内容 ID类型的多部分,请求内容类型:multipart/form-data;boundary=AaB03x

When I consume only mode, for instance, it works fine. It only breaks for 2 or more parameters. Any idea for why is that wrong?

例如,当我只使用mode 时,它工作正常。它只会中断 2 个或更多参数。知道为什么这是错误的吗?

采纳答案by fcm

It seems we found the problem, and it was related to the format of the request. The correct format should have been:

好像找到问题了,和请求的格式有关。正确的格式应该是:

Content-type: multipart/form-data, boundary=AaB03x

--AaB03x
content-disposition: form-data; name="mode"

T--AaB03x

content-disposition: form-data; name="type"

M--AaB03x

content-disposition: form-data; name="path"

c:/img/--AaB03x

content-disposition: form-data; name="image"; filename="image.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: binary

imgdata--AaB03x--

Changing to this format allowed me to consume the other parameters.

更改为这种格式允许我使用其他参数。

回答by Chris

I faced similar issue sometime back.

我曾经遇到过类似的问题。

The following code did the trick for me

以下代码对我有用

@POST
@Consumes("multipart/form-data")
public void yourMethod(<params>) throws Exception {
}

In short, it is I think the @Consumesannotation you are missing.

简而言之,我认为@Consumes您缺少注释。

回答by Anup Deshpande

For consuming multipart form data. use @consumes tag & provide "multipart/form-data" along with value parameter like

用于使用多部分表单数据。使用@consumes 标签并提供“multipart/form-data”以及像这样的值参数

@Consumes(value = "multipart/form-data")

@Consumes(value = "multipart/form-data")

refer https://jnorthr.wordpress.com/2012/07/10/http-header-content-type-and-encodings/

参考 https://jnorthr.wordpress.com/2012/07/10/http-header-content-type-and-encodings/