Java Web 中对 ServletRequest 的一些非常规操作解决方案

提取 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());

你可能感兴趣的:(Java Web 中对 ServletRequest 的一些非常规操作解决方案)