【问题标题】:How to decompress json data request at server side?如何在服务器端解压json数据请求?
【发布时间】:2018-04-24 05:56:38
【问题描述】:

我有一个 python 客户端代码,它使用 compressed 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

【问题讨论】:

    标签: java rest compression


    【解决方案1】:

    由于您使用 zlib 来压缩数据,您可以使用 Gzip 或 InflaterInputStream 来解压数据,其他 stackoverflow 用户遇到了与您类似的问题,您可以找到一种方法来解压缩为 gzip here 和InflaterInputStream here ,谷歌搜索 zlib 在 java 中也找到了这个 library

    【讨论】:

    • 我之前有过这段代码。首先,我需要获取请求正文(我不知道),然后我可以解压缩。非常感谢!
    【解决方案2】:

    感谢大家的帮助,通过它我能够完成我的任务。可能需要很长时间才能完成,但值得。下面是我的工作代码。 [值得分享]

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-03
      相关资源
      最近更新 更多