通过RESTful CXF使用multipart / form-data [英] Consuming a multipart/form-data via RESTful CXF
问题描述
我一直在使用Apache服务器与Jackson一起使用和生成JSON文件的web服务。
但是,其中一个服务的方法应该能够从一个移动应用程序保存上传的图像,该应用程序向我的web服务发出多部分/表单数据POST请求,我不知道如何处理这种内容-type在我的上下文中。我们通常创建Request和Response对象来使用和生成JSON,但是,我担心这不适用于这种情况。
I've been working in a webservice that consumes and produces JSON files using Apache CXF in conjuction with Jackson.
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.
这是请求格式:
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?
我设法使用以下方法消费multipart / form-data:
I managed to consume multipart/form-data by using this:
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
)
{
但是,当尝试使用以下POST请求时:
However, when trying to consume the following POST request:
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--
我收到以下错误:
javax.ws.rs.BadRequestException:org.apache.cxf.jaxrs.utils.multipart.MultipartReadException:找不到内容ID 类型的多部分,请求内容类型:multipart / form -data; boundary = AaB03x
javax.ws.rs.BadRequestException: org.apache.cxf.jaxrs.utils.multipart.MultipartReadException: No multipart with content id type found, request content type : multipart/form-data;boundary=AaB03x
例如,当我只使用模式时,它可以正常工作。它只会中断2个或更多参数。任何想法为什么是错的?
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?
推荐答案
看来我们发现了问题,它与请求的格式有关。正确的格式应该是:
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.
这篇关于通过RESTful CXF使用multipart / form-data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!