關於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流解析無關 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM