Java后臺基于POST獲取JSON格式數(shù)據(jù)
1、直接使用request.getParamater()的方法獲取(這種取參方式對于POST和GET的提交方式均適用);
2、通過請求體的IO流獲取參數(shù)(這種方式只能用于POST,因為GET方式?jīng)]有請求體);
String s ='';InputStream in = null;BufferedInputStream bin = null;try{ in = request.getInputStream(); bin = new BufferedInputStream(in); int len = 0; byte[] b = new byte[1024]; while( (len = bin.read(b)) != -1){ s += new String(b,0,len); }} catch (IOException e) { e.printStackTrace();}finally{ try{ bin.close(); }catch (IOException e) { e.printStackTrace(); } try{ in.close(); }catch (IOException e) { e.printStackTrace(); }}//最后根據(jù)取到的字符串適用JSONUtil工具將其轉(zhuǎn)換成相應(yīng)的對象(根據(jù)JSON工具類進(jìn)行調(diào)整)類名稱 對象名 = JSONUtil.jsonToobj(s , '類名稱.clsss');
流的另一種處理方式:
InputStream in = req.getInputStream();BufferedReader bin = new BufferedReader(new InputStreamReader(in, 'utf-8'));String line = null;StringBuffer content = new StringBuffer();while ((line = bin.readLine()) != null) { content.append(line); }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. php使用正則驗證密碼字段的復(fù)雜強度原理詳細(xì)講解 原創(chuàng)2. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫金額)的函數(shù)3. 基于javaweb+jsp實現(xiàn)企業(yè)車輛管理系統(tǒng)4. XML在語音合成中的應(yīng)用5. jscript與vbscript 操作XML元素屬性的代碼6. Jsp+Servlet實現(xiàn)文件上傳下載 文件列表展示(二)7. asp.net core 認(rèn)證和授權(quán)實例詳解8. ASP.NET MVC把數(shù)據(jù)庫中枚舉項的數(shù)字轉(zhuǎn)換成文字9. ASP.NET MVC使用Boostrap實現(xiàn)產(chǎn)品展示、查詢、排序、分頁10. 基于PHP做個圖片防盜鏈
