这篇文章主要介绍了如何通过ServletInputStream读取http请求传入的数据,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
这篇文章主要介绍了如何通过ServletInputStream读取http请求传入的数据,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
这篇文章主要介绍了如何通过ServletInputStream读取http请求传入的数据,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
问题提出:使用nodejs的http向java web发送请求,java后台未收到数据。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
/** * 增加数据 * @param 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<String, Object> map = new HashMap<String, Object>(); int len = ((DBManager) conn).insertOne(obj); map.put("status", (len == 0)?("SUCCESS"):("ERROR")); return MVC.toString(map);} |
使用时需要转化成JSON
|
1
2
|
JSONObject jsonObj = JSONObject.fromObject(strcont);System.out.println(jsonObj.getString("module")); |
需要的jar包:

如图像等。这是一个抽象类。
ServletRequest接口的getInputStream()方法返回ServletInputStream类的实例。
所以可以得到:
|
1
|
ServletInputStream sin=request.getInputStream(); |
Java
int readLine(byte[] b, int off, int len) - 它读取输入流。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持米米素材网。
原文链接:https://blog.csdn.net/swl979623074/article/details/52992733
发表评论