使用jersey的過濾器獲取參數(shù)時(shí)發(fā)現(xiàn),用
InputStream entityStream = requestContext.getEntityStream();
獲取轉(zhuǎn)換完成之后,進(jìn)入到接口中參數(shù)就消失了,查閱資料之后發(fā)現(xiàn)是轉(zhuǎn)換成流之后只能使用一次,所以要在轉(zhuǎn)換器中重新set進(jìn)一個(gè)流
//支持流重用
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]);
}
}
//獲取參數(shù)的方法
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();
}