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

    }

    }

    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献