我一直在一个 Web 服务中工作,该服务使用 Apache CXF 与 Jackson 结合使用和生成 JSON 文件。
但是,该服务的方法之一应该能够保存从移动应用程序上传的图像,该应用程序向我的网络服务发出多部分/表单数据 POST 请求,并且我不知道如何在我的应用程序中处理这种内容类型语境。我们通常创建“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的多部分type找到,请求内容类型:multipart/form-data;boundary=AaB03x
当我只消费mode,例如,它工作得很好。它只会中断 2 个或更多参数。知道为什么这是错误的吗?