如何通过ServletInputStream读取http请求传入的数据

网友投稿 290 2022-09-20


如何通过ServletInputStream读取http请求传入的数据

目录通过ServletInputStream读取http请求传入的数据1. 使用ServletInputStream获取传入的数据2. 通过ServletInputTthkowjVLuStream获取的是String类型ServletInputStream类ServletInputStream类提供流从请求对象读取二进制数据

通过ServletInputStream读取http请求传入的数据

问题提出:使用nodejs的http向java web发送请求,java后台未收到数据。

1. 使用ServletInputStream获取传入的数据

/**

* 增加数据

* @pahttp://ram module

* @param function

* @param system

* @param platform

* @param time

* @param status

* @return ModelAndView

* @throws IOException

*/

@RequestMapping("/insertOne")

public ModelAndView insertOne(HttpServletRequest req) throws IOException {

ServletInputStream ris = req.getInputStream();

StringBuilder content = new StringBuilder();

byte[] b = new byte[1024];

int lens = -1;

while ((lens = ris.read(b)) > 0) {

content.append(new String(b, 0, lens));

}

String strcont = content.toString();// 内容

JSONObject jsonObj = JSONObject.fromObject(strcont);

DBObject obj = new BasicDBObject();

obj.put("module", jsonObj.getString("module"));

obj.put("function", jsonObj.getString("function"));

obj.put("system", jsonObj.getString("system"));

obj.put("platform", jsonObj.getString("platform"));

obj.put("time", jsonObj.getString("time"));

obj.put("status", jsonObj.getString("status"));

Map map = new HashMap();

int len = ((DBManager) conn).insertOne(obj);

map.put("status", (len ==TthkowjVLu 0)?("SUCCESS"):("ERROR"));

return MVC.toString(map);

}

2. 通过ServletInputStream获取的是String类型

使用时需要转化成JSON

JSONObject jsonObj = JSONObject.fromObject(strcont);

System.out.println(jsonObj.getString("module"));

需要的jar包:

ServletInputStream类

ServletInputStream类提供流从请求对象读取二进制数据

如图像等。这是一个抽象类。

ServletRequest接口的getInputStream()方法返回ServletInputStream类的实例。

所以可以得到:

ServletInputStream sin=request.getInputStream();

Java

ServletInputStream类的方法

ServletInputStream类中只定义了一种方法

int readLine(byte[] b, int off, int len) - 它读取输入流。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:OSPF多区域与路由的重分发(不同ospf进程间路由重分布)
下一篇:有没有好奇过,用浏览器访问某个网站时,中间经历了什么,如何到达对方的?(初学版)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~