package com.yicha.utility;import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;public class TestUtil {
public static String gunzip(String compressedStr){
if(compressedStr==null){
return null;
}
ByteArrayOutputStream out= new ByteArrayOutputStream();
ByteArrayInputStream in=null;
GZIPInputStream ginzip=null;
byte[] compressed=null;
String decompressed = null;
try {
compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
in=new ByteArrayInputStream(compressed);
ginzip=new GZIPInputStream(in); byte[] buffer = new byte[1024];
int offset = -1;
while ((offset = ginzip.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
decompressed=out.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ginzip != null) {
try {
ginzip.close();
} catch (IOException e) {}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {}
}
}
return decompressed;
}
public static void toStart(){
String m = TestUtil.gunzip("H4sIAAAAAAAAAAEYAOf/YWJj5rC055S16LS5YWJj5ZKM6JCo5ouJwwfq+RgAAAA=");
System.out.println("测试:"+m);
}
//直接运行和在servlet中调用Start()方法结果不一样
public static void main(String[] args) throws IOException {
toStart();
}
}
这是一个工具类,main方法直接运行打印出结果,
把这个类在servlet中调用toStart(),打印结果不一样。
天大的bug啊。
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;public class TestUtil {
public static String gunzip(String compressedStr){
if(compressedStr==null){
return null;
}
ByteArrayOutputStream out= new ByteArrayOutputStream();
ByteArrayInputStream in=null;
GZIPInputStream ginzip=null;
byte[] compressed=null;
String decompressed = null;
try {
compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
in=new ByteArrayInputStream(compressed);
ginzip=new GZIPInputStream(in); byte[] buffer = new byte[1024];
int offset = -1;
while ((offset = ginzip.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
decompressed=out.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ginzip != null) {
try {
ginzip.close();
} catch (IOException e) {}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {}
}
}
return decompressed;
}
public static void toStart(){
String m = TestUtil.gunzip("H4sIAAAAAAAAAAEYAOf/YWJj5rC055S16LS5YWJj5ZKM6JCo5ouJwwfq+RgAAAA=");
System.out.println("测试:"+m);
}
//直接运行和在servlet中调用Start()方法结果不一样
public static void main(String[] args) throws IOException {
toStart();
}
}
这是一个工具类,main方法直接运行打印出结果,
把这个类在servlet中调用toStart(),打印结果不一样。
天大的bug啊。
如果你指定base64编码的字符集,我猜结果肯定一样!
80分算法题
decompressed = out.toString("utf-8");
自己再去试....
自己不会用就说java有bug...你不是在找喷吗
过来顶下lz
分给我啊
虽然你有点冲动
但是我觉得你很有怀疑精神!
但是,我觉得99.9999%的可能性,是因为你没有能理解原理而导致的错误,而不是jdk本身的BUG
难道你觉得这种情况,别人都没遇到过吗?又不复杂。。