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