简体   繁体   中英

How to decompress json data request at server side?

I have a python client side code which hits an URL with compressed JSON data.

I want to decompress and print the JSON data (that i got from client request) in java .

Client code:

  #!/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的小工具也提出了这个

Thank you all for your help, through which i could able to complete my task. It might took so long to complete but worth it. Below is my working code. [Worth sharing]

@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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM