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啊。

解决方案 »

  1.   

    可能跟字符集有关系,应该不是bug
    如果你指定base64编码的字符集,我猜结果肯定一样!
      

  2.   

    servlet运行平台的默认编码和桌面的默认编码一致吗
      

  3.   

    http://bbs.csdn.net/topics/390298088
    80分算法题
      

  4.   

    话说,LZ写代码都从来不考虑字符集的吗??
    decompressed = out.toString("utf-8");
    自己再去试....
    自己不会用就说java有bug...你不是在找喷吗
      

  5.   

    hh
    过来顶下lz
    分给我啊
    虽然你有点冲动
    但是我觉得你很有怀疑精神!
      

  6.   

    你用servlet的时候servlet容器会提供一个编码,和虚拟机提供的编码不一样的
      

  7.   

    如果你觉得你发现了BUG,你可以直接提交问题给甲骨文,说不定还能获得一笔奖金。
    但是,我觉得99.9999%的可能性,是因为你没有能理解原理而导致的错误,而不是jdk本身的BUG
    难道你觉得这种情况,别人都没遇到过吗?又不复杂。。