标签:files red rip conf cep tps app utf-8 ons
使用POST方法发送数据时,我们习惯于把数据包装成json格式。
有些情况下,我们会在Filter中读取body数据进行数据校验,
GET方法获取参数比较简单。对于POST方法,可使用如下方法从request中获取body参数:
private String getBody(HttpServletRequest request) throws IOException {
InputStream in = request.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8")));
StringBuffer sb = new StringBuffer("");
String temp;
while ((temp = br.readLine()) != null) {
sb.append(temp);
}
if (in != null) {
in.close();
}
if (br != null) {
br.close();
}
return sb.toString();
}
注意,这里有了一次request.getInputStream()
调用。
但是在测试时,一直报JSON格式不正确的错误。经调查发现,项目中使用了公司基础组件中的Filter,而该Filter中也解析了body。同时,不出所料,也是通过调用getInputStream()
方法获取的。
原来:
因此,当自己写的Filter中调用了一次getInputStream()
后,后面再调用getInputStream()
读取的数据都为空,所以才报JSON格式不正确的错误。
HttpServletRequestWrapper
进行包装缓存数据
所谓缓存数据,其实就是调用ServletRequest
的setAttribute(String s, Object o)
来存储数据。
String body = getBody(request);
request.setAttribute("body", body);
优点:
方便
缺点:
不能控制第三方Filter
getAttribute
方法就能获取数据了:request.getAttribute("body");
HttpServletRequestWrapper
包装
public class RequestWrapper extends HttpServletRequestWrapper {
private final byte[] body;
public RequestWrapper(HttpServletRequest request) throws IOException {
super(request);
body = getBodyStringFromReq(request).getBytes(Charset.forName("UTF-8"));
}
public String getBodyString() {
try {
return new String(body, "UTF-8");
} catch (UnsupportedEncodingException ex) {
return new String(body);
}
}
private String getBodyStringFromReq(ServletRequest request) {
StringBuilder sb = new StringBuilder();
InputStream inputStream = null;
BufferedReader reader = null;
try {
inputStream = request.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
}
在Filter中使用时,FilterChain.doFilter()
传入Wrapper对象:
public class TestFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
RequestWrapper requestWrapper = new RequestWrapper((HttpServletRequest)request);
String body = requestWrapper.getBodyString();
chain.doFilter(requestWrapper, response); //传入Wrapper对象
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
这样,位于后面的Filter就可以拥有唯一一次调用HttpServletRequest.getInputStream()
的机会了。
优点:
不影响第三方Filter
缺点:
多写了这么多代码,麻烦了一些
HttpServletRequest.getInputStream()多次读取问题
标签:files red rip conf cep tps app utf-8 ons
原文地址:https://www.cnblogs.com/drafire/p/11937578.html