Java Web 中对 ServletRequest 的一些非常

2019-10-29  本文已影响0人  過眼云烟

提取 body 中的数据

老方法
/**

* obtain request body

*

* @param request the ServletRequest

* @return body string it maybe is null

*/

public static String obtainBody(ServletRequest request) {

BufferedReader br = null;

StringBuilder sb = new StringBuilder();

try {

br = request.getReader();

String str;

while ((str = br.readLine()) != null) {

sb.append(str);

}

br.close();

} catch (IOException e) {

log.error(" requestBody read error");

} finally {

if (null != br) {

try {

br.close();

} catch (IOException e) {

log.error(" close io error");

}

}

}

return sb.toString();

}

java8新方法

String body = request.getReader().lines().collect(Collectors.joining());
上一篇 下一篇

猜你喜欢

热点阅读