使用jersey的过滤器获取参数时发现,用
InputStream entityStream = requestContext.getEntityStream();
获取转换完成之后,进入到接口中参数就消失了,查阅资料之后发现是转换成流之后只能使用一次,所以要在转换器中重新set进一个流
//支持流重用
InputStream entityStream = requestContext.getEntityStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = entityStream.read(buffer)) > -1) {
baos.write(buffer, 0, len);
}
baos.flush();
String requestStr = this.inputStreamToString(new ByteArrayInputStream(baos.toByteArray()));
requestContext.setEntityStream(new ByteArrayInputStream(baos.toByteArray()));
String[] arrs = requestStr.split("&");
LinkedList<String> param = Lists.newLinkedList();
if(null!=arrs){
for (int i=0;i<arrs.length;i++){
param.add(arrs[i]);
}
}
//获取参数的方法
public String inputStreamToString(InputStream in) throws IOException {
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
return out.toString();
}
网友评论