HttpServletRequest 中获取httpbody内容


  //可以使用 ServletInputStream
   @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            int contentLength = req.getContentLength();
            byte[] buffer = new byte[contentLength];
            ServletInputStream in = null;
            try {
                in = req.getInputStream();
                in.read(buffer, 0, contentLength);
                in.close();
                String bodyData = new String(buffer, StandardCharsets.UTF_8);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (null != in) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
}
posted @ 2021-04-03 11:20  fly_bk  阅读(673)  评论(0)    收藏  举报