簡體   English   中英

如何在服務器端解壓json數據請求?

[英]How to decompress json data request at server side?

我有一個Python客戶端代碼,該代碼使用壓縮的 JSON數據命中URL

我想在java中 解壓縮並打印JSON數據(從客戶端請求中獲取)。

客戶代碼:

  #!/usr/bin/python

  import sys, getopt
  import requests
  import json
  from zlib import compress

  s = requests.session()
  url = "http://1.1.1.1:8080/eventfull/send/data/"
  payload = dict(
    username="test",
    password="test123",
    emailid=sys.argv[1],
    campaignfrom="info@newsletter.x.com",
    send_id="1234",
    istest="1",
    render_id=sys.argv[2],
    subject="Eventfull :: Services HeartBeat",
    htmlbody="<html><body><p>Hi Team,</p></br></br><p>This is a Test Campaign to ensure eventfull calls are working as expected</p></br></br><p>Thanks,</p><br><p>Tech Team</p></body></html>",
    textbody="Testing"
  )
  json_string = json.dumps(payload)
  compressed = compress(json_string,9)
  response = s.post(url, data=compressed )

  print response.status_code
  print response.content

由於您使用的zlib壓縮數據,你既可以用gzip或的InflaterInputStream解壓縮數據,其他計算器用戶面臨着類似的問題,以你的,你能找到一種方法來解壓以gzip格式在這里 ,為的InflaterInputStream 這里 ,一Google在Java中搜索zlib的小工具也提出了這個

謝謝大家的幫助,我可以通過它完成我的任務。 完成可能需要很長時間,但值得。 下面是我的工作代碼。 [值得分享]

@POST  
@Path("/Data")  
@Consumes("application/zip")


public Response uploadFile2(InputStream incomingData) {




    try {
        //Decompressing...
        InflaterInputStream inf = new InflaterInputStream(incomingData);

        //Extracting Json Data....
        JSONParser jsonParser = new JSONParser();
        JSONObject jsonObject = (JSONObject)jsonParser.parse(
                new InputStreamReader(inf, "UTF-8"));


        String name = (String) jsonObject.get("username");
    }
    catch(SQLException se){
        System.out.println(se.getLocalizedMessage());
    }


    catch (Exception e) {
        e.printStackTrace();
    }


    // return HTTP response 200 in case of success
    return Response.status(200).entity("Success").build();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM