中易网

处理multiparam文件上传问题,怎么解决

答案:1  悬赏:10  
解决时间 2021-03-06 16:26
处理multiparam文件上传问题,怎么解决
最佳答案
I want to upload a JPG file and a JSON-serialized Java object. On the server I am using Apache CXF, on the client I am integration testing with rest-assured.
My server code looks like:
@POST
@Path("/document")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response storeTravelDocument(
@Context UriInfo uriInfo,
@Multipart(value = "document") JsonBean bean,
@Multipart(value = "image") InputStream pictureStream)
throws IOException
{}

My client code looks like:
given().
multiPart("document", new File("./data/json.txt"), "application/json").
multiPart("image", new File("./data/image.txt"), "image/jpeg").
expect().
statusCode(Response.Status.CREATED.getStatusCode()).
when().
post("/document");

Everything works fine when I read the json part from the file as in the first multiPart line. However, when I want to serialize the json instance I come into problems. I tried many variants, but none worked.
I thought this variant should work: on the client
JsonBean json = new JsonBean();
json.setVal1("Value 1");
json.setVal2("Value 2");

given().
contentType("application/json").
formParam("document", json).
multiPart("image", new File("./data/image.txt"), "image/jpeg").
...

and on the server
public Response storeTravelDocument(
@Context UriInfo uriInfo,
@FormParam(value = "document") JsonBean bean,
@Multipart(value = "image") InputStream pictureStream)

but no. Can anyone tell me how it should be?

2016年03月06日43分24秒

Try different approach (worked for me), I am not sure if this is suitable in your case.
Make JsonBean a JAXB entity, that it add @XmlRootEntity above class definition.
Then, instead of formParam
given().
contentType("application/json").
body(bean). //bean is your JsonBean
multiPart("image", new File("./data/image.txt"), "image/jpeg").

then
public Response storeTravelDocument(
@Context UriInfo uriInfo,
JsonBean bean, //should be deserialized properly
@Multipart(value = "image") InputStream pictureStream)

I've never tried that with @Multipart part, but, hopefully it would work.

2016年03月06日43分24秒

Multipart/form-data follows the rules of multipart MIME data streams, see w3.org. This means that each part of the request forms a part in the stream. Rest-assured supports already simple fields (strings), files and streams, but not object serialization into a part. After asking on the mailing list, Johan Haleby (the author of rest-assured) suggested to add an issue. The issue is already accepted, see issue 166.
The server will stay as it is:
@POST
@Path("/document")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response storeTravelDocument(
@Context UriInfo uriInfo,
@Multipart(value = "document") JsonBean bean,
@Multipart(value = "image") InputStream pictureStream)
throws IOException
{}

The client code will look like:
given().
multiPartObject("document", objectToSerialize, "application/json").
multiPart("image", new File("./data/image.txt"), "image/jpeg").
expect().
statusCode(Response.Status.CREATED.getStatusCode()).
when().
post("/document");

Maybe the name "multiPartObject" will change. We will see once it is implemented.

2016年03月06日43分24秒
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
有什么东西喷在身上蚊子不会咬、急急…
2013年凯美瑞经典豪华版卖多少钱
大连艺术学院的艺术类文化课分数线是他们学校
仁安商务酒店在什么地方啊,我要过去处理事情
印度治安真的很乱吗
什么叫台原生态系统
梦幻西游,本人玩了个69力五庄,刚才去杀个知
若a∈(0,π/2)利用单位圆证明:sina+cosa>
谁能帮我设计一个婚礼主题的LOGO,用名字的尾
男生皮肤黑的人穿玫瑰红的衣服好看嘛
金黄沟怎么去啊,有知道地址的么
u盘无法识别 并且发烫
什么是中国传统语言文化?
电抗器施加工频耐压首端接试验高压尾端是否需
博艺广告我想知道这个在什么地方
推荐资讯
拉立怎么去啊,有知道地址的么
找一款 大字体的手机炒股软件。给个下载地址
金运养车坊在哪里啊,我有事要去这个地方
正方:穿越剧的热播对人们对历史的认识利大于
蓝月亮形象设计中心(健康路)在什么地方啊,我
青岛一个45尺柜的费用大概是多少,场站费,送
巧姐副食地址在什么地方,想过去办事
LOHO公寓是什么
谁给发下树脂工艺品的主要材料是什么?以及相
2012年4月18日下午6点46分出生的男孩王姓天字
为什么圆通速递7621258473从广州转运中心发到
67年农业8月28日,阳历是甚麼时候?
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?