ntsjcao 2016-08-31 03:34 采纳率: 0%
浏览 5670
已结题

用pako.js压缩字符串,如何在后端用java解开?

我压缩采用pako.js
js压缩代码: function compressStr() {
var sstr = '我是未压缩的字符串';
sstr = decodeURIComponent(encodeURIComponent(sstr));
var cpredString = pako.deflate(sstr,{to:'string'});
return cpredString;

    }
我的java解压缩代码
public static byte[] decompressStrToBytes(String instr) throws IOException {

    ByteArrayInputStream bis = new ByteArrayInputStream(instr.getBytes("utf-8"));
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    InflaterInputStream iis = new InflaterInputStream(bis, new Inflater(true));
    int readCount = 0;
    byte[] buf = new byte[1024];
    try {
        while ((readCount = iis.read(buf, 0, buf.length)) > 0) {
            bos.write(buf, 0, readCount);
        }
        iis.close();

    } catch (IOException e) {
        System.out.println(e.getMessage());
    }

     return bos.toByteArray();
    }
    前端数据可以正确交给服务端,服务端无法正确解压,错误:invalid stored block lengths
    请教各位大神。
  • 写回答

3条回答 默认 最新

  • devine007 2016-08-31 06:40
    关注

    把压缩的字符串toByte下传入下述方法试下呢
    public static byte[] decompress(byte[] value) throws DataFormatException {

    ByteArrayOutputStream bos = new ByteArrayOutputStream(value.length);  
    
    Inflater decompressor = new Inflater();  
    
    try {  
      decompressor.setInput(value);  
    
      final byte[] buf = new byte[1024];  
      while (!decompressor.finished()) {  
        int count = decompressor.inflate(buf);  
        bos.write(buf, 0, count);  
      }  
    } finally {    
      decompressor.end();  
    }  
    
    return bos.toByteArray();  
    

    }

    }

    评论

报告相同问题?