后台獲取POST方式提交的JSON格式數據


1、直接使用request.getParamater()的方法獲取(這種取參方式對於POST和GET的提交方式均適用);

2、通過請求體的IO流獲取參數(這種方式只能用於POST,因為GET方式沒有請求體);

String s ="";
InputStream in = null;
BufferedInputStream bin = null;
try{
    in = request.getInputStream();
    bin = new BufferedInputStream(in);
    int len = 0;
    byte[] b = new byte[1024];
    while( (len = bin.read(b)) != -1){
        s += new String(b,0,len);
    }
} catch (IOException e) {
    e.printStackTrace();
}finally{
    try{
        bin.close();
    }catch (IOException e) {
        e.printStackTrace();
    }
     try{
        in.close();
    }catch (IOException e) {
        e.printStackTrace();
    }
}
//最后根據取到的字符串適用JSONUtil工具將其轉換成相應的對象(根據JSON工具類進行調整)
類名稱 對象名 = JSONUtil.jsonToobj(s , "類名稱.clsss");

 流的另一種處理方式:

InputStream in = req.getInputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in, "utf-8"));
String line = null;
StringBuffer content = new StringBuffer();
while ((line = bin.readLine()) != null) {
         content.append(line);
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM