【Servlet】getInputStream()与getParameterMap()获得Post请求的数据区别

Servlet表单数据处理

根据Servlet规范,如果同时满足下列条件,则请求体(Entity)中的表单数据,将被填充到request的parameter集合中(request.getParameter系列方法可以读取相关数据):
1 这是一个HTTP/HTTPS请求
2 请求方法是POST(querystring无论是否POST都将被设置到parameter中)
3 请求的类型(Content-Type头)是application/x-www-form-urlencoded
4 Servlet调用了getParameter系列方法

如果上述条件没有同时满足,则相关的表单数据不会被设置进request的parameter集合中,相关的数据可以通过request.getInputStream()来访问。反之,如果上述条件均满足,相关的表单数据将不能再通过request.getInputStream()来读取。

复制代码

Servlet Specifiaction 3.0:
3.1.1 When Parameters Are Available
The following are the conditions that mustbe met before post form data will be
populated to the parameter set:
1. The request is an HTTP or HTTPS request.
2. The HTTP method is POST.
3. The content type is application/x-www-form-urlencoded.
4. The servlet has made an initial call of any of the getParameterfamily of methods
on the request object.
If the conditions are not met and the post form data is not included in the parameter
set, the post data must still be available to the servlet via the request object’s input
stream. If the conditions are met, post form data will no longer be available for
reading directly from the request object’s input stream.

复制代码

类似的例子,还有response.getOutputStream和getWriter,它们往往也是一对矛盾体 。

从这可以看出是四个条件缺一不可的时候才能使用getParameterMap()方法来获得内容的,如果有一条不满足,就不能用这个方法。
然后我查了一下”请求的类型Content-Type“是个什么东东。于又找到一个帖子 还有很多哈,但是大概了解了一些就是:
form 中Enctype=multipart/form-data 的作用
表单中enctype=”multipart/form-data”的意思,是设置表单的MIME编码。默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;
只有使用了multipart/form-data,才能完整的传递文件数据,进行下面的操作.
在Form元素的语法中,EncType表明提交数据的格式,用 Enctype 属性指定将数据回发到服务器时浏览器使用的编码类型。
下边是说明:
(1) application/x-www-form-urlencoded:窗体数据被编码为名称/值对。这是标准的编码格式,具体的数据例子如下所示。

custname=苏林&elecontid=elecontid0001&idtype=0&idno=411303198802190512&custage=30&education=20

(2) multipart/form-data:窗体数据被编码为一条消息,页上的每个控件对应消息中的一个部分。
(3) text/plain:窗体数据以纯文本形式进行编码,其中不含任何控件或格式字符。

ENCTYPE=”multipart/form-data”用于表单里有图片上传。

<form name="userInfo" method="post" action="first_submit.jsp" ENCTYPE="multipart/form-data">

 

表单标签中设置enctype=”multipart/form-data”来确保匿名上载文件的正确编码。
如下

<tr>
<td height="30" align="right">上传企业营业执照图片:</td>
<td><INPUT TYPE="FILE" NAME="uploadfile" SIZE="34"    onChange="checkimage()"></td>
</tr>

就得加ENCTYPE=”multipart/form-data”。

表单中enctype=”multipart/form-data”的意思,是设置表单的MIME编码。默认情况,
这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;
只有使用了multipart/form-data,才能完整的传递文件数据,进行下面的操作。
enctype=”multipart/form-data”是上传二进制数据; form里面的input的值以2进制的方式传过去。
form里面的input的值以2进制的方式传过去,所以request就得不到值了。

也就是说加了这段代码,用request就会传递不成功,取表单值加入数据库时,用到下面的:

SmartUpload su = new SmartUpload();//新建一个SmartUpload对象
su.getRequest().getParameterValues();取数组值
su.getRequest().getParameter( );取单个参数单个值

 

后来又查才发现如果是指上传数据的内容的话,对于不同的文件,有很多种文件,会对应各种不同的类型。只是request默认的是application/x-www-form-urlencoded,所以用getParameterMap()能获得,用getInputStream()获得不了。

在不同的技术场景下,给POST请求传参数有不同的方法: - **Express框架**:在HTTP协议里,POST请求用于向指定资源提交数据,其数据通常包含在请求体中,而非URL的查询字符串,适合传输大量数据或敏感信息,但引用未提及Express里具体传参方法 [^1]。 - **Java Servlet**:在Servlet中,若要给POST请求传参后获取参数,可使用`getParameterMap()`方法遍历得到。此外,不论GET或POST请求,都能通过`getRequestURL`和`getParameterMap()`来得到请求完整路径 [^2]。 - **Java发送HTTP POST请求**:虽然引用提及Java发送httpPost请求可传消息头、消息体,但未给出具体传参方法 [^3]。 在Java中使用`HttpURLConnection`发送POST请求并传参的示例代码如下: ```java import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; public class HttpPostExample { public static void main(String[] args) throws Exception { String url = "http://example.com/api"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // 设置请求方法为POST con.setRequestMethod("POST"); // 设置请求头 con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 要传递的参数 String urlParameters = "param1=value1&param2=value2"; byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8); int postDataLength = postData.length; // 发送POST请求 con.setDoOutput(true); try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) { wr.write(postData); } // 获取响应 try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) { String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } System.out.println(response.toString()); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值