java - 用于解密 GZIP 正文的 Rest Api

标签 java rest gzip

API 正在以 GZIP 形式发送正文中的大量数据,我需要创建 REST API 来解密并将其保存在数据库中,但我无法解密数据。

`@GetMapping
    public void hello() throws IOException {
        String payload = "{\n" +
                "    \"name1\": \"shrikant\",\n" +
                "    \"date\": \"Fri Apr 05 15:48:59 IST 2019\"\n" +
                "}";
        String urlStr = "http://localhost:8080/hello";
        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(60000);
        conn.setConnectTimeout(60000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.addRequestProperty("Content-Encoding", "gzip");
        OutputStream os = conn.getOutputStream();
        GZIPOutputStream gos = new GZIPOutputStream(os);
        gos.write(payload.getBytes(StandardCharsets.UTF_8));
        System.out.println("payload " + 
        Arrays.toString(payload.getBytes(StandardCharsets.UTF_8)));
        os.close();
        int responseCode = conn.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);
    }`

用于接收数据的API。

     @PostMapping("hello")
    public byte[] hello1(HttpServletRequest request) throws IOException {
        System.out.println("hi");
        ByteArrayInputStream bis = new ByteArrayInputStream();
        GZIPInputStream gis = new GZIPInputStream(bis);
        BufferedReader br = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
        StringBuilder sb = new StringBuilder();
        String line;
        while((line = br.readLine()) != null) {
            sb.append(line);
        }
    }

但无法解密数据。

  • 如何解密请求。

最佳答案

在客户端,您应该在关闭 OutputStream 之前关闭 GZIPOutputStream。

    gos.close();
    os.close();

在服务器端,您应该使用来自请求的InputStream

    ServletInputStream inputStream = request.getInputStream();
    GZIPInputStream gis = new GZIPInputStream(inputStream);
    BufferedReader br = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
    StringBuilder sb = new StringBuilder();
    String line;
    while((line = br.readLine()) != null) {
        sb.append(line);
    }

    System.out.println(sb.toString());

为了更好地使用 try-with-resources block ,您将不必记住关闭流。

    try (OutputStream os = conn.getOutputStream()) {
        try (GZIPOutputStream gos = new GZIPOutputStream(os)) {
            gos.write(payload.getBytes(StandardCharsets.UTF_8));
        }
    }

关于java - 用于解密 GZIP 正文的 Rest Api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55570962/

相关文章:

java - 查找 Java 中是否存在 Rest 服务中的方法

java - swing 组件的名称属性是什么?

java - 使用回收器 View android studio访问其他 View

rest - 具有ravendb id的ServiceStack路由

performance - 这是真的? "Gzipped items will not have the same etags (even if the content did not change)"

java - JTable 的 Customrenderer 被多次调用

python-3.x - 在Python3中将XML文件发送到REST API

iphone - iOS 上除了 Restkit 之外的简单 Restful 框架/客户端?

ruby - 如何在 Ruby Sinatra 中读取 GZIP 负载

r - 寻求gz连接是不可预测的