java.util.zip.DataFormatException: stream error

今天收到一个bug;是红米上用wifi在web端接收文件,web端输入ip地址时,红米崩溃;

然后查看崩溃日志:java.util.zip.DataFormatException: stream error;发现是在android

在网上查了查,就看到说是使用GZipUtils方法进行GZip压缩时android4.4.4手机上会报错

gos.finish();

gos.flush();

去掉其中一个;

不过试了一下还是崩溃,就在项目里找到用这个压缩方法的地方;

public void writeTo(OutputStream outstream) throws IOException {

if (outstream == null) {

throw new IllegalArgumentException("Output stream may not be null");

}

InputStream instream = new ByteArrayInputStream(this.byteArray);

if (!isGzipByteArray) {

outstream = new GZIPOutputStream(outstream);

}

try {

copy(instream, outstream);

} finally {

instream.close();

}

}

protected void copy(InputStream instream, OutputStream outstream) throws IOException {

byte[] tmp = new byte[Constants.Config.BUFFER_LENGTH];

int l;

while ((l = instream.read(tmp)) != -1) {

outstream.write(tmp, 0, l);

}

if (outstream instanceof GZIPOutputStream) {

//注意流关闭顺序

((GZIPOutputStream) outstream).flush();

}

//注意流关闭顺序

outstream.close();

}

然后在GzipUtils里面

public void gzip(InputStream from, OutputStream to) throws IOException {

GZIPOutputStream gos = new GZIPOutputStream(to);

int count;

byte[] buffer = new byte[Config.BUFFER_LENGTH];

while ((count = from.read(buffer)) != -1) {

gos.write(buffer, 0, count);

}

/**

* 只需要一个finish()即可,否则会造成stream err,崩溃

* @author subowen 2016-12-15

*/

gos.finish();

//        gos.flush();

}

这样就不崩溃了;哈哈 菜鸟一枚,也不确定主要是什么问题,不过解决了

你可能感兴趣的:(java.util.zip.DataFormatException: stream error)