大家經(jīng)常 用servlet和jsp,但是對(duì) request.getInputStream()和request.getReader()比較陌生。
request.getParameter()
request.getInputStream()
request.getReader()
這 三個(gè)方法都是從request對(duì)象中得到提交的數(shù)據(jù),但是用途不同,要根據(jù)<form>表單提交數(shù)據(jù)的編碼方式選擇不同的方法。
HTML中的form表單有一個(gè)關(guān)鍵屬性 enctype=application/x-www-form-urlencoded 或multipart/form-data。 ?
enctype=application/x- www-form-urlencoded是默認(rèn)的編碼方式,這種編碼方式很簡(jiǎn)單,編碼后的結(jié)果通常是 field1=value2&field2=value2&… 的形式,如 name=aaaa&Submit=Submit。這種編碼的具體規(guī)則可以在 rfc2231 里查到, 通常使用的表單也 ? 是采用這種方式編碼的,Servlet 的 API 提供了對(duì)這種 編碼方式解碼的支持,只需要調(diào)用 ServletRequest 類(lèi)中的getParameter()方法就可 ? 以得到用戶表單中的字段和數(shù)據(jù)。 ?
這種編碼方式( application/x-www-form-urlencoded )雖然簡(jiǎn)單,但對(duì)于傳輸大塊的二進(jìn)制數(shù)據(jù)顯得力不從心,對(duì)于傳輸這類(lèi)數(shù)據(jù),瀏覽器 ? 采用了另一種編碼方式,即 "multipart/form-data" 的編碼方式,采用這種方式,瀏覽器可以很容易將表單內(nèi)的數(shù)據(jù)和文件放在一起發(fā)送。這 ? 種編碼方式先定義好一個(gè)不可能在數(shù)據(jù)中出現(xiàn)的字符串作為 分界符,然后用它將各個(gè)數(shù)據(jù)段分開(kāi),而對(duì)于每個(gè)數(shù)據(jù)段都對(duì)應(yīng)著 HTML 頁(yè)面表單 ? 中的一個(gè) Input 區(qū),包括一個(gè) content-disposition 屬性,說(shuō)明了這個(gè)數(shù)據(jù)段的一些信息,如果這個(gè)數(shù)據(jù)段的內(nèi)容是一個(gè)文件,還會(huì)有? Content-Type 屬性,然后就是數(shù)據(jù)本身,如果以這種方式提交數(shù)據(jù)就要用request.getInputStream()或request.getReader()得到 提交的數(shù)據(jù) ? ,用 request.getParameter()是得不到提交的數(shù)據(jù)的。 ?
通過(guò)下面的代碼可以輸出采用 multipart/form-data的編碼提交的數(shù)據(jù)內(nèi)容:
- //1 ??
- int ?len?=?request.getContentLength();??
- byte ?buffer[]?=? new ? byte [len];??
- //2 ??
- InputStream?in?=?request.getInputStream();??
- int ?total?=? 0 ;??
- int ?once?=? 0 ;??
- while ?((total?<?len)?&&?(once?>= 0 ))?{??
- once?=?in.read(buffer,total,len);??
- total?+=?once;??
- }??
- //3 ??
- OutputStream?out= new ?BufferedOutputStream( new ?FileOutputStream( "c:\\Receive.log" , true ));??
- byte []?breaker= "\r\nNewLog:?-------------------->\r\n" .getBytes();??
- System.out.println(request.getContentType());??
- out.write(breaker, 0 ,breaker.length);??
- out.write(buffer);??
- out.close();??
從 指定的文件( Receive.log )中可以看到如下的內(nèi)容: ?
-----------------------------7d137a26e18
Content-Disposition: form-data; name="name"
123
-----------------------------7d137a26e18
Content-Disposition: form-data; name="introduce"
I am...
I am..
-----------------------------7d137a26e18
Content-Disposition: form-data; name="file3"; filename="C:\Autoexec.bat"
Content-Type: application/octet-stream
@echo off
prompt $d $t [ $p ]$_$$
SET PATH=d:\pf\IBMVJava2\eab\bin;%PATH%;D:\PF\ROSE98I\COMMON
-----------------------------7d137a26e18-- ?
上面是用 IE 進(jìn)行測(cè)試的結(jié)果,通過(guò)request.getInputStream()或request.getReader()可以得到form表單中提交的數(shù)據(jù),但 是還要對(duì)數(shù)據(jù)進(jìn)行 ? 分 析才能得到form表單提交的每個(gè)參數(shù)的值。 ?
最后注意 request.getParameter()、 request.getInputStream()、request.getReader()這三種方法是有沖突的,因?yàn)榱髦荒鼙蛔x一次。
比如: ?
當(dāng)form表單內(nèi)容采用 enctype=application/x-www-form-urlencoded編碼時(shí),先通過(guò)調(diào)用request.getParameter() 方法得到參數(shù)后,再調(diào)用 ? request.getInputStream()或request.getReader()已經(jīng)得不到流中的內(nèi)容,因?yàn)樵谡{(diào)用 request.getParameter()時(shí)系統(tǒng)可能對(duì)表單中提交的數(shù) ? 據(jù)以流的形式讀了一次,反之亦然。 ?
當(dāng)form表單內(nèi)容采用 enctype=multipart/form-data編碼時(shí),即使先調(diào)用request.getParameter()也得不到數(shù)據(jù),但是這時(shí)調(diào)用 ? request.getParameter()方法對(duì) request.getInputStream()或request.getReader()沒(méi)有沖突,即使已經(jīng)調(diào)用了 request.getParameter()方法也 ? 可以通過(guò)調(diào)用request.getInputStream()或request.getReader()得 到表單中的數(shù)據(jù),而request.getInputStream()和request.getReader()在同 ? 一個(gè)響應(yīng)中是不能混合使用的,如果混合使用就會(huì)拋異常。 ?
附帶servlet源碼:
- protected ? void ?doPost(HttpServletRequest?request,?HttpServletResponse?response)? throws ?ServletException,??
- IOException??
- {??
- System.out.println( "----------post--------------" );??
- ??
- ??
- int ?len?=?request.getContentLength();??
- byte ?buffer[]?=? new ? byte [len];??
- ??
- InputStream?in?=?request.getInputStream();??
- int ?total?=? 0 ;??
- int ?once?=? 0 ;??
- while ?((total?<?len)?&&?(once?>= 0 ))?{??
- once?=?in.read(buffer,total,len);??
- total?+=?once;??
- }??
- ??
- OutputStream?out= new ?BufferedOutputStream( new ?FileOutputStream( "c:\\Receive.log" , true ));??
- byte []?breaker= "\r\nNewLog:?-------------------->\r\n" .getBytes();??
- System.out.println(request.getContentType());??
- out.write(breaker, 0 ,breaker.length);??
- out.write(buffer);??
- out.close();??
- in.close();??
- ??
- }??
jsp源碼:
- <form?action= "http://localhost:8080/server/servlet/ReceiveServlet" ?method= "post" ?enctype= "multipart/form-data" >??
- <input?name= "aa" />??
- <input?type= "file" ?name= "myfile" >??
- <input?value= "提交" ?type= "submit" />??
- </form>??
request.getInputStream() 大多數(shù)用于 文件上傳 等
request.getParameter()、request.getInputStream()和request.getReader()
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
