跟据HttpRequest获取body内数据

1 . 字节流

 


InputStream inputStream = null ;
try {
inputStream = request.getInputStream();
BufferedInputStream byteOutputStream = new BufferedInputStream( inputStream );
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int a ;
while( ( a = byteOutputStream.read( bytes ) ) != -1){
byteArrayOutputStream.write( bytes , 0 , a);
}
String s = byteArrayOutputStream.toString("utf-8");
return s;
}catch(IOException e){
e.printStackTrace();
return e.getMessage();
}finally{
if(inputStream != null ){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}



2 . 字符流

BufferedReader reader = null ;
try {
reader = request.getReader();
StringBuffer sb = new StringBuffer();
String str ;
while (null != (str = reader.readLine())){
sb.append( str );
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
return e.getMessage();
}finally {
if(null == reader){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


3 . RequestBody方式 直接Map 或者 对象来接收 ,这个需要知道对方传输过来的样式

 

 

    这样Map就可以直接拿到了 . 


posted @ 2020-06-10 18:20  java_小跟班  阅读(10778)  评论(0编辑  收藏  举报