Java:HttpPost 傳輸Json數據過長使用HttpServletRequest解析


直接上代碼

/**
     * 測試生成json數據
     */
    @Test
    public void synYxGoodsInfoTest() {
        try {
            String url = "http://10.118.44.14:8070/teshi-web/goods/synYxGoods";
            GoodsInfo goodsInfo = new GoodsInfo();
            goodsInfo.setGoods_id(111);
            goodsInfo.setGoodsName("1231213");
            goodsInfo.setBrand(1);
            goodsInfo.setType(1);
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
            String jsonstr = JSON.toJSONString(goodsInfo);
            StringEntity se = new StringEntity(jsonstr);
            se.setContentType("text/json");
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            httpPost.setEntity(se);
            HttpResponse response = httpClient.execute(httpPost);

            //輸出調用結果
            if (response != null && response.getStatusLine().getStatusCode() == 200) {
                String result = EntityUtils.toString(response.getEntity());
                // 生成 JSON 對象
                JSONObject obj = JSONObject.parseObject(result);
                String errorcode = obj.getString("errorcode");
                if ("000".equals(errorcode)) {
                    System.out.println("addHkfishOrder_request_success");
                }
            }
        } catch (Exception e) {
            System.out.println("======回不來了=======");
        }
    }

 

解析

/**
     * 接收數據解析,並通過流返回數據
     */
    @RequestMapping(value = "/synYxGoods")
    @ResponseBody
    public String synYxGoods(HttpServletResponse response, HttpServletRequest request) throws IOException {
        //String json = request.getParameter("param");  //這是通過通過get方式去url 拼接的鍵值對,post方式取不到值。
        request.setCharacterEncoding("UTF-8");         //返回頁面防止出現中文亂碼
        BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));//post方式傳遞讀取字符流
        String jsonStr = null;
        StringBuilder result = new StringBuilder();
        try {
            while ((jsonStr = reader.readLine()) != null) {
                result.append(jsonStr);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        reader.close();// 關閉輸入流
        JSONObject jsonObject = JSONObject.parseObject(result.toString()); // 取一個json轉換為對象
        logger.info(jsonObject);
        GoodsInfo goodsInfo = new GoodsInfo();
        Date date = new Date();
        goodsInfo.setAccess_code1("001");
        goodsInfo.setAccess_code1("001");
        goodsInfo.setGoodsName(jsonObject.getString("goodsName"));     //通過鍵取到值,再將值封裝到類里面。               
goodsInfo.setType(Integer.parseInt(jsonObject.getString("type")));
List<ResultData<String>> data = yxGoodsService.synYxGoodsInfo(goodsInfo); String json_str = JSON.toJSONString(data); return write(response, json_str); }

 

文章轉載至:https://www.cnblogs.com/sharpest/p/6406013.html

 


免責聲明!

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



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