溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java后臺基于POST獲取JSON格式數(shù)據(jù)

發(fā)布時間:2020-08-28 14:05:20 來源:腳本之家 閱讀:205 作者:風(fēng)繾云流 欄目:編程語言

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工具類進行調(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í)有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI