[英]Accessing parts of a multipart/form-data post request in a Java REST web service
I have a multipart form which is supposed to upload a file as well as some parameters. 我有一个多部分表单,它应该上传一个文件以及一些参数。 It looks like this:
它看起来像这样:
<form id="upload" action="http://localhost:9998/test" method="post" enctype="multipart/form-data">
<input name="inputfile" type="file" size="50" accept="application/octet-stream">
<input name="someparameter" type="text" size="10">
<input type="submit" value="Go!">
</form>
The web service looks like this: Web服务看起来像这样:
@Path("/test")
public class ServiceInterface {
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void execute(@FormParam(value="someparameter") String param) {
System.out.println(param);
}
}
When submitting the form, the value for "someparameter" is always reported as null although in the form I entered a value. 提交表单时,“someparameter”的值始终报告为null,尽管在我输入的值的形式中。
My questions are: 我的问题是:
I am using Jersey 1.10. 我正在使用Jersey 1.10。
Ok, after googling quite a few hours I found the error in my code. 好吧,经过谷歌搜索几个小时后,我在代码中发现了错误。
You have to use the annotation @FormDataParam instead of @FormParam. 您必须使用注释@FormDataParam而不是@FormParam。
The resulting code looks like this: 生成的代码如下所示:
@Path("/test")
public class ServiceInterface {
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void execute(
@FormDataParam("someparameter") String param
@FormDataParam("inputfile") File inputfile
)
{
System.out.println(param);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.