Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
压缩URL的内容-Java_Java_Http_Gzip_Bytearray_Inputstream - Fatal编程技术网

压缩URL的内容-Java

压缩URL的内容-Java,java,http,gzip,bytearray,inputstream,Java,Http,Gzip,Bytearray,Inputstream,正如标题所示,我正试图从HTTP请求中获取并压缩字符串 urlConn = url.openConnection(); int len = CONTENT_LENGTH byte[] gbytes = new byte[len]; gbuffer = new GZIPInputStream(urlConn.getInputStream(), len); System.out.println(gbuffer.read(gbytes)+"/"+len); System.out.println(gby

正如标题所示,我正试图从HTTP请求中获取并压缩字符串

urlConn = url.openConnection();
int len = CONTENT_LENGTH
byte[] gbytes = new byte[len];
gbuffer = new GZIPInputStream(urlConn.getInputStream(), len);
System.out.println(gbuffer.read(gbytes)+"/"+len);
System.out.println(gbytes);
result = new String(gbytes, "UTF-8");
gbuffer.close();
System.out.println(result);
对于一些URL,它工作得很好。我得到如下输出:

42/42
[B@96e8209
The entire 42 bytes of my data. Abcdefghij.
对于其他人,它给了我如下输出:

22/77
[B@1d94882
The entire 77 bytes of
如您所见,前几个奇数字节的数据非常相似,如果不相同,那么它们不应该导致这些问题。我似乎真的记不住了。增加
CONTENT\u LENGTH
没有任何帮助,数据流的大小比那些给我带来问题的数据流大或小,工作正常

编辑:问题也不在原始gzip数据中,因为Cocoa和Python都将其压缩而没有问题

编辑:已解决。包括最终代码:

urlConn = url.openConnection();
int offset = 0, len = CONTENT_LENGTH
byte[] gbytes = new byte[len];
gbuffer = new GZIPInputStream(urlConn.getInputStream(), len);
while(offset < len)
{
    offset += gbuffer.read(gbytes, offset, offset-len);
}
result = new String(gbytes, "UTF-8");
gbuffer.close();
urlConn=url.openConnection();
int offset=0,len=CONTENT\u LENGTH
字节[]GB=新字节[len];
gbuffer=new gzip输入流(urlConn.getInputStream(),len);
while(偏移量
数据流中可能没有数据。第一个println()表示您只读取了22个字节,因此在调用read()时只有22个字节可用。您可以尝试循环,直到读取了长度为字节的内容。可能是这样的:

int index = 0;
int bytesRead = gbuffer.read(gbytes);
while(bytesRead>0 && index<len) {
    index += bytesRead;
    bytesRead = gbuffer.read(gbytes,index,len-index);
}
int索引=0;
int bytesRead=gbuffer.read(GB字节);

while(bytesRead>0&&index数据流中可能没有数据。您的第一个println()表示您只读取了22个字节,因此在调用read()时只有22个字节可用。您可以尝试循环,直到您读取的内容长度等于字节。可能类似于:

int index = 0;
int bytesRead = gbuffer.read(gbytes);
while(bytesRead>0 && index<len) {
    index += bytesRead;
    bytesRead = gbuffer.read(gbytes,index,len-index);
}
int索引=0;
int bytesRead=gbuffer.read(GB字节);
虽然(bytesRead>0&&index
gzip输入流.read()
不能保证在一次调用中读取所有数据。您应该使用循环:

byte[] buf = new byte[1024];
int len = 0, total = 0;
while ((len = gbuffer.read(buf)) > 0) {
    total += len;
    // do something with data
}
gzip输入流.read()
不能保证在一次调用中读取所有数据。您应该使用循环:

byte[] buf = new byte[1024];
int len = 0, total = 0;
while ((len = gbuffer.read(buf)) > 0) {
    total += len;
    // do something with data
}