跟据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就可以直接拿到了 . 



免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM