Java入参 出参 最初方法

时间:2022-03-14 21:08:00

获取入参的参数(json):

 

    String inputLine;
                String a = "";
                try {
                  while ((inputLine = request.getReader().readLine()) != null) {
                    a += inputLine;
                  }
                  request.getReader().close();
                } catch (Exception e) {
        
                  e.printStackTrace();
                }
                System.out.println(a);

 

输出参数的方法:

/** 
 * 以JSON格式输出 
 * @param response 
 */  
protected void responseOutWithJson(HttpServletResponse response,  
        Object responseObject) {  
    //将实体对象转换为JSON Object转换  
    JSONObject responseJSONObject = JSONObject.fromObject(responseObject);  
    response.setCharacterEncoding("UTF-8");  
    response.setContentType("application/json; charset=utf-8");  
    PrintWriter out = null;  
    try {  
        out = response.getWriter();  
        out.append(responseJSONObject.toString());  
        logger.debug("返回是\n");  
        logger.debug(responseJSONObject.toString());  
    } catch (IOException e) {  
        e.printStackTrace();  
    } finally {  
        if (out != null) {  
            out.close();  
        }  
    }  
} 

 或

     

responseOutWithJson((HttpServletResponse)context.getResponse(), resultJson);

/** * 以JSON格式输出 * @param response */ private void responseOutWithJson(HttpServletResponse response, JSONObject responseObject) { //将实体对象转换为JSON Object转换 PrintWriter out = null; try { // ((ServletRequest) response).setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=utf-8"); out=new PrintWriter(response.getOutputStream()); out.append(responseObject.toString()); out.flush(); }catch (IOException e) { e.printStackTrace(); }finally { if (out != null) { out.close(); } } }

 

读数据流:

InputStream input= request.getInputStream();