@FormDataParam和@FormParam有什么区别 [英] What is difference between @FormDataParam and @FormParam
问题描述
@FormDataParam
和 @FormParam
之间有什么区别?
我在一个方法中使用了多个 @FormDataParam
,但它抛出了媒体不支持的类型错误。但是当我使用 @FormParam
时,我得到了值。
I was using multiple @FormDataParam
in a method but it was throwing media unsupported type error. But when I used @FormParam
, I got the values.
所以,我需要知道有什么区别两者之间?
So, I need to know what is the difference between the two of them?
推荐答案
-
@FormDataParam
应该与Multipart类型数据一起使用(即multipart / form-data
或MediaType.MULTIPART_FORM_DATA
),其原始形式看起来像@FormDataParam
is supposed to be used with Multipart type data (i.e.multipart/form-data
orMediaType.MULTIPART_FORM_DATA
), which in its raw form looks something likeContent-Type: multipart/form-data; boundary=AaB03x --AaB03x Content-Disposition: form-data; name="submit-name" Larry --AaB03x Content-Disposition: form-data; name="files"; filename="file1.txt" Content-Type: text/plain ... contents of file1.txt ... --AaB03x--
Multipart主要用于发送二进制数据,如非文本文件。
Multipart is mainly used for sending binary data, like non-text files.
@FormParam
用于url编码的请求参数(即application / x-www-form-urlencoded
或MediaType.APPLICATION_FORM_URLENCODED
),其原始形式如下所示@FormParam
is for url-encoded request parameters (i.e.application/x-www-form-urlencoded
orMediaType.APPLICATION_FORM_URLENCODED
), which in raw form looks likeparam1=value1¶m2=value2
这两种类型主要用于客户端表单。例如
Both of these types are mainly used in client side forms. For example
<form method="POST" action="someUrl">
<input name="gender" type="text">
<input name="name" type="text">
</form>
以上会将请求参数发送为 application / x-www-form -urlencoded
。它将以原始形式发送为
the above would send the request parameters as application/x-www-form-urlencoded
. It would get sent in raw form as
gender=male&name=peeskillet
在服务器端,我们可以为表格中的每个命名参数使用 @FormParam
/ p>
On the server side, we can use a @FormParam
for each named parameter in the form
@FormParam("gender") String gender, @FormParam("name") String name
但是如果我们需要发送一个图像以及参数, application / x-form-url-encoded
数据类型不足,因为它只处理文本。所以我们需要使用Multipart
But if we need to send say an image along with the parameters, application/x-form-url-encoded
data type is not sufficient, as it only deals with text. So we need to use Multipart
<form method="POST" action="someUrl", enctype="multipart/form-data">
<input name="gender" type="text">
<input name="name" type="text">
<input name="avatar" type="file">
</form>
这里指定了Multipart类型,现在浏览器会发出类似
Here the Multipart type is specified, now the browser will send out the request with something like
Content-Type: multipart/form-data; boundary=AaB03x
--AaB03x
Content-Disposition: form-data; name="gender"
Male
--AaB03x
Content-Disposition: form-data; name="name"
Peskillet
--AaB03x
Content-Disposition: form-data; name="avatar"; filename="image.png"
Content-Type: image/png
... binary content of image file ...
--AaB03x--
在服务器上,类似于 application / x-www-form-urlencoded
上面的示例,对于每个Multipart参数(或更准确的字段),我们可以使用 @FormDataParam
来表示每个参数
On the server, similar with the application/x-www-form-urlencoded
example above, for each Multipart parameter (or field to be more precise), we can use @FormDataParam
to signify each parameter
@FormDataParam("gender") String gender,
@FormDataParam("name") String name,
@FormDataParam("avatar") InputStream avatar
另见:
- Forms in HTML Documents
这篇关于@FormDataParam和@FormParam有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!