解压缩GZIP http请求,它有几个部分

时间:2017-05-05 16:06:20

标签: java http gzip

我正在做一个http代理,遇到了响应解压缩的问题,这些问题来自客户端向服务器发出请求后的服务器。

E.g。 客户端发送获取https://stackoverflow.com/questions/some_question。 服务器发送几个部分的响应。 我使用以下方法对响应部分进行解压缩。

<script src="https://unpkg.com/vue"></script>

<div id="app">
  <ul>
    <li 
      v-for="(items,index) in list" 
      draggable="true" 
      @mouseenter="startDragID(index)" 
      @dragstart="drag(items.link,index)"
      :class="{'drag' : dragIndex == index , 'not-drag' : dragIndex != index}"
    >{{items.Name}}</li>
  </ul>
</div>

在回复的第一部分我得到

public static void gzipToString(ByteBuf buf) throws IOException {
        Reader reader = null;
        reader = new InputStreamReader(new GZIPInputStream(new ByteBufInputStream(buf)));

        while (true) {
            int ch = reader.read();
            if (ch==-1) {
                break;
            }
            System.out.print((char)ch);
        }
    }

在某些时候我得到comjava.io.EOFException:ZLIB输入流的意外结束

其余的回复将在此行的异常后出现

<!DOCTYPE html>
<html itemscope itemtype="http://schema.org/QAPage">

<head>

<title>java - GZIPInputStream to String - Stack Overflow</title>
    <link rel="shortcut icon" href="https://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico?v=4f32ecc8f43d">
    <link rel="apple-touch-icon image_src" href="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon.png?v=c78bd457575a">
    <link rel="search" type="application/opensearchdescription+xml" title="Stack Overflow" href="/opensearch.xml">
    <meta name="twitter:card" content="summary">
    <meta name="twitter:app:id:googleplay" content="comjava.io.EOFException: Unexpected end of ZLIB input stream

事实上,在第一次响应时我得到EOFException让我觉得要解压缩 我需要把所有部分的反应放在一起。

但由于某种原因,所有其他部分都不以GZIP格式返回。 据我所知,只有第一部分有GZIP标题。和其他人一样,在GZIP压缩的情况下,没有标题。

我该怎么办?

1 个答案:

答案 0 :(得分:0)

你不能在ByteBuf前面拍GZIPInputStream,这没有任何意义:InputStream对完整的var Kafka = require('node-rdkafka'); //console.log(Kafka.features); //console.log(Kafka.librdkafkaVersion); var producer = new Kafka.Producer({ 'metadata.broker.list': 'localhost:9092', 'dr_cb': true }); var topicName = 'MyTest'; //logging debug messages, if debug is enabled producer.on('event.log', function(log) { console.log(log); }); //logging all errors producer.on('event.error', function(err) { console.error('Error from producer'); console.error(err); }); //counter to stop this sample after maxMessages are sent var counter = 0; var maxMessages = 10; producer.on('delivery-report', function(err, report) { console.log('delivery-report: ' + JSON.stringify(report)); counter++; }); //Wait for the ready event before producing producer.on('ready', function(arg) { console.log('producer ready.' + JSON.stringify(arg)); for (var i = 0; i < maxMessages; i++) { var value = new Buffer('MyProducerTest - value-' +i); var key = "key-"+i; // if partition is set to -1, librdkafka will use the default partitioner var partition = -1; producer.produce(topicName, partition, value, key); } //need to keep polling for a while to ensure the delivery reports are received var pollLoop = setInterval(function() { producer.poll(); if (counter === maxMessages) { clearInterval(pollLoop); producer.disconnect(); } }, 1000); }); /* producer.on('disconnected', function(arg) { console.log('producer disconnected. ' + JSON.stringify(arg)); });*/ //starting the producer producer.connect(); 进行操作。请在此处查看我的回答:https://stackoverflow.com/a/48047974/839733