java解析http+json数据包

来源:互联网 发布:linux 进入图形命令 编辑:程序博客网 时间:2024/05/21 09:59
/** * 读取请求参数 * @author 向蓬 * @date 2016-6-29上午10:56:28 * @param request * @return * @throws IOException * String */public static String readRequestBody(HttpServletRequest request){InputStream inputStream = null;BufferedInputStream buf = null;StringBuffer requestJsonBuffer = null;try {inputStream = request.getInputStream();    buf = new BufferedInputStream(inputStream);    byte[] buffer = new byte[1024];    requestJsonBuffer = new StringBuffer();    int a = 0;    while ((a = buf.read(buffer)) != -1){    requestJsonBuffer.append(new String(buffer, 0, a, "UTF-8"));    }} catch (Exception e) {e.printStackTrace();}finally{//关闭连接if (null != buf){try {buf.close();} catch (IOException e) {e.printStackTrace();}}if (null != inputStream){try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}}   return null == requestJsonBuffer ? null : requestJsonBuffer.toString();}

0 0
原创粉丝点击