1. 程式人生 > >使用request.getInputStream() 獲取POST資料

使用request.getInputStream() 獲取POST資料

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();
}