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);
}
栏 目:JAVA代码
本文地址:http://www.codeinn.net/misctech/171179.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




