温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java后台基于POST获取JSON格式数据

发布时间:2020-08-28 14:05:20 来源:脚本之家 阅读:191 作者:风缱云流 栏目:编程语言

1、直接使用request.getParamater()的方法获取(这种取参方式对于POST和GET的提交方式均适用);

2、通过请求体的IO流获取参数(这种方式只能用于POST,因为GET方式没有请求体);

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();
  }
}//最后根据取到的字符串适用JSONUtil工具将其转换成相应的对象(根据JSON工具类进行调整)
类名称 对象名 = 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);
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI