我一直在使用Apache服务器和Jackson一起使用和生成JSON文件的web服务 .
但是,其中一项服务'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'知道如何在我的上下文中处理这种内容类型 . 我们通常创建"Request"和"Response"对象来使用和生成JSON,但是,我担心这不适用于这种情况 .
这是请求格式:
Content-type: multipart/form-data
"Description": text/plain
"Path": text/plain
"Image": image/jpeg
如何正确使用这种请求并保存图像服务器端?
[EDIT]
我设法使用以下方法来使用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
)
{
但是,在尝试使用以下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--
我收到以下错误:
javax.ws.rs.BadRequestException:org.apache.cxf.jaxrs.utils.multipart.MultipartReadException:找不到内容ID类型的多部分,请求内容类型:multipart / form-data; boundary = AaB03x
例如,当我只消耗 mode 时,它可以正常工作 . 它只会中断2个或更多参数 . 任何想法为什么是错的?
3 回答
我有时会遇到类似的问题 .
以下代码为我做了诀窍
简而言之,我认为你缺少
@Consumes
注释 .我们似乎找到了问题,它与请求的格式有关 . 正确的格式应该是:
更改为此格式允许我使用其他参数 .
用于使用多部分表单数据 . 使用@consumes标签并提供“multipart / form-data”以及值参数
@Consumes(value =“multipart / form-data”)
参考https://jnorthr.wordpress.com/2012/07/10/http-header-content-type-and-encodings/