当前位置: 代码迷 >> 综合 >> 使用request.getInputStream() 获取POST数据
  详细解决方案

使用request.getInputStream() 获取POST数据

热度:11   发布时间:2023-12-14 04:56:31.0
InputStream is = request.getInputStream(); 
DataInputStream input = new DataInputStream(is); 
String str =input.readUTF(); 分离参数方法 
public String splitString(String str,String temp){
     
String result = null; 
if (str.indexOf(temp) != -1) {
     
if (str.substring(str.indexOf(temp)).indexOf("&") != -1) {
     
result = str.substring(str.indexOf(temp)).substring(str.substring(str.indexOf(temp)).indexOf("=")+1, str.substring(str.indexOf(temp)).indexOf("&")); 
} else {
     
result = str.substring(str.indexOf(temp)).substring(str.substring(str.indexOf(temp)).indexOf("=")+1); 
} 
} 
return result; } 调用this.splitString(str, "id=") 对post和get传递参数分别获取 //获取post参数 
StringBuffer sb = new StringBuffer() ; 
InputStream is = request.getInputStream(); 
InputStreamReader isr = new InputStreamReader(is);   
BufferedReader br = new BufferedReader(isr); 
String s = "" ; 
while((s=br.readLine())!=null){
     
sb.append(s) ; 
} 
String str =sb.toString(); //防止用get传递参数 
if(str.equals("")){
     
if(request.getQueryString() != null) {
     
str = request.getRequestURL()+"?"+request.getQueryString(); 
} else {
     
str = request.getRequestURL().toString(); 
} 
}

转载自:http://lionlx.iteye.com/blog/787456

  相关解决方案