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

用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();  
    

    }

    }

    评论

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊