欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2021-08-30 10:03:53|栏目:JAVA代码|点击:

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);
  }

上一篇:ThreadLocal内存泄漏问题解决方案

栏    目:JAVA代码

下一篇:浅谈java继承中是否创建父类对象

本文标题:Java后台基于POST获取JSON格式数据

本文地址:http://www.codeinn.net/misctech/171179.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有