关于request.getInputStream()接参流程

Posted 班轮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于request.getInputStream()接参流程相关的知识,希望对你有一定的参考价值。

spring接到浏览器传来的post请求 所传进来的参数都在request里

1  @RequestMapping(value = PROXY_URL, method = RequestMethod.POST, produces = PRODUCES)
2     public Object proxy(final HttpServletRequest request, final HttpServletResponse response) {
3       String json = "";
4                 json = new String(readInputStream(request.getInputStream()), "UTF-8");  
5     }

此时debug查看request.getInputStream()的值是全部请求信息 但是并非我们想要是参数

 

 所以我们要对结果解析

readInputSream()是解析方法

 1     public static byte[] readInputStream(InputStream inStream) throws Exception {
 2         ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
 3         byte[] buffer = new byte[1024];
 4         boolean var3 = false;
 5 
 6         int len;
 7         while((len = inStream.read(buffer)) != -1) {
 8             outSteam.write(buffer, 0, len);
 9         }
10 
11         outSteam.close();
12         inStream.close();
13         return outSteam.toByteArray();
14     }
inStream.read(buffer)读取数据赋值给buffer

 

outSteam的结果就是我们想要的数据了

注意 这里的json格式的数据是前端定义好传进来的 和后台对io流解析无关 

 

以上是关于关于request.getInputStream()接参流程的主要内容,如果未能解决你的问题,请参考以下文章

如何多次读取 request.getInputStream()

request.getParameter()request.getInputStream()和request.getReader()

post方法 request.getInputStream()为空解惑

getParameter() getInputStream()和getReader() 区别

request.getInputStream为空的改进办法

springbootboot-HttpServletRequest.getInputStream() 获取post内容