servlet下的接口,获取传过来的参数

    private static String receivePost(HttpServletRequest request) {
        String result = "";
        try {
            request.setCharacterEncoding("UTF-8");
            ServletInputStream in = request.getInputStream();
            BufferedReader bf = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = bf.readLine()) != null) {
                buffer.append(line);
            }
            if (null != buffer && buffer.length() > 0) {
                result = URLDecoder.decode(buffer.toString(), "UTF-8");// 转成String并解码。
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = "-1";
        }

        return result;
    }
//下面是接口的方法,receivePost()调用上面的接收参数方法获取远程参数
public
static void messageSynchronization(HttpServletRequest request,HttpServletResponse response){ String obj = receivePost(request); PrintWriter out=null; // 定义变量保存将要返回的页面和错误信息 try { out = response.getWriter(); response.setContentType("text/json"); if (StringUtils.isNotEmpty(obj) && obj != "-1") { ****接口的主逻辑代码**** } out.println("{\"result\":\"1\"}"); }else{ out.println("{\"result\":\"0\"}"); } } }catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); out.println("{\"result\":\"-1\"}"); }finally{ if(out!=null){ out.flush(); out.close(); } } }

 

posted @ 2018-06-15 16:02  闭眼的鱼  阅读(1257)  评论(0编辑  收藏  举报